使用XMLStreamReader读取转义字符 [英] Reading escape characters with XMLStreamReader

查看:107
本文介绍了使用XMLStreamReader读取转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XMLStreamReader .

例如,我有这个元素:

<a>foo&amp;bar</a>

,当我读取该值时,& 之后的所有内容都会被截断,并且我得到的值是"foo"

and when I read the value, everything after the &amp; is truncated, and the value I get is "foo"

有什么想法可以解决吗?

Any ideas how that could be fixed ?

推荐答案

我不确定是什么问题-我的测试产生了您期望的结果.

I'm not sure what the problem is - my test produces the results you expect.

运行

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(
     new StringReader("<tag>foo&amp;bar</tag>"));
PrintWriter pw = new PrintWriter(System.out, true);
while (reader.hasNext())
{
    reader.next();
    pw.print(reader.getEventType());
    if (reader.hasText())
        pw.append(' ').append(reader.getText());
    pw.println();
}

生产

1
4 foo
4 &
4 bar
2
8

在JDK 1.6.0.11上-我知道已经很老了.如果结果不同,我将升级并发回邮件.

On JDK 1.6.0.11 - rather old I know. I'll upgrade and post back if results differ.

要记住的一件事是, XMLStreamReader 可以(并且确实可以!)将字符数据分成几个块,如您在上面看到的-重复的4个事件(4 = CHARACTERS)表示元素的文本作为3个事件发送.

One thing to bear in mind is that the XMLStreamReader can (and does!) break up character data into several blocks, as you see above - the repeated 4 events (4=CHARACTERS) indicates the text of the element is sent as 3 events.

这篇关于使用XMLStreamReader读取转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆