Java HTML / XML How to - Escape String in StAX








Question

We would like to know how to escape String in StAX.

Answer

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
//from w  ww  .  jav a  2 s .  c o  m
public class Main {

  public static void main(String[] args) throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

    xsw.writeStartDocument();
    xsw.writeStartElement("response");
    xsw.writeStartElement("message");
    xsw.writeCharacters("1 < 2");
    xsw.writeEndDocument();

    xsw.close();
  }

}