Parsing xml: attributes.getValue() returns null?

ⅰ亾dé卋堺 提交于 2019-12-25 12:44:45

问题


See this part of my xml:

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

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"));
        }
    }
}

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:

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)

...(sorry for formatting)

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

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


回答1:


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.

EDIT: The problem is probably that

attributes.getValue("rows")

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.

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

EDIT2: 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));
}

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.



来源:https://stackoverflow.com/questions/8450501/parsing-xml-attributes-getvalue-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!