[jdom-interest] How to get nextSibling, prevSibling, firstChild and lastChild in JDOM?

Harry Evans harry at moviso.com
Tue Sep 17 17:59:37 PDT 2002


No, two elements that contain exactly the same content, even if they are
siblings in the document, will not .equal or == each other.  They will
identifiably unique from the results of the parser and in JDOM.

Harry

> -----Original Message-----
> From: Meghasyam Bokam [mailto:mbokam at cs.nmsu.edu]
> Sent: Tuesday, September 17, 2002 5:53 PM
> To: Harry Evans
> Cc: jdom-interest at jdom.org
> Subject: RE: [jdom-interest] How to get nextSibling, prevSibling,
> firstChild and lastChild in JDOM?
>
>
> Thank You Harry.
>
> Is there any possibility of having two elements with the same
> content and everything in the xml file, then the
> List.indexOf(Object o)
> returns the first element in the tree. I don't know whether the parser
> removes the redundant elements when it parses the xml file.
>
> Meghasyam.
>
> On Tue, 17 Sep 2002, Harry Evans wrote:
>
> > It is a bit more roundabout but:
> >
> > //myElement is the <Element> element in your example
> > //List implementation:
> > Element parent = myElement.getParent();
> > List children = parent.getChildren();
> > int myIndex = children.indexOf(myElement);
> > //get prevSibling
> > if(myIndex > 0 && myIndex < children.size()) {
> >   Element prevElement = (Element)children.get(myIndex - 1);
> > }
> > //get nextSibling
> > if(myIndex >= 0 && myIndex < children.size() - 1) {
> >   Element nextElement = (Element)children.get(myIndex + 1);
> > }
> >
> > //Iterator implementation
> > ListIterator iterator =
> myElement.getParent().getChildren().listIterator();
> > boolean done = false;
> > while(iterator.hasNext() && !done) {
> >   Element element = (Element)iterator.next();
> >   if(myElement.equals(element) {
> >     done = true;
> >   }
> > }
> > if(!done) {
> >   //error
> > } else {
> >   //get prevSibling
> >   if(iterator.hasPrevious()) {
> >     Element prevElement = (Element)iterator.previous();
> >     iterator.next(); //put it back on myElement
> >   }
> >   //get nextSibling
> >   if(iterator.hasNext()) {
> >     Element nextElement = (Element)iterator.next();
> >     iterator.previous(); //put it back on myElement
> > }
> >
> > In my experience, usually getting next sibling and previous
> siblings is
> > done, at least in my experience, in the context of looking
> at all the
> > siblings, though your usage may vary.
> >
> > Anyone else have a more elegant solution?
> >
> > Harry
> >
> > > -----Original Message-----
> > > From: Meghasyam Bokam [mailto:mbokam at cs.nmsu.edu]
> > > Sent: Tuesday, September 17, 2002 4:59 PM
> > > To: Harry Evans
> > > Subject: RE: [jdom-interest] How to get nextSibling, prevSibling,
> > > firstChild and lastChild in JDOM?
> > >
> > >
> > > Thank You Harry. I understood getting the first and last
> child of an
> > > element. But I will explain what I mean by siblings.
> > >
> > > <?xml version="1.0"?>
> > > <root>
> > >    <prevSibling>
> > >    	text
> > >    </prevSibling>
> > >    <Element>
> > > 	<child1> text </child1>
> > > 	<child2> text </child2>
> > >    </Element>
> > >    <nextSibling>
> > > 	text
> > >    </nextSibling>
> > > </root>
> > >
> > > Now if I am processing at "Element" , how to get the
> "nextSibling" and
> > > "prevSibling" elements, considering only I have "Element"
> at my hand.
> > >
> > > Thank You.
> > >
> > > Meghasyam.
> > >
> > >
> > > On Mon, 16 Sep 2002, Harry Evans wrote:
> > >
> > > > You can access the children of any element as a standard
> > > java.util.List by
> > > > just calling getChildren() on the Element.  Some examples
> > > of how you could
> > > > get what you are asking for:
> > > >
> > > > xml:
> > > > <?xml version="1.0"?>
> > > > <root>
> > > >   <child>1</child>
> > > >   <child>2</child>
> > > >   <child>3</child>
> > > >   <child>4</child>
> > > >   <child>5</child>
> > > > </root>
> > > >
> > > > java code:
> > > > List children = myElement.getChildren();
> > > > //first child element
> > > > Element firstElement = (Element)children.get(0);
> > > > //last child element
> > > > Element lastElement =
> (Element)children.get(children.size() - 1);
> > > > Element arbitraryElement = (Element)children.get(3);
> > > > int currentIndex = children.indexOf(arbitraryElement);
> > > > if(currentIndex >= 0 && currentIndex < children.size() + 1) {
> > > > //sibling after arbitrary element
> > > > 	Element nextElement = children.get(currentIndex + 1);
> > > > }
> > > >
> > > > You could also just use an iterator, as the List returned
> > > by getChildren()
> > > > is a standard java.util.List.
> > > >
> > > > Harry
> > > >
> > > > > -----Original Message-----
> > > > > From: jdom-interest-admin at jdom.org
> > > > > [mailto:jdom-interest-admin at jdom.org]On Behalf Of
> Meghasyam Bokam
> > > > > Sent: Monday, September 16, 2002 5:21 PM
> > > > > To: jdom-interest at jdom.org
> > > > > Subject: [jdom-interest] How to get nextSibling, prevSibling,
> > > > > firstChild
> > > > > and lastChild in JDOM?
> > > > >
> > > > >
> > > > > Hi all,
> > > > >
> > > > >     I am new to JDOM, and I see there are no methods to get
> > > > > nextSibling,
> > > > > prevSibling, firstChild and lastChild of an ELEMENT. Can
> > > > > anybody help me
> > > > > to write those specific methods which I can use in my
> application.
> > > > >
> > > > > Thanks for the help.
> > > > >
> > > > > Meghasyam.
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > To control your jdom-interest membership:
> > > > > http://lists.denveronline.net/mailman/options/jdom-interest/yo
> > > > uraddr at yourhost.com
> > > >
> > >
> >
>




More information about the jdom-interest mailing list