[jdom-interest] getChildren() returns an empty list.. (pleas e help)

Robertson, Jason Jason.Robertson at acs-inc.com
Tue Oct 22 12:05:30 PDT 2002


You ask the root for all the "application-roles" children, but the root
doesn't have this child. You need to navigate down the tree: security ->
application -> application-roles -> role. (In this code I'm assuming you
want the "role" elements in the end, in other words your list should contain
2 items):

Element root = doc.getRootElement();
Element app = root.getChild("application");
Element appRoles = app.getChild("application-roles");
List roles = appRoles.getChildren("role");
System.out.println("Size " + roles.size());

You can also use this shorthand:

Element root = doc.getRootElement();
List roles = root.getChild("application")
                 .getChild("application-roles")
                 .getChildren("role");
System.out.println("Size " + roles.size());

Jason	

-----Original Message-----
From: Mirabito, Massimo [mailto:mcm8 at cdc.gov]
Sent: Tuesday, October 22, 2002 8:43 AM
To: 'jdom-interest at jdom.org'
Subject: [jdom-interest] getChildren() returns an empty list.. (please
help)
Importance: High


You will have to forgive me but I am extremely new to JDOM. I have been
struggling with  this particular problem for a day now and I cannot figure
it out.

Following is a summary of my problem

1) XML document
<?xml version="1.0" encoding="UTF-8"?>
<security xmlns="s2">
  <application>
    <application-id>100</application-id>
    <application-name>Application 1</application-name>
    <application-description>Application 1
Description</application-description>
    <application-roles>
      <role id="1" name="Role 1" description="Role 1 Desc" />
      <role id="2" name="Role 2" description="Role 2 Desc" />
    </application-roles>
  </application>
  <user-roles>
    <role id="1" name="Role 1" description="Role 1 Desc">
      <access id="1" name="can">111111</access>
    </role>
    <role id="2" name="Role 2" description="Role 2 Desc">
      <access id="2" name="irb_id">4444444</access>
    </role>
  </user-roles>
</security>

2) I load it into JDOM in the following manner

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));


3) I can get the root element I check and see if it has children and it
returns true.... but when I invoke getChildren() the List size is 0

root = doc.getRootElement();
System.out.println("has children " + root.hasChildren());		
List appRoles = root.getChildren("application-roles ");
System.out.println("Size " + appRoles.size());	


4) However I can process all elements in the document with no problem with
the following function

private void processElement(Element e) {
  List list = e.getChildren();
  Iterator i = list.iterator();
   while (i.hasNext()) {
    Element element = (Element) i.next();
    System.out.println(list.size() + " ***" + element.getName() + " - " +
    element.getAttributeValue("id") + " - " + 
    element.getAttributeValue("name") + " - " + 
    element.getAttributeValue("description") + " - " + 
    element.getText().trim()   );
    processElement (element);
  }
}

What Am I doing wrong?

Thanks in advance for any help

Max


_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list