0

I've looked at a few examples, but all of them seem to use simpler XML files than the one I'm trying to read.

Here's an example of the layout I'm trying to read:

<map>
    <map_values></map_values>

    <grid>
        <grid_values></grid_values>

        <square>
            <values></values>
        </square>

        <square>
            <values></values>
        </square>

        <square>
            <values></values>
        </square>
    </grid>

    <grid>
        <grid_values></grid_values>

        <square>
            <values></values>
        </square>

        <square>
            <values></values>
        </square>

        <square>
            <values></values>
        </square>
    </grid>
</map>

Currently I'm using DOM to try and read this in, but I'm not sure how to read in one grid's section of squares at a time. Right now I'm using the following:

NodeList squares = doc.getElementsByTagName("square");

but it's returning all the squares in both grids.

I'm OK switching out of DOM, I just need a suggestion for what to switch to and where to find a good tutorial.

4

5 回答 5

2

您可以为此使用 JAXB。为这些地图创建模型对象非常容易。

参见例如 http://www.mkyong.com/java/jaxb-hello-world-example/ 如何使用这个库。

于 2012-12-01T23:39:05.027 回答
2

我建议使用JAXB (Java Architecture for XML Binding),因为它易于使用,也可用于针对 XSD 进行验证(如果需要)。

Java Beans <----> XML

这是一个示例,可让您感受一下:

资料来源:JAXB Tutorail Basic

@XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {

  private String name;
  private String author;
  private String publisher;
  private String isbn;

  // If you like the variable name, e.g. "name", you can easily change this
  // name for your XML-Output:
  @XmlElement(name = "title")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public String getPublisher() {
    return publisher;
  }

  public void setPublisher(String publisher) {
    this.publisher = publisher;
  }

  public String getIsbn() {
    return isbn;
  }

  public void setIsbn(String isbn) {
    this.isbn = isbn;
  }

}
于 2012-12-02T09:24:35.750 回答
1

你如何做这样的事情:

NodeList grids = doc.getElementsByTagName("grid");

然后迭代grids,对于每一个,调用getElementsByTagName("square")?

那将解决您的问题:

NodeList grids = doc.getElementsByTagName("grid");
for (int i=0; i < grids.getLength(); ++i) {
    Element grid = (Element) grids.item(i);
    NodeList squares = grid.getElementsByTagName("square");
    ...
}
于 2012-12-01T23:37:29.277 回答
1

1)我个人的偏好是DOM。SAX 通常是我的第二选择(如果性能成为问题)。

2) 有很多(很多,很多!)关于 Java 和 XML 的好教程,包括:

3) 这是关于如何使用 Java 和 XPath 来获取您正在寻找的节点的一个很好的讨论:

于 2012-12-01T23:46:58.760 回答
0

您可能希望首先找到网格:

  NodeList grids = doc.getElementsByTagName("grid");

然后要获得第square一个grid节点的第一个,请尝试:

  Node gridNode = grids.item(0);
  Node squareNode = gridNode.getChildNodes().item(2); //as it is second child node

* _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ _ __ _ ____

但如果您可能想使用一些 XML 框架,例如XStream,它具有非常简单的 API 可以转换XMLJAVA对象,则更好。使用它,您可能希望在 JAVA 中转换两个 XML 文件并使用 JAVA 比较。

简单的步骤是:

  1. 定义简单 Java 对象 (POJO),例如MyXMLObj将 XML 属性映射到 Java 属性中。
  2. 将 Java 中的 XML 转换为:

    XStream xstream = new XStream();
    //or
    XStream xstream = new XStream(new DomDriver());
    //or
    //XStream xstream = new XStream(new StaxDriver());
    
     MyXMLObj myXMLObj= (MyXMLObj)xstream.fromXML(xml1);
    
  3. 只需遍历您的 java 对象并获得所需的属性值:

    myXMLObj.getGrids().get(0).getSquare();//returns square from first gird node
    
于 2012-12-02T00:01:26.567 回答