XML parsing with SAX: how to handle html as text within xml-tags

醉酒当歌 提交于 2019-12-11 07:16:41

问题


I get an xml response from an external server.

Using some tutorials I got SAX-Parser working.

There is a small problem still remaining.

Within the response there is e.g. description tag containing html like this:

<description><p><strong>Title</strong></p>Description</description> 

After parsing description field of my object contains only "<".

Is it possible to tell my parser to handle html as plain text?

Or maybe there are other possibilities to solve this problem.

Thank you.


回答1:


since you don't include your code, i have to imagine what you wrote. a common bug in SAX handler implementations is not handling the fact that the element text may be returned in multiple characters() method calls. you need to aggregate them all together until you get the endElement() event.




回答2:


Aside from solution SAX problem, you might consider using Stax (javax.xml.stream) solution instead: it is as performance as SAX, but oftentime bit more convenient. You can also force coalescing of textual content (XMLInputFactory.IS_COALESCING) to avoid problems like you are encountering with SAX.



来源:https://stackoverflow.com/questions/8027253/xml-parsing-with-sax-how-to-handle-html-as-text-within-xml-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!