-1

I currently have this line of code :

org.jdom2.Element typeContent = root.getChildren().get(objTypeIndex);

This was working fine until you added children with other names. Then it started to bug and change the data in the wrong child. If I got index 1 it would get the first earlier. but with new children it will get the instead with current line of code. That not what I want to do.

Is there anyway I can get all children by name ?

If I have root children like

<insert-data></insert-data>
<insert-data></insert-data>
<insert-data></insert-data>

and then

<type></type>
<type></type>
<type></type>
<type></type>

And I only wanna get the from the root? How can I do that with JDOM.

Ive tried :

org.jdom2.Element typeContent = (Element) root.getChildren("type").get(
                objTypeIndex);

but it cast exception : Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 19 Size: 19

Sembrano
  • 1,127
  • 4
  • 18
  • 28

1 Answers1

0

Take a look at the API (http://www.jdom.org/docs/apidocs/org/jdom2/Element.html#getChildren(java.lang.String)), how to be able to obtain children by name.

Smutje
  • 17,733
  • 4
  • 24
  • 41
  • Ive done that before I posted and its casting exception – Sembrano Feb 20 '14 at 13:09
  • Yes, because you statically access a given index without knowing if this index is valid. – Smutje Feb 20 '14 at 13:41
  • org.jdom2.Element typeContent = root.getChildren().get(objTypeIndex); This one is working but edit on wrong place atm. but when I add "type" in my getchildren it gives exception. So nothing wrong with the index I think right? the index is 19. And I have 19 – Sembrano Feb 20 '14 at 13:42
  • oh yes. it starts counting from 0 not from 1. Thats it. – Sembrano Feb 20 '14 at 14:02