[英]How to read XML String with Java DOM Parser ? Poll, results
我嘗試從此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
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
}
}
}
試試這個,
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.