0

A third party system is sending an XML file having '&' character in data. They are not even using CDATA. (Poorly designed system it is).

In Java, how to read that XML file and escape the & and other special characters (<, >, ", ')?

I know this question has been asked many times, but here we don't have any control over the third party system. So how we can read that "invalid" XML file and make it a valid one?

** I'm not able to use SAX/DOM parser as it considers the input file an invalid one.

6
  • How are you loading the XML into Java? What is currently failing? You could use the commons-lang XML Escape utils but it depends what you're doing currently. Some example code would be good! Edit: XML 1.0 or 1.1 specification? May 7, 2015 at 13:20
  • Loading the file as InputStream, but whenever trying to parse it, its throwing an exception as it does not consider the input file a valid XML.
    – Abhishek
    May 7, 2015 at 13:23
  • If you can read the XML in as a string you can escape it using string functions. You can then parse the String back into XML with the escaped characters. Your supplier needs to update their system as it's currently not valid XML, at least tell them this too. Check out commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/… May 7, 2015 at 13:24
  • @DanielTung, thanks for your input but I have checked this. How to apply the function? Suppose, the input XML text is <sometext> Here is some "Text" that I'd like to be "escaped" for XML & here is some Swedish: Tack. Vars?god. </sometext> After applying the StringEscapeUtils it would become,&lt;sometext&gt; Here is some &quot;Text&quot; that I&apos;d like to be &quot;escaped&quot; for XML &amp; here is some Swedish: Tack. Vars&#229;god. &lt;/sometext&gt; It will destroy all the XML tags.
    – Abhishek
    May 7, 2015 at 13:26
  • If you're reading the file line by line you could use regex? Get the string between the first > and the last < and convert that, then rewrite the string back to be <sometext>Here is some &quot;Text&quot; that I&apos;d like to be &quot;escaped&quot; for XML &amp; here is some Swedish: Tack. Vars&#229;god.</sometext> May 7, 2015 at 13:29

1 Answer 1

0

Treat is as XPL. XPL is structured exactly like XML but allows the "special characters" in text fields. The XPL to XML conversion utilities will do exactly what you need. http://hll.nu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.