kXML (XmlPullParser) 未命中 END_TAG

发布于 2024-10-08 23:01:46 字数 2089 浏览 3 评论 0原文

我正在尝试找出一种方法来重写我的一些 XML 解析代码。我目前正在使用 kXML2,这是我的代码 -

    byte[] xmlByteArray;
    try {
        xmlByteArray = inputByteArray;
        ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
        InputStreamReader xmlReader = new InputStreamReader(xmlStream);
        KXmlParser parser = new KXmlParser();
        parser.setInput(xmlReader);
        parser.nextTag();

        while(true)
        {
            int eventType = parser.next();
            String tag = parser.getName();
            if(eventType == XmlPullParser.START_TAG)
            {
                System.out.println("****************** STARTING TAG "+tag+"******************");
                if(tag == null || tag.equalsIgnoreCase(""))
                {
                    continue;
                }
                else if(tag.equalsIgnoreCase("Category"))
                {
                    // Gets the name of the category.
                    String attribValue = parser.getAttributeValue(0);
                }
            }
            if(eventType == XmlPullParser.END_TAG)
            {
                System.out.println("****************** ENDING TAG "+tag+"******************");
            }
            else if(eventType == XmlPullParser.END_DOCUMENT)
            {
                break;
            }
    }
    catch(Exception ex)
    {

    }

我的输入 XML 如下 -

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns="">
<Category name="xyz">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="abc">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="def">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>   

我的问题是,我希望它在遇到结束 xml 标记时命中 XmlPullParser.END_TAG。它确实命中了 XmlPullParser.START_TAG,但它似乎只是跳过/忽略了所有 END_TAG

这是它应该如何工作的吗?或者我错过了什么?

非常感谢任何帮助,
特贾。

I'm trying to figure out a way to rewrite some of my XML parsing code. I'm currently working with kXML2 and here's my code -

    byte[] xmlByteArray;
    try {
        xmlByteArray = inputByteArray;
        ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
        InputStreamReader xmlReader = new InputStreamReader(xmlStream);
        KXmlParser parser = new KXmlParser();
        parser.setInput(xmlReader);
        parser.nextTag();

        while(true)
        {
            int eventType = parser.next();
            String tag = parser.getName();
            if(eventType == XmlPullParser.START_TAG)
            {
                System.out.println("****************** STARTING TAG "+tag+"******************");
                if(tag == null || tag.equalsIgnoreCase(""))
                {
                    continue;
                }
                else if(tag.equalsIgnoreCase("Category"))
                {
                    // Gets the name of the category.
                    String attribValue = parser.getAttributeValue(0);
                }
            }
            if(eventType == XmlPullParser.END_TAG)
            {
                System.out.println("****************** ENDING TAG "+tag+"******************");
            }
            else if(eventType == XmlPullParser.END_DOCUMENT)
            {
                break;
            }
    }
    catch(Exception ex)
    {

    }

My input XML is as follows -

<root xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns="">
<Category name="xyz">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="abc">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>
<Category name="def">
      <elmt1>value1</elmt1>
      <elmt2>value2</elmt2>
</Category>   

My problem briefly is, I'm expecting it to hit XmlPullParser.END_TAG when it encounters a closing xml tag. It does hit the XmlPullParser.START_TAG but it just seems to skip / ignore all the END_TAGs.

Is this how is it's supposed to work? Or am I missing something?

Any help is much appreciated,
Teja.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ι不睡觉的鱼゛ 2024-10-15 23:01:46

好吧,不确定这是否是您的确切问题,但您错过了

</root>

您是否尝试过在开始标记和内容之后打印出什么 eventType 类型?

Well not sure if this is your exact problem but you are missing a

</root>

Have you tried printing out what eventType type is after start tag and after content?

々眼睛长脚气 2024-10-15 23:01:46

哎呀,我很抱歉,一如既往,事实证明编写一个包罗万象的块是一个坏主意。我没有注意到我没有在此处发布的一段代码存在异常,该代码应该进入 XmlPullParser。END_TAG。删除它并像魅力一样工作:)

Oops, I'm sorry, as always, it turned out to be a bad idea to write a catch-all block. I didn't notice that there were exceptions in a piece of code that I didn't post here, which is supposed to go into XmlPullParser.END_TAG. Removed it and works like charm :)

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