我该如何解析这个xml代码?我想获得球速值

时间:2013-10-26 23:53:36

标签: java xml xml-parsing

<?xml version="1.0" encoding="ISO-8859-1"?>
<board>
    <ball xVelocity="-2.1" yVelocity="-3.5" />
    <cezmi1 x="1" score="2" /> 
    <gizmos>
        <squareBumper   x="0"  y="2" />
        <squareBumper   x="3"  y="3" />
        <squareBumper  x="5"  y="5" />
        <squareBumper   x="7" y="7" />
        <squareBumper   x="12"  y="12" />   
        <squareBumper   x="7"   y="12" />
        <squareBumper   x="18"  y="10" />
    </gizmos> 
</board>

我想在我的Example.java中使用像xVelocity和yVelocity这样的速度值

但我不知道我该如何解析呢?

3 个答案:

答案 0 :(得分:0)

你可以这样解析它。

File xmlFile = new File("your.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile); 
NodeList nodes = doc.getElementsByTagName("ball");
for (int i= 0; i< nodes.getLength(); i++) { 
    Node node = nodes.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
       Element eElement = (Element)node ;    
       System.out.println("xVelocity : " + eElement.getAttribute("xVelocity"));
       System.out.println("yVelocity: " + eElement.getAttribute("yVelocity"));
    }
} 

this有一个例子。

答案 1 :(得分:0)

这将读取多个球标记并获取xVelocity和yVelocity

File xmlFile = new File("/path/file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);

doc.getDocumentElement().normalize();

NodeList nList = doc.getElementsByTagName("ball");

for (int temp = 0; temp < nList.getLength(); temp++) {

    Node nNode = nList.item(temp);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

        Element eElement = (Element) nNode;

        System.out.println("xVelocity : " + eElement.getAttribute("xVelocity"));
        System.out.println("yVelocity : " + eElement.getAttribute("yVelocity"));
    }
}

答案 2 :(得分:0)

N.B。我没有仔细阅读你的问题,并认为你想要挑选x和y属性。但是下面的例子展示了如何提取你的任何元素或属性,包括你想要的元素或属性,所以我决定按原样容忍这个例子; XPath非常灵活。

您可以使用利用XPath的JDOM:

import java.util.Iterator;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

public static void main(String[] args) {
  try {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build("xml/board.xml");

    // search for x attribute
    XPathFactory xpfac = XPathFactory.instance();
    XPathExpression<Attribute> xp = xpfac.compile("//squareBumper/@x", Filters.attribute());
    List<Attribute> results = xp.evaluate(doc);

    Iterator<Attribute> iterator = results.iterator();

    // retreive all x attributes (y is similar)
    while (iterator.hasNext()) {
      Attribute a = iterator.next();
      System.out.println(a.getIntValue());
    }
  }
  catch (Exception e) {
    e.printStackTrace();
  }
}

XPath简化了解析(有关XPath的说明,请参阅w3schools)。