[jdom-interest] repeating nodes

Flávio Marim flavio at bs2.com.br
Wed Sep 8 12:14:19 PDT 2004


OK. It inserted the nodes.
But the <def> node is among many others. All nodes in document are filled
in diferent and non-linear moments. How can I assembly the XML in correct
order without loosing of inner contents? The tree is:

<main>
   <listed-item>
      <place>The place</place>
      <receiver>The receiver</receiver>
      <def>
         <number>number of 1st def</number>
         <type>type of 1st def</type>
      </def>
      <def>
         <number>number of 2nd def</number>
         <type>type of 2nd def</type>
      </def>
      <alert>
         <text>Alert #1</text>
      </alert>
      <alert>
         <text>Alert #2</text>
      </alert>
   </listed-item>
</main>

Phill_Perryman at Mitel.COM disse:
> If you keep detaching the element it will remove it from the tree
>
> I suspect you need code more like
>
> public void setDef(String string) {
>    StringTokenizer st = new StringTokenizer(string, "|");
>    while(st.hasMoreElements()){
>       Element listedItem = new Element("listed-item");
>       Element number = new Element("number");
>       Element type = new Element("type");
>       number.setText(st.nextToken());
>       type.setText(st.nextToken());
>         listedItem.addContent(number);
>       listedItem.addContent(type);
>       def.addContent(listedItem);
>    }
> }
>
> /Phill
> IS Dept, Software Engineer.
> phill_perryman at mitel.com
> http://www.mitel.com
> Tel: +44 1291 436023
>
>
>
>
> Flávio Marim <flavio at bs2.com.br>
> Sent by: jdom-interest-bounces at servlets.com
> 08/09/2004 16:53
>
>
>         To:     jdom-interest at jdom.org
>         cc:
>         Subject:        [jdom-interest] repeating nodes
>
>
> I think I miss something in the docs...
> I'm trying to build an XML that has a node wich has multiple children with
> the same name as follows:
>
> <def>
>    <listed-item>
>       <number></number>
>       <type></type>
>    </listed-item>
>    <listed-item>
>       <number></number>
>       <type></type>
>    </listed-item>
> </def>
>
> I'm getting the data from a string like this:
>
> |number1|type1|number2|type2|...|numberN|typeN|
>
> public void setDef(String string) {
>    StringTokenizer st = new StringTokenizer(string, "|");
>    while(st.hasMoreElements()){
>       setNumber(st.nextToken());
>       setType(st.nextToken());
>       def.addContent(listedItem.detach());
>    }
> }
>
> The methods setNumber() and setType() just set text to "number" and "type"
> tags.
> When the method setDef() is called, the tree is already built.
>
> The problem: the final built document is
>
> <def>
>    <listed-item>
>       <number>N</number>
>       <type>N</type>
>    </listed-item>
> </def>
>
> I mean, only the LAST <listed-item> block is recorded. The <listed-item>'s
> are all overwritten. Which one by his next.
>
> What is the beginner here missing?
>
> Thanks in advance.
>
> --
> Flávio Marim

--
Flávio Marim


More information about the jdom-interest mailing list