[jdom-interest] Feature request

Bradley S. Huffman hip at cs.okstate.edu
Fri Jul 18 19:01:11 PDT 2003


Eric VERGNAUD writes:

> Hi all,
> 
> I've been using JDOM for 2-3 months and am very pleased with it. However, I
> sometimes find that it requires some heavy writing when you're doing very
> light things.
> 
> In the documents I create, there is very often only one single child.
> Typically, the output looks like that:
> 
> <root>
>     <child_level_1>
>         <child_level_2>
>             <child_level_3>
>                 <child_level_4>
>                 Data
>                 </child_level_4>
>             </child_level_3>
>         </child_level_2>
>     </child_level_1>
>     <child_level_1>
>         <child_level_2>
>             <child_level_3>
>                 <child_level_4>
>                 Data
>                 </child_level_4>
>             </child_level_3>
>         </child_level_2>
>     </child_level_1>
> </root>
> 
> I haven't found a way to simply add one single child to an element, so my
> code looks like the following:
> 
> list1 = new ArrayList();
> 
> // create first child
> child1 = new Element("child_level_1");
> list1.add(child1);
> 
> list2 = new ArrayList();
> child2 = new Element(("child_level_2");
> list2.add(child2);
> child1.setChildren(list2);
> 
> list3 = new ArrayList();
> child3 = new Element(("child_level_3");
> list3.add(child3);
> child2.setChildren(list3);
> 
> list4 = new ArrayList();
> child4 = new Element(("child_level_4");
> list4.add(child4);
> child3.setChildren(list4);
> 
> // create second child
> child1 = new Element("child_level_1");
> list1.add(child1);
> 
> list2 = new ArrayList();
> child2 = new Element(("child_level_2");
> list2.add(child2);
> child1.setChildren(list2);
> 
> list3 = new ArrayList();
> child3 = new Element(("child_level_3");
> list3.add(child3);
> child2.setChildren(list3);
> 
> list4 = new ArrayList();
> child4 = new Element(("child_level_4");
> list4.add(child4);
> child3.setChildren(list4);
> 
> root.setChildren(list1);
> 
> A bit heavy, don't you think ? I would very much appreciate to be able to
> write simply:
> 
> list1 = new ArrayList();
> 
> // create first child
> child1 = new Element("child_level_1");
> list1.add(child1);
> 
> child2 = new Element(("child_level_2");
> child1.setChild(child2 );
> 
> child3 = new Element(("child_level_3");
> child2. setChild(child3 );
> 
> child4 = new Element(("child_level_4");
> child3. setChild(child4 );
> 
> // create second child
> child1 = new Element("child_level_1");
> list1.add(child1);
> 
> child2 = new Element(("child_level_2");
> child1. setChild(child2 );
> 
> child3 = new Element(("child_level_3");
> child2. setChild(child3 );
> 
> child4 = new Element(("child_level_4");
> child3. setChild(child4 );
> 
> root.setChildren(list1);
> 
> What do you think ?

root = new Element("root");
Element e1 = new Element("child_level_1");
root.addContent(e1);
Element e2 = new Element("child_level_2");
e1.addContent(e2);
Element e3 = new Element("child_level_3");
e2.addContent(e3);
Element e4 = new Element("child_level_4");
e3.addContent(e4);
e4.setText("Data");

Element e11 = new Element("child_level_1");
root.addContent(e11);
Element e12 = new Element("child_level_2");
e11.addContent(e12);
Element e13 = new Element("child_level_3");
e12.addContent(e13);
Element e14 = new Element("child_level_4");
e13.addContent(e14);
e14.setText("Data");

Brad



More information about the jdom-interest mailing list