【发布时间】:2011-02-12 01:06:56
【问题描述】:
我在使用 SAX 解析自闭合 XML 标记时遇到问题。我正在尝试从 Google Base API 中提取链接标签。我在解析常规标签方面取得了相当大的成功。
这是xml的sn-p
<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++;
}
}
【问题讨论】:
-
您应该确保所有块代码都缩进四个空格(这次我已经为您编辑了)。这也适用于 xml 示例。
-
有什么症状?两个方法都没有被调用,还是只有一个?
-
会牢记这一点!仅调用起始元素。
-
哦顺便说一句..谢谢你的格式化!
-
我无法复制。 endElement 为我调用。添加日志记录。