Java: Ignoring escapes when parsing XML

Posted by Personman on Stack Overflow See other posts from Stack Overflow or by Personman
Published on 2010-04-12T21:07:26Z Indexed on 2010/04/12 21:13 UTC
Read the original article Hit count: 268

Filed under:
|
|

I'm using a DocumentBuilder to parse XML files. However, the specification for the project requires that within text nodes, strings like " and < be returned literally, and not turned into the corresponding ASCII values.

A previous similar question, http://stackoverflow.com/questions/1979785/read-escaped-quote-as-escaped-quote-from-xml, received one answer that seems to be specific to Apache, and another that appears to simply not not do what it says it does. I'd love to be proven wrong on either count, however :)

For reference, here is some code:

  file = new File(fileName);
  DocBderFac = DocumentBuilderFactory.newInstance();
  DocBder = DocBderFac.newDocumentBuilder();
  doc = DocBder.parse(file);

  NodeList textElmntLst = doc.getElementsByTagName(text);
  Element textElmnt = (Element) textElmntLst.item(0);

  NodeList txts = textElmnt.getChildNodes(); 
  String txt = ((Node) txts.item(0)).getNodeValue();
  System.out.println(txt);

I would like that println() to produce things like

&quot;3&gt;2&quot;

instead of

"3>2"

which is what currently happens. Thanks!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about java