Nesting XML Elements in Java

Discussion in 'Java' started by dmckeon@ameritas.com, Apr 18, 2006.

  1. Guest

    I am new to Java and XML, so forgive any ignorance. I am working on a
    program to create an XML document and I am having some problems with
    nesting. I want something like the following:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Employees>
    <EMPLOYEE>
    <NAME>
    <FIRST>John</FIRST>
    <LAST>Doe</LAST>
    </NAME>
    <TITLE>Mr.</TITLE>
    <WAGE>
    <RATE>Salaried</RATE>
    <WAGE>40,000</WAGE>
    </WAGE>
    </EMPLOYEE>
    </Employees>

    But instead, I am getting:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Employees>
    <NAME>
    <FIRST>Jeff</FIRST>
    <LAST>Fry</LAST>
    </NAME>
    <TITLE>Senior</TITLE>
    <WAGE>
    <RATE>Salaried</RATE>
    <WAGE>40,000</WAGE>
    </WAGE>
    <EMPLOYEE />
    </Emplyees>

    The difference being the EMPLOYEE tag. It appears to be a problem
    calling the startParent method twice in a row. Here is my XML code:

    package igxml;

    import java.io.*;
    import org.w3c.dom.*;
    import org.xml.sax.helpers.DefaultHandler;
    import javax.xml.parsers.*;
    import org.apache.xml.serialize.*;

    public class IgXMLBuild extends DefaultHandler {

    Document doc;
    Element root;
    Element parentElement;
    Element childElement;
    Text textData;

    public void startDocument(String docName)
    {

    // Create the DOM object
    DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
    builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    System.out.println("Could not locate a JAXP DocumentBuilder
    class");
    }
    doc = builder.newDocument();

    /* create the root node */
    root = doc.createElement(docName);
    return;
    }

    public Element startParent (String parentName)
    {
    /* create a row node */
    parentElement = doc.createElement(parentName);
    return parentElement;
    }

    public void endParent (Element parentElement)
    {
    /* append the row node to the root node */
    root.appendChild(parentElement);
    }

    public void addParent(String passParentName, String
    passParentValue)
    {
    parentElement = startParent(passParentName);
    parentElement.appendChild(doc.createTextNode(passParentValue));
    endParent(parentElement);
    }

    public void addChild(String passElementName, String passElementValue)
    {
    childElement = doc.createElement(passElementName);
    textData = doc.createTextNode(passElementValue);
    childElement.appendChild(textData);
    parentElement.appendChild(childElement);
    }

    public String finishDocument()
    {
    /* append the root node to the empty document */
    doc.appendChild(root);

    try {
    // Serialize the document
    StringWriter out;
    out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, new
    OutputFormat(doc));
    serializer.serialize(doc);
    String xmlString = out.toString();
    return xmlString;
    } catch (IOException e) {
    System.err.println(e);
    return e.toString();
    }
    }
    }

    And here is the test application I am using to call these methods:

    package igxml;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.w3c.dom.*;

    public class TestApp {

    static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS
    ");

    public static void logit(String m){
    System.out.println( df.format(new Date()) + m);
    }

    public static void main(String[] args) {
    /* logit("TestApp started...");
    String tranReqDoc, docList;
    ImgDocList dl = new ImgDocList();
    tranReqDoc = "<?xml version='1.0'
    encoding='UTF-8'?><DATA><IMG-APP-ID>PS</IMG-APP-ID><IMG-FOLDER-ID>1111111111</IMG-FOLDER-ID><IMG-USER-ID>ISJAF</IMG-USER-ID></DATA>";
    docList = dl.getDocList(tranReqDoc);
    System.out.println(docList);
    logit("TestApp finished...");*/
    Element empElement;
    Element nameElement;
    Element wageElement;

    IgXMLBuild xmlDoc = new IgXMLBuild();
    xmlDoc.startDocument("Employees");
    empElement = xmlDoc.startParent("EMPLOYEE");
    nameElement = xmlDoc.startParent("NAME");
    xmlDoc.addChild("FIRST", "John");
    xmlDoc.addChild("LAST", "Doe");
    xmlDoc.endParent(nameElement);
    xmlDoc.addParent("TITLE", "Mr.");
    wageElement = xmlDoc.startParent("WAGE");
    xmlDoc.addChild("RATE", "Salaried");
    xmlDoc.addChild("AMMOUNT", "40,000");
    xmlDoc.endParent(wageElement);
    xmlDoc.endParent(empElement);
    empElement = xmlDoc.startParent("EMPLOYEE");
    nameElement = xmlDoc.startParent("NAME");
    xmlDoc.addChild("FIRST", "Jane");
    xmlDoc.addChild("LAST", "Doe");
    xmlDoc.endParent(nameElement);
    xmlDoc.addParent("TITLE", "Mrs.");
    wageElement = xmlDoc.startParent("WAGE");
    xmlDoc.addChild("RATE", "Hourly");
    xmlDoc.addChild("AMMOUNT", "42.00");
    xmlDoc.endParent(wageElement);
    xmlDoc.endParent(empElement);
    empElement = xmlDoc.startParent("EMPLOYEE");
    nameElement = xmlDoc.startParent("NAME");
    xmlDoc.addChild("FIRST", "John");
    xmlDoc.addChild("LAST", "Smith");
    xmlDoc.endParent(nameElement);
    xmlDoc.addParent("TITLE", "Sir");
    wageElement = xmlDoc.startParent("WAGE");
    xmlDoc.addChild("RATE", "Salaried");
    xmlDoc.addChild("AMMOUNT", "48,000");
    xmlDoc.endParent(wageElement);
    xmlDoc.endParent(empElement);
    String xmlString = xmlDoc.finishDocument();
    System.out.println(xmlString);
    }
    }


    Any idea why I can't nest that extra level???
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >I am new to Java and XML, so forgive any ignorance. I am working on a
    > program to create an XML document and I am having some problems with
    > nesting. I want something like the following:
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <Employees>
    > <EMPLOYEE>
    > <NAME>
    > <FIRST>John</FIRST>
    > <LAST>Doe</LAST>
    > </NAME>
    > <TITLE>Mr.</TITLE>
    > <WAGE>
    > <RATE>Salaried</RATE>
    > <WAGE>40,000</WAGE>
    > </WAGE>
    > </EMPLOYEE>
    > </Employees>
    >
    > But instead, I am getting:
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <Employees>
    > <NAME>
    > <FIRST>Jeff</FIRST>
    > <LAST>Fry</LAST>
    > </NAME>
    > <TITLE>Senior</TITLE>
    > <WAGE>
    > <RATE>Salaried</RATE>
    > <WAGE>40,000</WAGE>
    > </WAGE>
    > <EMPLOYEE />
    > </Emplyees>
    >
    > The difference being the EMPLOYEE tag. It appears to be a problem
    > calling the startParent method twice in a row. Here is my XML code:
    >

    [most of the code snipped]
    > public void endParent (Element parentElement)
    > {
    > /* append the row node to the root node */
    > root.appendChild(parentElement);
    > }


    The reason NAME and WAGE are added to the root is because that's what your
    code says to do.

    - Oliver
     
    Oliver Wong, Apr 19, 2006
    #2
    1. Advertising

  3. Guest

    But my question is why am I getting <EMPLOYEE /> rather than:
    <EMPLOYEE>
    ....
    </EMPLOYEE>
     
    , Apr 20, 2006
    #3
  4. wrote:
    > But my question is why am I getting <EMPLOYEE /> rather than:
    > <EMPLOYEE>
    > ...
    > </EMPLOYEE>


    Because that's what you have coded. You are mixing up your own
    addParent/endParent methods.

    As a general remark, the API of your XML builder is rather confusing and
    the architecture is suboptimal. You have partly externalized state
    handling (the return data from startXXX). This state (the location where
    you are in the tree) should better be internal builder state. You
    probably need a stack inside the builder to hold the state.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Apr 20, 2006
    #4
  5. Guest

    Thanks for the reply. As I said, I am VERY new to this. I was
    wondering... If we had an XML schema, could we just pass in the element
    name and value and then use the schema somehow to know where to nest
    the value? I've seen schemas used for validation. I just wasn't sure
    if they could also be used to create the XML. It sounds reasonable to
    me.
     
    , Apr 20, 2006
    #5
  6. Oliver Wong Guest

    <> wrote in message
    news:...
    > Thanks for the reply. As I said, I am VERY new to this. I was
    > wondering... If we had an XML schema, could we just pass in the element
    > name and value and then use the schema somehow to know where to nest
    > the value? I've seen schemas used for validation. I just wasn't sure
    > if they could also be used to create the XML. It sounds reasonable to
    > me.
    >


    No, because the same element name might appear in multiple locations in
    the schema.

    - Oliver
     
    Oliver Wong, Apr 20, 2006
    #6
  7. Oliver Wong Guest

    "Oliver Wong" <> wrote in message
    news:_jR1g.812$Fd6.2@edtnps82...
    >
    > <> wrote in message
    > news:...
    >> Thanks for the reply. As I said, I am VERY new to this. I was
    >> wondering... If we had an XML schema, could we just pass in the element
    >> name and value and then use the schema somehow to know where to nest
    >> the value? I've seen schemas used for validation. I just wasn't sure
    >> if they could also be used to create the XML. It sounds reasonable to
    >> me.
    >>

    >
    > No, because the same element name might appear in multiple locations in
    > the schema.


    BTW, do you have to use Schemas? It might be conceptually simpler if you
    just define your own tree-like abstract data type, and implement a
    "toXMLString()" method on the nodes. I suggest this, because I notice that
    the code you posted earlier doesn't seem to use schemas at all.

    - Oliver
     
    Oliver Wong, Apr 20, 2006
    #7
  8. Guest

    I guess I just don't know what best practice/industry standard is for
    this sort of thing. No one else in my company codes Java or XML, so my
    team is blazing the trail. Since I have no one to ask, I just want to
    make sure I do a good job. FYI -- We're converting our COBOL system to
    a web-based Java SOA system.

    I understand your point about an element name appearing in multiple
    places in a schema, but shouldn't there be a way to qualify it so it
    knows which one you're referring to. At the very least, is there a way
    to do it if all element names are unique?

    We started down the path of just creating our own XML string without
    using SAX or DOM or anything else. We were getting away from that in
    an attempt to fit the norms of best practice. Perhaps we're going
    about it incorrectly. I would love to hear from anyonw out there what
    they recommend for creating XML and sample Java code would be amazing!
    I'm not trying to plagarize, it's just hard to understand all of the
    new terminology, so to see it in action helps me understand better.

    Once again, thanks so much for your input!
     
    , Apr 20, 2006
    #8
  9. Oliver Wong Guest

    <> wrote in message
    news:...
    >I guess I just don't know what best practice/industry standard is for
    > this sort of thing. No one else in my company codes Java or XML, so my
    > team is blazing the trail. Since I have no one to ask, I just want to
    > make sure I do a good job. FYI -- We're converting our COBOL system to
    > a web-based Java SOA system.
    >
    > I understand your point about an element name appearing in multiple
    > places in a schema, but shouldn't there be a way to qualify it so it
    > knows which one you're referring to. At the very least, is there a way
    > to do it if all element names are unique?
    >
    > We started down the path of just creating our own XML string without
    > using SAX or DOM or anything else. We were getting away from that in
    > an attempt to fit the norms of best practice. Perhaps we're going
    > about it incorrectly. I would love to hear from anyonw out there what
    > they recommend for creating XML and sample Java code would be amazing!
    > I'm not trying to plagarize, it's just hard to understand all of the
    > new terminology, so to see it in action helps me understand better.


    You might find this book helpful:
    http://www.cafeconleche.org/books/xmljava/

    - Oliver
     
    Oliver Wong, Apr 20, 2006
    #9
  10. Guest

    Awesome! I'll check it out!
     
    , Apr 20, 2006
    #10
  11. hereticmessiah

    Joined:
    May 1, 2006
    Messages:
    1
    I have something that might help you

    Here goes the gratuitous self promotion. :)

    I wrote a library a while back that might ease your pain as far as building XML documents goes. It's called ElementNode, and you can find it here: http://talideon.com/projects/elementnode/

    Using it, the document you're looking to build would be built something like this:

    ElementNode root = new ElementNode("Employees");
    ElementNode employee = root.addChild("EMPLOYEE");
    employee.addChild("NAME").addTextChild("FIRST", "John").addTextChild("LAST", "Doe");
    employee.addChild("TITLE", "Mr.");
    employee.addChild("WAGE").addTextChild("RATE", "Salaried").addTextChild("WAGE", "40,000");

    String serialisedXml = root.toString();


    The library automatically takes care of tag balancing.
     
    Last edited: May 1, 2006
    hereticmessiah, May 1, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill C

    Child Elements/Nesting

    Bill C, Feb 26, 2004, in forum: XML
    Replies:
    0
    Views:
    382
    Bill C
    Feb 26, 2004
  2. P
    Replies:
    1
    Views:
    1,174
    Joe Kesselman
    Jul 7, 2006
  3. Ultrus
    Replies:
    4
    Views:
    2,645
    Stefan Behnel
    Aug 30, 2007
  4. Trans
    Replies:
    10
    Views:
    308
    Sean O'Halpin
    Sep 16, 2005
  5. Replies:
    6
    Views:
    126
    Richard Cornford
    Feb 19, 2007
Loading...

Share This Page