0

XML:

<root>
    <foz>
        ....
        <row>
            <column>PD1</column>
            <column>PD2</column>
            <column>PD3</column>
        </row>
        ...
    </foz>
</root>

JAVA:

      FileInputStream fin;  
      fin = new FileInputStream(zip); //zip is a File Object
      ZipInputStream zin = new ZipInputStream(fin);
      ZipEntry ze = null;
      SAXBuilder builder = new SAXBuilder();
        Document document = (Document)builder.build(zin);

        Element rootNode = document.getRootElement();
         List list = rootNode.getChildren("foz");

        for ( int i = 0; i < list.size(); i++ ) {
         Element node = (Element) list.get(i);

         List li = node.getChildren("row");       
         for ( int j = 0; j < li.size(); j++ ){
            Element nodePda = (Element) li.get(j);
            String id = nodePda.getChildTextTrim("column");
            ...
         }
        }
      ...

I don't no why the sentence "rootNode.getChildren("foz");" only returns ONE element in list "li", return PD1 but not PD2 and PD3 values. Can anybody help me?

Thanksss in advance!

pktangyue
  • 8,326
  • 9
  • 48
  • 71
user2003559
  • 1
  • 1
  • 1

1 Answers1

1

JDOM appears to be doing the right thing... In your example there is only one child 'foz' of the root node, thus rootnode.getChildren("foz") returns a list of one element.

Then, you take that one foz Element, and (successfully) get all the "row" children. Now, with each 'row' Element you call the getChildText("column") method. This will always find the first child element called 'column' and return its text value, thus, you get the value 'PD1' only. See its documentation here: getChildText(String) which in turn references here: getChild(java.lang.String)

Bottom line is JDOM is doing what you asked it to do.

What you should do is:

  • Upgrade to JDOM 2.x - it will help with the casting of values, and it is faster, more up o date, and better supported.
  • Use another loop inside the row Element and call getChildren("column"), and get the text for each 'column' child Element.

Rolf

rolfl
  • 17,539
  • 7
  • 42
  • 76