Skip to content

Instantly share code, notes, and snippets.

@lethee
Last active December 22, 2016 15:11
Show Gist options
  • Save lethee/5e474a60c5c802a6cb6712ff7d76c17e to your computer and use it in GitHub Desktop.
Save lethee/5e474a60c5c802a6cb6712ff7d76c17e to your computer and use it in GitHub Desktop.
package com.lethee;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class Main {
public static String getExampleXmlString() {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" ?>");
sb.append("<root>");
sb.append(" <item>");
sb.append(" <![CDATA[ Gong &amp; You ]]>");
sb.append(" </item>");
sb.append("</root>");
return sb.toString();
}
public static void read() throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(getExampleXmlString())));
NodeList names = doc.getElementsByTagName("item");
Node name = names.item(0);
for (int i = 0; i < name.getChildNodes().getLength(); ++i) {
Node nameChild = name.getChildNodes().item(i);
if (nameChild instanceof CDATASection) {
String text = ((CDATASection) nameChild).getData();
System.out.println(text);
}
}
}
public static String toXmlString(Document doc) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter out = new StringWriter();
tf.transform(new DOMSource(doc), new StreamResult(out));
return out.toString();
}
public static void write() throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElement("root");
doc.appendChild(rootElement);
Element item1 = doc.createElement("item");
item1.appendChild(doc.createCDATASection("Gong < & > You")); // CDATA is not 'escape'-ed
rootElement.appendChild(item1);
Element item2 = doc.createElement("item");
item2.appendChild(doc.createTextNode("Gong < & > You")); // Text is 'escape'-ed
rootElement.appendChild(item2);
// HTML escape table
// http://www.freeformatter.com/html-entities.html
System.out.println(toXmlString(doc));
}
public static void main(String[] args) throws Exception {
read();
write();
}
}
/* output
Gong &amp; You
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<item><![CDATA[Gong < & > You]]></item>
<item>Gong &lt; &amp; &gt; You</item>
</root>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment