SAX 解析器不解析特殊字符“&”
我正在使用 SAX 解析器来解析 xml,如下所示:
<items>
<item>
<id>1000</id>
<title>Rock Music</title>
</item>
<item>
<id>1011</id>
<title>R&B Music</title>
</item>
......................
当我解析时,我只能得到:
1000 Rock Music
1011
,但不能得到包含特殊字符的文本,之后什么也没有......它就停在那里。我对空格没意见,问题只是特殊字符。 我尝试过:
InputSource is = new InputSource(sourceUrl.openStream());
is.setEncoding("ISO-8859-1"); //also "UTF-8" and other encoding but didn't work.
如何提取并显示包含“&”的文本假设我无权编辑 xml?(我听说如果你在编写 xml 时编写 &
,它就可以正常工作!?)
I'm using SAX Parser to parse an xml like this one:
<items>
<item>
<id>1000</id>
<title>Rock Music</title>
</item>
<item>
<id>1011</id>
<title>R&B Music</title>
</item>
......................
When I parse I can get just:
1000 Rock Music
1011
but not text that contains special character and nothing after...it stops there. I'm all right with blank spaces, the problem is just with special char.
I have tried :
InputSource is = new InputSource(sourceUrl.openStream());
is.setEncoding("ISO-8859-1"); //also "UTF-8" and other encoding but didn't work.
How can I extract and display text that contains "&" supposing that I have no access editing the xml?, (I heard that if you write &
when you write the xml it works fine !?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将输入包装在
FilterInputStream
之类的内容中 并在 SAX 看到文本之前自行HTML 编码文本。但是,实际上,您应该与发送此数据的人交谈,并让他们修复损坏的 XML。
You can try to wrap the input in something like a
FilterInputStream
and HTML-encode the text yourself before SAX sees it.But, really, you should speak to whoever is sending this data, and get them to fix the broken XML.