public
Created

  • Download Gist
dom.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("root");
rootElement.appendChild(doc.createTextNode("A\u0000B"));
doc.appendChild(rootElement);
 
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
System.out.println(new String(os.toByteArray(), StandardCharsets.UTF_8));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.