为什么 qName 有效而 LocalName 无效?

发布于 2024-12-20 05:14:56 字数 1888 浏览 1 评论 0原文

我正在学习 Java SAX API。我使用 php 制作了自己的 XML feed。这是 XML 文档

在此处输入图像描述

现在,当我想让我的应用程序输出到控制台时,什么也没有出现。我将问题定位到扩展 DefaultHandlerXMLHandler 中的 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 正确显示了所有名称,一旦我将变量更改为 qNameList就会显示出来。 items 集合类型确实已填满并且工作正常。

我来这里是想问为什么 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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-12-27 05:14:56

根据 Java API 的 SAX 命名空间

默认情况下,XML 读取器将报告命名空间 URI 和本地名称
对于属于命名空间的每个元素,在开始和
结束处理程序。

也许,如果您向 XML 添加命名空间并在该命名空间中定义元素,它会返回有效的 localName。文章还提到,通过命名空间处理,某些实现将返回 qName

As per the SAX namespace for Java API,

By default, an XML reader will report a Namespace URI and a localName
for every element that belongs in a namespace, in both the start and
end handler.

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 empty qName.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文