我在使用SAX解析自我关闭XML标记时遇到问题。我试图从Google Base API中提取链接标记。我在解析常规标记方面取得了合理的成功。使用SAX解析器解析自我关闭XML标记时遇到问题
这里是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++;
}
}
你应该确保所有的块代码都缩进四个空格(我这次为你编辑过)。这也适用于xml示例。 – 2010-04-25 07:48:32
有什么症状?这两种方法都不会被调用,或者只有一种? – 2010-04-25 07:53:08
会记住这一点! 只调用启动元素。 – sandesh 2010-04-25 08:01:51