我在使用 SAX 解析自关闭 XML 标签时遇到问题。我正在尝试从 Google Base API 中提取链接标签。我在解析常规标签方面取得了一定的成功。
这是 xml 的一个片段
<entry>
<id>http://www.google.com/base/feeds/snippets/15802191394735287303</id>
<published>2010-04-05T11:00:00.000Z</published>
<updated>2010-04-24T19:00:07.000Z</updated>
<category scheme='http://base.google.com/categories/itemtypes' term='Products'/>
<title type='text'>En-el1 Li-ion Battery+charger For Nikon Digital Camera</title>
<link rel='alternate' type='text/html' href='http://rover.ebay.com/rover/1/711-67261-24966-0/2?ipn=psmain&icep_vectorid=263602&kwid=1&mtid=691&crlp=1_263602&icep_item_id=170468125748&itemid=170468125748'/>
.
.
等等
我可以解析更新和已发布的标签,但不能解析链接和类别标签。
这是我的 startElement 和 endElement 覆盖
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("title") && xmlTags.peek().equals("entry")) {
insideEntryTitle = true;
}
xmlTags.push(qName);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
// If a "title" element is closed, we start a new line, to prepare
// printing the new title.
xmlTags.pop();
if (insideEntryTitle) {
insideEntryTitle = false;
System.out.println();
}
}
xmltags 声明..
private Stack<String> xmlTags = new Stack<String>();
大家有什么帮助吗?
这是我在这里发表的第一篇文章..我希望我遵守发帖规则!非常感谢大家..
更正:endElement
被叫。characters
才不是。
public void characters(char[] ch, int start, int length) throws SAXException
{
if (insideEntryTitle)
{
String url= new String(ch, start, length);
System.out.println("url="+title);
i++;
}
}