开发者

problem in XML parser when a extra quote

I have written a xml parser which successfully parses a xml file which is given as input.But sometime the input file that is given to may parser has double quote in a text property because of which my parser crashes.

Eg

<tag  myprop=" this has a extra quote here like " some times" >  </tag>

I k开发者_如何学Gonow the tag that may /may not have the extra quote.I use a dom parser.

How can i handle this situation?


You won't be able to use an XML parser until you have actual XML. What you currently have is invalid (ie not XML). You should escape the quote-mark inside the attribute beforehand.

The escaped code would look like:

<tag  myprop=" this has a extra quote here like &quot; some times" >  </tag>

As to why your parser crashes, well there are dozens of XML libraries in existence - have you looked at any of those? I would personally expect to receive a ParseException or something like that.


I don't know for sure, but I think it's just invalid XML and so your parser should fail gracefully (rather than crashing) but I don't think it should successfully parse such a file.


You can not. That is not a valid XML, so the DOM parser will fail to parse.


see XML 1.0 specification, section 2.4:
http://www.w3.org/TR/xml/#attdecls

To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos ; ", and the double-quote character (") as "&quot ;"."

so, since it's not valid XML your parser shouldn't be trying to handle the invalid value, it just needs to give an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜