为什么 qName 有效而 LocalName 无效?
我正在学习 Java SAX API。我使用 php 制作了自己的 XML feed。这是 XML 文档
现在,当我想让我的应用程序输出到控制台时,什么也没有出现。我将问题定位到扩展 DefaultHandler
的 XMLHandler
中的 endElement
方法。这是我的实现。
public void endElement(String uri, String localName, String qName) throws SAXException {
//I added the next three lines for debugging
System.out.println("Found End Element " + count + " times");
System.out.println("Localname = " + localName);
System.out.println("QName = " + qName);
super.endElement(uri, localName, qName);
if (this.currentItem != null){
if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNAME)){
currentItem.setItemName(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMSITE)){
currentItem.setItemSite(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNO)){
currentItem.setItemNo(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEM)){
System.out.println(currentItem);
items.add(currentItem);
}
builder.setLength(0);
}
count++;
}
事实证明, localName
一直为空,因此条件从未成立,代码也从未进入决策块。另一方面,qName
正确显示了所有名称,一旦我将变量更改为 qName
,List
集合类型确实已填满并且工作正常。
我来这里是想问为什么 qName 有效,而 localName 无效?而 IBM DeveloperWorks 的教程使用了 RSS 提要,并且 localName
非常适合他。
PS 这是 IBM 教程使用的提要: http://www.androidster.com/android_news.rss< /a>
I was learning the Java SAX API. I made my own XML feed using php. Here is the XML Document
Now when i wanted to make my application output to the console nothing came up. I pinpointed the problem to the endElement
method in my XMLHandler
that extends DefaultHandler
. Here is my implementation of it.
public void endElement(String uri, String localName, String qName) throws SAXException {
//I added the next three lines for debugging
System.out.println("Found End Element " + count + " times");
System.out.println("Localname = " + localName);
System.out.println("QName = " + qName);
super.endElement(uri, localName, qName);
if (this.currentItem != null){
if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNAME)){
currentItem.setItemName(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMSITE)){
currentItem.setItemSite(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEMNO)){
currentItem.setItemNo(builder.toString());
} else if (localName.equalsIgnoreCase(me.osama.XMLParsing.BaseFeedParser.ITEM)){
System.out.println(currentItem);
items.add(currentItem);
}
builder.setLength(0);
}
count++;
}
Turns out that localName
kept on coming empty hence the conditions never held true and the code never went into the decision block. On the other hand qName
brought all names out properly and once i changed the variable to qName
the List<Item> items
collection type did fill up and worked correctly.
I am here to ask why did qName work and not localName? Whereas the tutorial from IBM's DeveloperWorks used an RSS feed and localName
worked perfectly for him.
P.S. this is the feed the IBM Tutorial used: http://www.androidster.com/android_news.rss
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Java API 的 SAX 命名空间,
也许,如果您向 XML 添加命名空间并在该命名空间中定义元素,它会返回有效的
localName
。文章还提到,通过命名空间处理,某些实现将返回空qName
。As per the SAX namespace for Java API,
Perhaps, if you add a namespace to the XML and define your elements in that namespace, it would return a valid
localName
. The article also mentions that with namespace processing, some implementations will return emptyqName
.