[jdom-interest] Getting two copies of every element

Troy Shahoumian troy at troy.org
Sun Aug 13 22:55:48 PDT 2000


JDOMers--

I just started using JDOM.  In the following code I use getChildren() to
get the children of an element.  To my chagrin, I get two copies of each
element.  The version of JDOM is beta 4.  Help, please!

Code:

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class PrettyPrinter {
   public static void main(String[] args) {
        // Assume filename argument
        String filename = args[0];

        try {
            // Build the document with SAX and Xerces, no validation
            SAXBuilder builder = new SAXBuilder();
            // Create the document
            Document doc = builder.build(new File(filename));
            Element dept = doc.getRootElement();
            java.util.List children = dept.getChildren();
            for (int i=0; i<children.size(); i++ ) {
              Element e = (Element)children.get(i);
              System.out.println("Element name: "+e.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Document:
<?xml version="1.0"?>
<!DOCTYPE department SYSTEM "department.dtd">
<department>
  <employee id="J.D">
    <name>John Doe</name>
    <email>John.Doe at foo.ibm.com</email>
  </employee>

  <employee id="B.S">
    <name>Bob Smith</name>
    <email>Bob.Smith at foo.com</email>
<url href="http://www.trl.jp.ibm.com/~amiller/"/>
  </employee>

<employee id="A.M"><name>Alice Miller</name>
<url href="http://www.trl.jp.ibm.com/~amiller/"/>


  </employee>
  <test/>
</department>

Output:
Element name: employee
Element name: employee
Element name: employee
Element name: test
Element name: employee
Element name: employee
Element name: employee
Element name: test





More information about the jdom-interest mailing list