如何使用Java DOM解析器读取XML字符串?投票结果

时间:2019-07-16 07:34:02

标签: java xml jdom domparser readxml

我尝试从该API https://www.boardgamegeek.com/xmlapi/boardgame/13/catan解析数据XML字符串2天,但没有成功。我想获得所有与语言有关的名称和值。

用单词poll和result来理解这个XML很麻烦。 我分享一些代码...

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

我只有这样的输出:

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

Process finished with exit code 0

2 个答案:

答案 0 :(得分:0)

language_dependence不是受轮询的节点,它是属性的值,因此您需要以不同的方式提取它。这是一个例子

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
        }
    }
}

答案 1 :(得分:0)

尝试一下,

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

输出

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