解析XML:attributes.getValue()返回null? [英] Parsing xml: attributes.getValue() returns null?

查看:1890
本文介绍了解析XML:attributes.getValue()返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅我的XML的这一部分:

See this part of my xml:

<blocktype>
    <properties name="normal_blue" type="1"/>
    <resources image="brick_blue"/>
    <properties powerup="0" shield="0" />
</blocktype>

我使用这个code解析它:

I'm using this code to parse it:

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    if (localName.equalsIgnoreCase("level")) {
        block = null;
        inLevel = true; // parsing level
        Log.d("MyContentHandler", "Parsing Level = " + Boolean.toString(inLevel));
    } if (localName.equalsIgnoreCase("blocktype")) {
        bundle = null;
        inBlockType = true; // parsing blocktype
        Log.d("MyContentHandler", "Parsing blocktype = " + Boolean.toString(inBlockType));
    };

    // handling level parsing
    if (inLevel) {
        int n = 0;
        if (localName.equalsIgnoreCase("properties")) {
                int c = Integer.parseInt(attributes.getValue("columns"));
                int r = Integer.parseInt(attributes.getValue("rows"));
                bundle = new XmlLevelBundle(r, c);
                bundle.name = attributes.getValue("name");
            }// bundle might be null?
            if (localName.equalsIgnoreCase("block")) {
                bundle.types[n] = Integer.parseInt(attributes.getValue("type"));
                n++;
            }
    }

    // handling BlockType parsing
    if (inBlockType) {

        if (localName.equalsIgnoreCase("properties")) {
            block.name = attributes.getValue("name");
            block.type = Integer.parseInt(attributes.getValue("type")); //nullpointer?
        } else if (localName.equalsIgnoreCase("resources")) {
            block.resourceName = attributes.getValue("image");
        } else if (localName.equalsIgnoreCase("properties")) {
            block.powerUp = Integer.parseInt(attributes.getValue("powerup"));
            block.shield = Integer.parseInt(attributes.getValue("shield"));
        }
    }
}

现在,一切似乎都在工作的时候带走了第二条语句。正如你可以看到我的XML却有键入属性,它仍然返回null,按照以下logcat的:

Now everything seems to work when I take away the second statement. As you can see my xml actually has the type attribute, still it returns null, according to the following logcat:

18 12-09:53:30.741:WARN / System.err的(851):
  java.lang.NumberFormatException:无法解析'空'作为整数
  12-09 18:53:30.769:WARN / System.err的(851):在
  java.lang.Integer.parseInt(Integer.java:406)12-09 18:53:30.769:
  WARN / System.err的(851):在
  java.lang.Integer.parseInt(Integer.java:382)12-09 18:53:30.779:
  WARN / System.err的(851):在
  cd.ark.utils.MyContentHandler.startElement(MyContentHandler.java:57)

12-09 18:53:30.741: WARN/System.err(851): java.lang.NumberFormatException: unable to parse 'null' as integer 12-09 18:53:30.769: WARN/System.err(851): at java.lang.Integer.parseInt(Integer.java:406) 12-09 18:53:30.769: WARN/System.err(851): at java.lang.Integer.parseInt(Integer.java:382) 12-09 18:53:30.779: WARN/System.err(851): at cd.ark.utils.MyContentHandler.startElement(MyContentHandler.java:57)

...(抱歉格式)

所以基本上它说NULL被传递到的Integer.parseInt()。

So basically it says Null is being passing into Integer.parseInt().

所以,任何人都可以帮我这个NullPointerException异常?
我一直停留在这个很长一段时间。
谢谢!

So could anyone help me with this NullPointerException? I've been stuck on this for a long time. Thanks!

推荐答案

由于标签之一是SAXParser的,我假定这就是你使用的是什么。在这种情况下,方法调用似乎正确的,留下问题是属性未正确使用你的XML包含的值初始化。请加code来告诉你如何生成属性

Since one of the tags is "saxparser", I'm assuming that's what you're using. In that case, the method calls seem correct, leaving the problem to be that attributes isn't correctly initialized with the values that your XML contains. Please add code to show how you generate attributes.

编辑:的问题是可能是

attributes.getValue("rows")

因为你没有使用返回null 限定的名称在XML;看到在SAX库的文档的 Attributes.getValue(字符串)。如果你使用 Attributes.getValue(字符串)和合格的名字都没有用,它会返回

returns null since you aren't using qualified names in your XML; see the documentation on the SAX library, specifically the documentation of Attributes.getValue(String). If you use Attributes.getValue(String) and qualified names aren't available, it will return null.

合格的名字上所以有用的,但简短的解释可以发现<一个href=\"http://stackoverflow.com/questions/6429634/differences-between-a-local-name-and-qualified-name-in-a-xml-attribute\">here.

A useful, but brief explanation of qualified names on SO can be found here.

EDIT2:我不知道如何正确调节XML(从未使用XML我自己),但你没有使用命名空间,你也许可以做你想做的通过使用 Attributes.getValue(INT)因为这不会对属性的名称,但它拥有的属性列表上工作。你可能需要弄清楚虽然属性的顺序;你可以这样明白这一点:

I don't know how to properly adjust the XML (never used XML myself), but you don't have to use namespaces, you might be able to do what you want by using Attributes.getValue(int) since that doesn't work on the names of the attributes but on the list of attributes it holds. You probably need to figure out the order of the attributes though; you could figure that out this way:

for(int i = 0; i < attributes.getLength(); i++) {
    System.out.println(attributes.getValue(i));
}

希望帮助;也许你可以找到关于所以在XML命名空间有帮助的;否则,如果你的程序需要使用解决他们的名字的属性,你可能必须了解XML命名空间。

Hope that helps; maybe you can find something helpful on namespaces in XML on SO; otherwise if your program requires the use of addressing attributes by their name, you probably will have to learn about XML namespaces.

这篇关于解析XML:attributes.getValue()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆