[jdom-interest] repeating nodes

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Wed Sep 8 08:26:45 PDT 2004


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
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://servlets.com/pipermail/jdom-interest/attachments/20040908/26863485/attachment.htm


More information about the jdom-interest mailing list