This discussion is archived
4 Replies Latest reply: Sep 21, 2009 3:14 AM by 807580 RSS

Newbie questions about JDom

807580 Newbie
Currently Being Moderated
Hello, this weekend I have tried to do one function with JDom, but I haven't understood very good the Api and I have some questions that I hope that someone can clarify. If I see this function, I could do more functions because I will understand better the api.

I have one list with this dates:

Nombre
Nombre.Apellido
Nombre.Apellido.Segundo
Nombre2
Nombre2.Apellido
Nombre2.Apellido2
. . . .

And I want to create something like this:
<?xml version="1.0" encoding="UTF-8"?>
<Agendas>
<Cuenta label="Persona1" id="1">
<node label="Nombre" id="x">
<node label="Apellido" id="x">
<node label="Segundo" id="x"/>
</node>
</node>
<node label="Nombre2" id="x">
<node label="Apellido" id="x"/>
<node label="Apellido2" id="x"/>
</node>
</Cuenta>
</Agendas>
I don't want something static, because the list can change, and I can have much more nodes. I want something dinamic. I'm using 'split' to separate the list, but I haven't get do the correct function. Probably is very easy, but I want to see done to understand better the Api and continue doing more things with the api.

Thanks!
  • 1. Re: Newbie questions about JDom
    807580 Newbie
    Currently Being Moderated
    http://lmgtfy.com?q=jdom+example

    http://www.brics.dk/~amoeller/XML/programming/jdomexample.html
  • 2. Re: Newbie questions about JDom
    807580 Newbie
    Currently Being Moderated
    Thanks, I have read some examples, and I have try do more than one thing during the weekend, but I don't know how can apply JDom in this function that I need. I don't know how can create the children dinamicatly. I will continue trying.
  • 3. Re: Newbie questions about JDom
    807580 Newbie
    Currently Being Moderated
    I can't get done this function. I will use my old function that I create a XML without JDom, and then I will create the document:
    SAXBuilder builder = new SAXBuilder();
            Document document = builder.build(new ByteArrayInputStream(data.getBytes()));//data= my xml
    Is not the best solution, I prefer create a XML with JDom but I can't. I someone want help me, I will thank you, but I will use my old function, because I can't create the new because I'm thinking in the old and Jdom is not the same.
  • 4. Re: Newbie questions about JDom
    807580 Newbie
    Currently Being Moderated
    I'm adding my String to document, but exist anyway to add like a Element?

    I want to create my document and then add differents Strings. Something like this.
    String xml =
                    "<account>" +
                    "    <country name=\"Japan\" capital=\"Tokyo\"/>" +
                    "    <country name=\"France\" capital=\"Paris\"/>" +
                    "</account>";
    
    Document doc = new Document();
    Element root = new Element("Accounts");
    doc.addContent(root)
    root.addContent(xml);
    I can't do the last line. If I use the bouilder that I put in the before post I can't because I get a document, not and element. I can do something like this with element?