0

I tried to parse data XML String from this API https://www.boardgamegeek.com/xmlapi/boardgame/13/catan for 2 days without success. I would like to get all language dependences with names and values.

Understanding this XML by the word poll and results is troublesome. I share some my code ...

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class DomParserDemo {

    public static void main(String[] args) {

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader("<poll name=\"language_dependence\" title=\"Language Dependence\" totalvotes=\"170\"><results><result level=\"1\" value=\"No necessary in-game text\" numvotes=\"168\"/><result level=\"2\" value=\"Some necessary text - easily memorized or small crib sheet\" numvotes=\"0\"/><result level=\"3\" value=\"Moderate in-game text - needs crib sheet or paste ups\" numvotes=\"0\"/><result level=\"4\" value=\"Extensive use of text - massive conversion needed to be playable\" numvotes=\"0\"/><result level=\"5\" value=\"Unplayable in another language\" numvotes=\"2\"/></results></poll>"));
            Document doc = dbBuilder.parse(is);

            doc.getDocumentElement().normalize();
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("language_dependence");
            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;
                    System.out.println("language dependence : "
                            + eElement.getAttribute("level"));
                    System.out.println("value: "
                            + eElement
                            .getElementsByTagName("value")
                            .item(0)
                            .getTextContent());
                    System.out.println("numvotes: "
                            + eElement
                            .getElementsByTagName("numvotes")
                            .item(0)
                            .getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

I only have the output like:

Root element :poll
----------------------------

Process finished with exit code 0
  • 1
    What do you mean with "without success"? Do you get an error? If yes, then what is the exact error message? Does the program not do what you expected? What exactly did you expect, and what does it actually do - and how do these differ? – Jesper Jul 16 '19 at 07:42
  • @Jesper without success - is working, but the program didn't do what I expected. I expect to write down all language dependences with names and values from XML. – popMichael Jul 16 '19 at 08:04

2 Answers2

0

language_dependence is not a node under poll, it is a value for an attribute so you need to extract it differently. Here is an example

doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList list = doc.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node n = list.item(i);
    NamedNodeMap map = n.getAttributes();
    Node name = map.getNamedItem("name");
    if (name.getNodeValue().equals("language_dependence")) {
        NodeList children = n.getChildNodes();           
        for (int j = 0; j < children.getLength(); j++) {
            Node child = children.item(j); //<results>
            NodeList resultList = child.getChildNodes(); //list of <result> 
            //and so on
        }
    }
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Hi @Joakim Danielson, thanks for solution ! But, could you show me how to do stuff with node? Still I do it wrong and I have null values in console – popMichael Jul 16 '19 at 12:26
  • @popMichael you continue to drill down the same way, see my updated answer. Or look at the other answer, it seems more efficient. – Joakim Danielson Jul 16 '19 at 15:11
0

try with this,

NodeList nodeList = doc.getElementsByTagName("result");

    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        System.out.println("Level = "+element.getAttribute("level")+", "+
                           "Value = "+element.getAttribute("value")+", "+
                           "NumVotes = "+element.getAttribute("numvotes"));
    }

output,

Level = 1, Value = No necessary in-game text, NumVotes = 168
Level = 2, Value = Some necessary text - easily memorized or small crib sheet, NumVotes = 0
Level = 3, Value = Moderate in-game text - needs crib sheet or paste ups, NumVotes = 0
Level = 4, Value = Extensive use of text - massive conversion needed to be playable, NumVotes = 0
Level = 5, Value = Unplayable in another language, NumVotes = 2
Lakshan
  • 1,404
  • 3
  • 10
  • 23