Joseph
Joseph

Reputation: 169

XML read attribute and element

I'm trying to read a XML similar to this:

   <Cells>
          <Cell row="3" column="1">a</Cell>
          <Cell row="5" column="0">4</Cell>
   </Cells>

And i'm using DOM Parser.. And i want to read the row, column and the Content ( in this case "a" or "4");

Here is a sample of my code

doc.getDocumentElement().normalize();

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

        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(eElement.getAttribute("row") + "\t"
                        + eElement.getAttribute("column") + "\t"); 
                System.out.println(eElement.getElementsByTagName("Cell").item(0).getTextContent());              
            }
        }

When i run the program it freezes at "eElement.getElementsByTagName("Cell")" and only print the --> 3 1 (the row and the column)

I don't get why it does not print the Element Cell, the "a" and "4" are inside the Cell element right? I cant read XML in that format?

Thank you!

Upvotes: 1

Views: 51

Answers (1)

liuruibin
liuruibin

Reputation: 69

Try this, Change

System.out.println(eElement.getElementsByTagName("Cell").item(0).getTextContent();

TO

System.out.println(eElement.getTextContent());

Upvotes: 1

Related Questions