Java Sax Parser 只返回一行标记 [英] Java Sax Parser only returning one line of a tag
问题描述
我试图解析 xml 中的描述标签,但它只输出一行:
I am trying to parse the description tag in the xml but it only outputs one line:
description: <img src=http://www.ovations365.com/sites/ovations365.com/images/event/441705771/sparkswebsite_medium.jpg alt="SPARKS: Understanding Energy">
这只是 CDATA 中文本的一小部分,我正在尝试输出多个项目的描述.为什么我无法获取整个 CDATA?
That is only a small part of the text in the CDATA and I'm trying to output the description for multiple items. Why can't I get the whole CDATA?
XML 位于:http://feeds.feedburner.com/Events-Ovations365
package com.example.ovations_proj;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.example.ovations_proj.RssItem;
public class RssParseHandler extends DefaultHandler {
private List<RssItem> rssItems;
// Used to reference item while parsing
private RssItem currentItem;
// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingLink;
private boolean parsingDes;
public RssParseHandler() {
rssItems = new ArrayList<RssItem>();
}
public List<RssItem> getItems() {
return rssItems;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if ("item".equals(qName)) { //item
currentItem = new RssItem();
} else if ("title".equals(qName)) { //title
parsingTitle = true;
} else if ("link".equals(qName)) { //link
parsingLink = true;
}else if ("description".equals(qName) ) { //description
parsingDes = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("End Element :" + qName);
if ("item".equals(qName)) {
rssItems.add(currentItem);//item
currentItem = null;
} else if ("title".equals(qName)) {//title
parsingTitle = false;
} else if ("link".equals(qName)) {//link
parsingLink = false;
} else if ("description".equals(qName) ) { //description
parsingDes = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle) {
if (currentItem != null){
currentItem.setTitle(new String(ch, start, length));
}
} else if (parsingLink) {
if (currentItem != null) {
currentItem.setLink(new String(ch, start, length));
parsingLink = false;
}
} else if (parsingDes) {
if (currentItem != null) {
currentItem.setDes(new String(ch, start, length));
System.out.println("description: " + currentItem.getDes());
parsingDes = false;
}
}
}
}
推荐答案
部分中的字符数据似乎正在发送多个块,即多次调用
characters
方法.
It seems that the character data in the <![CDATA[...]]>
sections is being sent in multiple chunks, i.e. in multiple calls to the characters
method.
characters
方法的 ContentHandler 文档 提到 SAX 解析器可以随意执行此操作:
The ContentHandler documentation for the characters
method mentions that SAX parsers are free to do this:
SAX 解析器可能会在单个块中返回所有连续的字符数据,也可能会将其拆分为多个块[....]
SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks[....]
因此,您需要调整 characters
方法以处理对同一块连续字符数据的多次调用.
You'll therefore need to adjust your characters
method to handle being called multiple times for the same chunk of contiguous character data.
这篇关于Java Sax Parser 只返回一行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!