0 Replies Latest reply: Feb 1, 2013 11:59 AM by 740130 RSS

    how to escape special characters while writing xml with StAX

    740130
      I couldn't escape the special characters while writing xml with StAX. I am using BEA/Oracle implementation (com.bea.core.weblogic.stax_1.9.0.0.jar) on Weblogic10.3.5
      e.g. character ä

      public static void main(String[] args) throws XMLStreamException {
      XMLEventFactory eventFactory = XMLEventFactory.newInstance();
      XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
      XMLEventWriter writer = outputFactory.createXMLEventWriter(System.out);
      writer.add(eventFactory.createStartDocument());
      writer.add(eventFactory.createStartElement("", null, "test", null, null));
      writer.add(eventFactory.createCharacters("test special characters & and ä "));
      writer.add(eventFactory.createEndElement("", null, "test"));
      writer.add(eventFactory.createEndDocument());
      writer.flush();
      }

      produces the below output
      <?xml version='1.0' encoding='UTF-8'?><test>test special characters &amp; and ä </test>

      If I try escaping with apache StringEscapeUtils.escapeXml(), StAX XMLEventWriter double escapes the apache escaped &amp;#228 to &amp;amp;#228;
      <?xml version='1.0' encoding='UTF-8'?><test>test special characters &amp;amp;amp; and &amp;amp;#228; </test>

      I also tried disabling the default escaping of XMLEventWriter by setting the property javax.xml.stream.isEscapingCharacters; but setting this property has no effect.
      outputFactory .setProperty("javax.xml.stream.isEscapingCharacters", Boolean.FALSE);

      Please can anyone help me.

      Edited by: user8870888 on Feb 1, 2013 9:57 AM

      Edited by: user8870888 on Feb 1, 2013 9:59 AM