So for a project I'm doing in school I'm building an app in android which analyses some video game data from an XML file. the XML file is pretty huge, but here's an extract of the general structure:
<modlist> <affix mod="Base Physical Damage Reduction Rating" modtype="Prefix" signaturemod="false" legacymod="false" category="Armour" twohweaponmod="false" onehweaponmod="false" applicablegear="Belt"> <aliases> <alias> to Armour</alias> </aliases> <tiers> <mod name="Lacquered" level="1" mastercrafted="false" tier="6"> <stats> <stat name="Base Physical Damage Reduction Rating" from="3" to="10" /> </stats> </mod> <!--Then several more <mod> tags with <stats> and <stat> tags in them.--> </tiers> </affix> <!--Then around 140 more <affix> tags.--> So what I'm trying to do at this moment is that I have a string which tells me which mod to look for in the affix tag, and an integer which I want to compare with the 'from' and 'to' attributes in the 'stat' tag which is the last child node.
The problem I'm having in my code is that for some reason it doesn't compare to an element node, but I have a feeling that's not the actual issue, I think I'm doing something wrong. It would be nice if someone could look at the code I wrote and fix it or give me a better solution:
private Tier findTier(NodeList list, String mod, String attribute, int value) { // Search through all affix tags. parameter list is rootelement list. for (int i = 0; i < list.getLength(); i++) { Node nodeOne = list.item(i); // I have a function in the CreateItemActivity class which is basically x..getNodeType() == Node.ELEMENT_NODE if (CreateItemActivity.checkNodeType(nodeOne)) { Element eleOne = (Element) nodeOne; // test = value of attribute 'mod' String test = eleOne.getAttributes().getNamedItem(attribute).getNodeValue(); // if value of the attribute matches the parameter mod (found the correct mod in which to check the stat tag if (test.equals(mod)) { NodeList listOne = nodeOne.getChildNodes(); // Now go through childnodes of this node (aliases and tiers, we only want tiers) for (int k = 0; k < listOne.getLength(); k++) { Node nodeTwo = listOne.item(i); if (CreateItemActivity.checkNodeType(nodeTwo)) { Element eleTwo = (Element) nodeTwo; if (eleTwo.getTagName() == "tiers") { NodeList listTwo = nodeTwo.getChildNodes(); // Now go through all children of tiers (the mod tags) for (int j = 0; j < listTwo.getLength(); j++) { // Here I'm trying to get to the stat tag. Node nodeThree = listTwo.item(i); Node subNode = nodeThree.getFirstChild(); Node stats = subNode.getFirstChild(); String stringFrom = stats.getAttributes().getNamedItem("from").getNodeValue(); int from = Integer.parseInt(stringFrom); String stringTo = stats.getAttributes().getNamedItem("to").getNodeValue(); int to = Integer.parseInt(stringTo); // Compare the numbers to the parameter value if (value > from && value < to) { Node tier = listTwo.item(i); if (CreateItemActivity.checkNodeType(tier)) { String correctTier = tier.getAttributes().getNamedItem("tier").getNodeValue(); String tierName = tier.getAttributes().getNamedItem("name").getNodeValue(); Tier tierInfo = new Tier(Integer.parseInt(tierName), correctTier); return tierInfo; } } } } } } } } } return null; } Now the error I get is that the checkNodeType returns false, so the nodes apparently aren't element nodes, but if I remove the check for element, I get a null pointer exception when trying to get the node.item(i) values.
I've been stuck with this for a while now and I can't seem to find a solution, I would be really grateful for some assistance.
No comments:
Post a Comment