问题描述
我在使用SAX解析自关闭XML标记时遇到麻烦.我正在尝试从Google Base API中提取链接标记.我在解析常规标记方面取得了一定的成功.
I am having trouble parsing self closing XML tags using SAX. I am trying to extract the link tag from the Google Base API.I am having reasonable success in parsing regular tags.
这是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'/>
.
.
以此类推
我可以解析更新和已发布标签,但不能解析链接和类别标签.
I can parse the updates and published tags, but not the link and category tag.
这是我的startElement和endElement替代
Here are my startElement and endElement overrides
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的声明.
declaration for xmltags..
private Stack<String> xmlTags = new Stack<String>();
有没有帮助的人?
这是我在这里的第一篇文章.我希望我遵守了发布规则!谢谢你们..
this is my first post here.. I hope I have followed posting rules! thanks a ton guys..
更正: endElement
被调用.字符
没有.
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元素标签之间传递内容(以块为单位,每个方法调用一个块).所以如果您有一个像这样的XML元素
What characters
does is deliver the content between the XML element tags (in chunks, one chunk per method call). Soif you have an XML element like
<Foo someattrib="" />
然后不调用 characters
,因为解析器没有内容可告诉您.
then the characters
doesn't get called, because there's no content there for the parser to tell you about.
如果您依赖于字符方法,即使标记为空,也必须在此处调用, 您做错了 .
If you are relying on your characters method having to get called here even if the tag is empty, you are doing it wrong.
characters方法将元素文本添加到缓冲区中,但是startElement和endElement需要负责清除和读取缓冲区,因为endElement是您知道已收到所有元素文本的地方.如果没有要阅读的字符,可以不调用任何字符应该没问题.
The characters method adds element text to a buffer, but startElement and endElement need to be in charge of clearing and reading from the buffer because endElement is the place where you know you’ve received all the element text. It should be ok to have characters not get called if there is nothing to read.
因为您可能还没有用一个字符调用所有内容,所以该方法中一定不能包含任何业务逻辑.如果存在,那么您的代码有时将无法正常工作.
Because you may not have all the content yet in any one characters call there must not be any business logic in that method. If there is then your code won’t work at some point.
有关如何实现字符的信息,请参见此示例.如果您要读取的是属性值,请参见此示例.
For how to implement characters see this example. If what you want to do is read attribute values see this example.
这篇关于使用SAX解析器解析自关闭XML标记时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!