我有以下问题:
原创RSS文件的一部分:
<item>
<title> I can get data in tag this </title>
<description><p> i don't get data in this </p></description></item>
当我使用StAX解析器读取文件时,特殊字符'&amp; lt'; 。它会自动转换为'&lt;'。然后我无法在标签“&lt;'description&gt;'
的其余部分获取数据这是我的代码:
public Feed readFeed() {
Feed feed = null;
try {
boolean isFeedHeader = true;
String description = "";
String title = "";
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = read();
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
String localPart = event.asStartElement().getName()
.getLocalPart();
switch (localPart) {
case "title":
title = getCharacterData(event, eventReader);
break;
case "description":
description = getCharacterData(event, eventReader);
break;
}
} else if (event.isEndElement()) {
if (event.asEndElement().getName().getLocalPart() == ("item")) {
FeedMessage message = new FeedMessage();
message.setDescription(description);
message.setTitle(title);
feed.getMessages().add(message);
event = eventReader.nextEvent();
continue;
}
}
}
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;}
private String getCharacterData(XMLEvent event, XMLEventReader eventReader)
throws XMLStreamException {
String result = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
result = event.asCharacters().getData();
}
return result;}
我按照以下说明操作:http://www.vogella.com/tutorials/RSSFeed/article.html
答案 0 :(得分:5)
教程存在缺陷。它没有考虑到你可以为单个文本块获取多个文本事件(当你有嵌入的实体时会发生这种事件)。
为了让您的生活更轻松,请确保在创建XMLEventReader之前在XMLInputFactory上将IS_COALESCING属性设置为true(此属性会强制读者将所有相邻文本事件组合到单个事件中)。 / p>