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.

1
  • I looked at it, and for what I'm doing JAXB might be best. I have to work on another project right now, and I'm not sure if I should mark this question as answered until I can get back to it... Commented Dec 3, 2012 at 2:10

5 Answers 5

2

You could use JAXB for this. It is very easy to create model objects for these map.

See for example http://www.mkyong.com/java/jaxb-hello-world-example/ on how to use this library.

Sign up to request clarification or add additional context in comments.

1 Comment

This did the trick. I was able to create the different layers by having different classes with XmlRootElement, each one being an object inside the other.
2

I would suggest using JAXB (Java Architecture for XML Binding), as it is easy to use and also can be used to validate against XSD (if required).

Java Beans <----> XML

Here is a sample for giving you the feel of it:

Source: 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;
  }

}

Comments

1

1) My personal preference is DOM. SAX is usually my second choice (if performance becomes an issue).

2) There are many (many, MANY!) good tutorials on Java and XML, including:

3) Here's a good discussion about how to use Java and XPath to get the node(s) you're looking for:

2 Comments

The mkyong tutorial has the same problem that I mentioned above. While great for a simpler XML, it doesn't show how to overcome the issue I'm having.
That's what XPath is for! Do read the Developerworks link. And don't give up on DOM. IMHO
1

How about you do something like this:

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

And then iterate over grids, and for each one, invoke getElementsByTagName("square")?

That would solve your problem:

NodeList grids = doc.getElementsByTagName("grid");
for (int i=0; i < grids.getLength(); ++i) {
    Element grid = (Element) grids.item(i);
    NodeList squares = grid.getElementsByTagName("square");
    ...
}

3 Comments

I can iterate over the grids, but each item in there is a node itself, and you cant call getElementsByTagName on a node
Each item there is a Node instance. However, Element extends Node. You can cast grids.item(i) to Element. Element has a getElementsByTagName method.
Updated my answer, to demonstrate how.
0

You may want to find the grids first as :

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

Then to get the first square for first grid node, try:

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

*____________________________________________

But better if you may want to use some XML frameworks e.g. XStream which has very simple API to convert XML into JAVA objects. Using this, you may want to convert both XML files in JAVA and use JAVA comparison.

Simple steps would be:

  1. Define simple Java object (POJO) e.g. MyXMLObj to map the XML attributes in Java attributes.
  2. Convert your XMLs in Java as :

    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. Simply Traverse your java object and get the desired attribute value as:

    myXMLObj.getGrids().get(0).getSquare();//returns square from first gird node
    

1 Comment

@Dr.Cyanide: I updated my answer with node traversal as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.