4

I am writing a REST based webservice code, in which an xml is read as POST body content. <REQ>J*Wt!&Gs+aGphxR</REQ>

The data may contain alphanumeric or symbols.

I am using json-jena jar and converting the xml to java object and reading the values.

JSONObject jsonObject = XML.toJSONObject(body);

For the specific request above I am getting error as below org.json.JSONException: Missing ';' in XML entity: &gs at 40 [character 41 line 1]

Please let me know how can i convert to json object without error.

My requirement is to read the values between the REQ tag with the actual values specified by the users (not the encoded string).

3
  • Do you have to escape the "<" and ">"?? Commented Oct 9, 2012 at 0:56
  • my request will come properly with < and > converted to its equivalent. Commented Oct 9, 2012 at 1:03
  • @user1332962 - the issue here is not < and > but escaping the & on its own Commented Oct 9, 2012 at 8:22

3 Answers 3

4

The issue is that the supplier is not sending XML. In XML an & starts a entity which must be ended with a ;

The correct XML for your example is either <REQ>J*Wt!&amp;Gs+aGphxR</REQ> by replacing & by its entity reference &amp; (XML also include &lt; and &gt; for < and >) or possibly enclose the data in a CDATA section <REQ><![CDATA[J*Wt!&Gs+aGphxR\]]></REQ>

Sign up to request clarification or add additional context in comments.

3 Comments

The &Gs in the value is not the > symbol. It is considering as > symbol and giving error telling ; is missing. I need solution to escape this error and continue the xml to json formatting.
Missing a bracket at beginning, middle, and end: <![CDATA[ ... ]]>
@user1332962 - You can't escape the error as the input is not XML as the & needed to be followed by a ; to end the entity, so you need to escape the & on its own irrespective of wht the following letter is.
1

This appears to be complaining about XML well-formedness rather than any errors in making the conversion itself (though it is probably reading the XML as the first step toward conversion which is why you're seeing an error).

How are you producing the content in the first place? You should make sure that is itself well-formed. XML elements like your <REQ> cannot just have arbitrary text. The characters & and < must be escaped within element content (and ' or " may need to be escaped within attribute content). To avoid needing to escape & and < within element content, you could use CDATA sections, assuming your parsers support them, but bear in mind that the specific sequence ]]> will need to be escaped.

Comments

1

This isn't valid XML, because the & character is reserved. To get this to work, you will probably have to escape it like this &amp;. Other symbols that are reserved are ", <, and >. You can escape these like this: &quot;, &lt;, and &gt.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.