[jdom-interest] Element.getChildren(java.lang.String name)?

Gary Bentley gb at opengroup.org
Wed Mar 7 07:06:46 PST 2001


Try:

Element root = doc.getRootElement ("complexType");

List items = root.getChildren ("sequence");

if (items.size () == 0)
{

    System.out.println ("No sequence items");
    return;

}

ListIterator listit = items.listIterator ();

while (listit.hasNext ())
{

    Element el = (Element) listit.next ();

}

G.

-----Original Message-----
From: Paul Simon Barriscale [mailto:barrisca at student.cs.ucc.ie]
Sent: Wednesday, March 07, 2001 2:37 PM
To: Gary Bentley
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Element.getChildren(java.lang.String name)?


I see what you mean. However I'm trying to use an Iterator to "iterate"
through this.

Element root = doc.getRootElement() // complexType

java.util.Iterator iter = root.getChildren("sequence").iterator();
while (iter.hasNext())
{
	org.jdom.Element elem = (org.jdom.Element) iter.next();
	System.out.println(elem.getName());
}

This should be the same thing I thought.

>
> If you are trying to find the "element" element...then you need to dig one
> level down...
>
> Element root = doc.getRootElement ("complexType");
> Element el = root.getChild ("sequence").getChild ("element");
>
> If you are using getChildren that will return a List of elements...
> In that case you have to index into the List and get the element you
want...
>
> List children = root.getChildren ("sequence");
> Element firstchild = (Element) children.get (0);
> Element el = firstchild.getChild ("element");
>
> Remember to check for null for the children List.
>
> It may also be a naming problem...i.e. you could have spelled something
> incorrectly...
>
> To get around that I use static final variables...
>
> i.e.
>
> public class XMLDefs
> {
>
>     static public final String complexType = "complexType";
>
> }
>
> Then use it:
>
> Element root = doc.getRootElement (XMLDefs.complexType);
>
> That way the compiler will tell you if you are wrong...
> It's a bit of an overhead but saves a lot of time for other problems...
>
> G.
>
> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Paul Simon Barriscale
> Sent: Wednesday, March 07, 2001 12:53 PM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] Element.getChildren(java.lang.String name)?
>
>
> I'm having trouble accessing the the children of a specific element.
> this is a fragment of the XML
>
> <complexType name="RepositoryType">
> 	<sequence>
> 		<element name="IRObject" type="IRObjectType"/>
> 		...
> 		...
> 	</sequence>
> </complexType>
>
> by when I call Element.getChildren(complexType) it does not find this
> element.
> Am i calling this method incorrectly? any help is appreciated.
>
> Regards,
> Paul
>
>
>
> --
> Paul Barriscale
> barrisca at student.cs.ucc.ie    http://student.cs.ucc.ie/01/barrisca
> _______________________________________________________
> University College Cork
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
> t.com
>


--
Paul Barriscale
barrisca at student.cs.ucc.ie    http://student.cs.ucc.ie/01/barrisca
_______________________________________________________
University College Cork




More information about the jdom-interest mailing list