Proper way to store an url in xml?

2019-05-23 04:19发布

问题:

I am storing data in xml file, In one of the node I have to store an url which consists of special character like & I used &amp instead of & and xml shows no error but when I did SAX parsing String value returned within the node is the string which is after &,

I am guessing the way I am storing the url is not proper.

What is right way of storing an url in xml.

Currently I am storing as,

<param>http://www.example.com?param1=abc&amp;v=1</param>

XML has no errors, but SAX parser won't return the whole url.

EDIT: I have semicolon after &amp in the url, I missed it the first time.

Solution: It was not the problem with XML , but the way I handled XML in XMLHandler,
I was using new String(ch,start,length);
Now changed to stringBuilder();

回答1:

Your XML is correct (now that you have added the semi-colon), so your use of the SAX API must be wrong.

Maybe see the following answer: Sax parsing and encoding



回答2:

You need a semicolon (;) after the &amp:

<param>http://www.example.com?param1=abc&amp;v=1</param>

EDIT: Okay, now that this has been clarified a bit, I think the issue is that SAX doesn't have to return the entire string in a single characters() call. See this question for more information on how to resolve this.



回答3:

if ampv is param then:

<param>http://www.example.com?param1=abc&amp;ampv=1</param>

else if v is param:

<param>http://www.example.com?param1=abc&amp;v=1</param>


标签: xml sax