Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters in XML response received from server

Tags:

java

scala

In my Scala code, I am fetching a response from a server using the getInputStream method of HttpUrlConnection class. The response is XML data. However the data contains HTML entities like & and '.

Is there a way I can replace these characters with their text equivalent so that I can parse the XML properly?

like image 788
james Avatar asked May 16 '11 13:05

james


People also ask

Does XML accept special characters?

When you use wizards to customize any string in your XML file, you can use the following special symbols: <, >, &, ', ". You can also use these symbols when you are editing a query in Expert Mode or when you are manually entering SQL code into XML files between CDATA tags.

How do I find special characters in XML?

Open an XML document in the text editing mode, right click inside it and there is a new menu item "Determine Complex Layout Chars".

What are the illegal characters in XML?

The only illegal characters are & , < and > (as well as " or ' in attributes, depending on which character is used to delimit the attribute value: attr="must use &quot; here, ' is allowed" and attr='must use &apos; here, " is allowed' ). They're escaped using XML entities, in this case you want &amp; for & .

What is &GT in XML?

The ampersand character (&) starts entity markup (the first character of a character entity reference). &gt; The greater-than character (>) ends a start-tag or an end-tag.


1 Answers

It's necessary to encode those entities in xml so they don't interfere with its syntax. The &lt;(<) and &gt; (>) entities make this more obvious. It would be impossible to parse XML whose content was littered with < and > symbols.

Scala's scala.xml package should give you the tools you need to parse your xml. Here's some guidance from the library's author.

like image 75
BennyFlint Avatar answered Nov 10 '22 03:11

BennyFlint