Java: ignoring screens in XML parsing

I use DocumentBuilder to parse XML files. However, the project specification requires that lines of text, such as &quot;and &lt;, be returned literally and not decoded as characters ( "and <).

A previous similar question, Read the escaped quote as a hidden quote from xml , got one answer that seems to be specific to Apache, and another that looks just don't do what it says. I would love to be mistaken in both calculations :)

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 println () to create things like

&quot;3&gt;2&quot;

instead

"3>2"

which is happening now. Thank!

+3
4

, . , & s ( , ). , .

: , , . , , , , , :)

-3

xml-

 StringEscapeUtils.escapeXml(str);

(javadoc, commons-lang)

+2

DocumentBuilder XML . , , &quot; &lt;, (" <).

. .

, , , , .

CDATA - , '<' XML, XML . , XML, '<' , .

+2

, dom4j Node.asXML(). , , node , , - .

+1
source

Source: https://habr.com/ru/post/1740832/


All Articles