Java tutorial
import java.io.StringReader; import java.io.StringWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; public class Main { public static void main(String[] args) throws Exception { XMLOutputFactory xof = XMLOutputFactory.newFactory(); StringWriter sw = new StringWriter(); XMLStreamWriter xsw = xof.createXMLStreamWriter(sw); xsw.writeStartDocument(); xsw.writeStartElement("foo"); xsw.writeCharacters("<>\"&'"); xsw.writeEndDocument(); String xml = sw.toString(); System.out.println(xml); // READ THE XML XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml)); xsr.nextTag(); // Advance to "foo" element System.out.println(xsr.getElementText()); } }