0

I am storing XML file in DB as string format, before storing in DB i am making sure that the document is well formed and converting all special characters <,>,&,",' to &lt; &gt; etc.,

when i get the string data back form DB and converting the String to XML document using DOM Parser, but when i convert these encoded special characters (&lt; &gt; etc.) are being converted back to actual characters (<,>).

But I don't want this automatic decoding that making by DOM parser, i just want the same encoded characters as it is.

How do i avoid this decoding happening form DOM parser

0m3r
  • 12,286
  • 15
  • 35
  • 71
JAVA IJNA
  • 9
  • 2

1 Answers1

0

Have you tried escaping the entities, e.g. &lt;, in the XML returned from the database before parsing it? For example:

Document document = documentBuilder.parse(new InputSource(new StringReader(xml.replaceAll("&", "&amp;"))));
ck1
  • 5,243
  • 1
  • 21
  • 25
  • The String is having all the special characters in the form on encoded form only, < is in < and > in > and & is in &, then moment when i try to convert string to Document then these encoded characters being converted to actual characters like < is converted to – JAVA IJNA Jun 11 '16 at 22:00
  • It's working for me. Are you able to show an example of your code that's not working right? E.g., `"Some text with entities: < >".replaceAll("&", "&")` – ck1 Jun 11 '16 at 22:07
  • For me to help you further, please provide a code example demonstrating the problem. – ck1 Jun 11 '16 at 22:14
  • This is my Example calss – JAVA IJNA Jun 11 '16 at 23:59
  • { Document doc = null; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; db = dbf.newDocumentBuilder(); doc = db.parse(new InputSource(new StringReader(XMLdata))); if (doc != null) { NodeList nodeList = doc.getElementsByTagName("Inputs"); for(int x=0,size= nodeList.getLength(); x – JAVA IJNA Jun 12 '16 at 00:03
  • This is the output:::A line must be active on your account for at least 30 days before it can be transferred to a new account.You have days remaining before you can transfer. – JAVA IJNA Jun 12 '16 at 00:03
  • Here if you see in output the decoding happened already i don't want that to be happening, i am expecting "A line must be active on your account for at least 30 days before it can be transferred to a new account.You have <?ufd 'Test'?> days remaining before you can transfer" without decoding by DOM parser – JAVA IJNA Jun 12 '16 at 00:06
  • Sorry the comment is not allowing me more characters so added in separate comments please help – JAVA IJNA Jun 12 '16 at 00:07
  • I have created another question that will have all the info. this is the link::: http://stackoverflow.com/questions/37769619/disable-automatic-decoding-by-xml-dom-parser-in-java – JAVA IJNA Jun 12 '16 at 00:17