プログラマメモ2 - programmer no memo2

XMLを出力 2007/04/06

XMLデータをインデントして出力するためのテスト。
XMLSerializerを使うのが簡単なのだが、deprecatedになっている。
使ったxercesのバージョンは2.9.0



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.xerces.dom.CoreDOMImplementationImpl;
import org.apache.xerces.dom.CoreDocumentImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;

public class TestXml {
static String xml = "<root><test name=\"01\"><item>item1</item></test></root>";

public static void main(String[] args) throws UnsupportedEncodingException,
SAXException, IOException, ParserConfigurationException {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

Document document = factory.newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes("utf-8")));
System.out.println(">>>>XMLSerializerを使用した結果");
System.out.println(serialize(document));
System.out.println(">>>>Transformerを使用した結果");
System.out.println(serialize2(document));
System.out.println(">>>>Transformerを使用した結果");
System.out.println(serialize3(xml));
System.out.println(">>>>LSSerializerを使用した結果");
System.out.println(serialize4(document));
}

protected static String serialize(Document document) {
try {
OutputFormat format = new OutputFormat(document, "utf-8", true);
StringWriter writer = new StringWriter();
XMLSerializer serializer = new XMLSerializer(writer, format);
serializer.serialize(document);
String result = writer.toString();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected static String serialize2(Document document) {
try {

Source source = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();

Writer writer = new StringWriter();
Result result = new StreamResult(writer);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);

return writer.toString();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}

protected static String serialize3(String s) {
try {

Source source = new StreamSource(new ByteArrayInputStream(s
.getBytes("utf-8")));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Writer writer = new StringWriter();
Result result = new StreamResult(writer);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

transformer.transform(source, result);

return writer.toString();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

protected static String serialize4(Document document) {

CoreDocumentImpl coreDocumentImpl = new CoreDocumentImpl();
DOMImplementation implementation = coreDocumentImpl.getImplementation();
CoreDOMImplementationImpl impl2 = (CoreDOMImplementationImpl) implementation;
LSSerializer serializer = impl2.createLSSerializer();

return serializer.writeToString(document);
}

}


>>>>XMLSerializerを使用した結果
<?xml version="1.0" encoding="utf-8"?>
<root>
<test name="01">
<item>item1</item>
</test>
</root>

>>>>Transformerを使用した結果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test name="01">
<item>item1</item>
</test>
</root>

>>>>Transformerを使用した結果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test name="01">
<item>item1</item>
</test>
</root>

>>>>LSSerializerを使用した結果
<?xml version="1.0" encoding="UTF-16"?>
<root><test name="01"><item>item1</item></test></root>

: