0
<?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

但我不知道如何解析它?

4

3 回答 3

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

有一个例子

于 2013-10-27T00:06:34.530 回答
0

注意我没有仔细阅读您的问题,并认为您想挑选 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)。

于 2013-10-29T22:42:21.070 回答
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"));
    }
}
于 2013-10-27T00:07:00.347 回答