[jdom-interest] JDOM and recursive documents

James Cook James.Cook at jobpartners.com
Wed Nov 21 07:34:58 PST 2001


This works, but as I dont know how you hold the values, I have made a vector
of String arrays.

    public void run() {
        Vector v = new Vector();
        v.add(new String[] {"a", "b"});
        v.add(new String[] {"c", "d"});
        v.add(new String[] {"e", "f"});
        v.add(new String[] {"g", "h"});
        Element root = createXML(v);
        Document doc = new Document(root);
        XMLOutputter serializer = new XMLOutputter();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.setIndent(true);
        serializer.setNewlines(true);
        try {
            serializer.output(doc, out);
            out.flush();
            out.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        String tmp = new String(out.toByteArray());
        System.out.println(tmp);
    }

    private Element createXML(Vector data) {
        Element element = null;

        if (data.size() > 0) {
            String[] nameValuePair = (String[]) data.get(0);
            element = new Element("NameValuePair").setAttribute("name",
nameValuePair[0]);
            element.addContent(new
Element("Value").addContent(nameValuePair[1]));
            data.remove(0);
            Element child = createXML(data);
            if (child != null) {
                element.addContent(child);
            }
        }

        return element;
    }

output:

<?xml version="1.0" encoding="UTF-8"?>
<NameValuePair name="a">
  <Value>b</Value>
  <NameValuePair name="c">
    <Value>d</Value>
    <NameValuePair name="e">
      <Value>f</Value>
      <NameValuePair name="g">
        <Value>h</Value>
      </NameValuePair>
    </NameValuePair>
  </NameValuePair>
</NameValuePair>


-----Original Message-----
From: Vijay Gummadi [mailto:vijaykgummadi at yahoo.com]
Sent: 21 November 2001 14:56
To: jdom-interest at jdom.org
Subject: [jdom-interest] JDOM and recursive documents


Folks,
 
    After making several attempts and looking up all
the archives, I seek your input in solving this
problem I encountered.
 
    How do I use JDOM to write/create an XML document
that has a structure similar to the one shown here:
 
    <NameValuePair name="a">
        <Value>b</Value>
        <NameValuePair name="c">
            <Value>d</Value>
            <NameValuePair name="e">
                <Value>f</Value>
                <!-- Further nesting possible -->
            </NameValuePair>
        </NameValuePair>
    </NameValuePair>
 
    Any help, including code samples are highly
appreciated.
 
regards,
Vijay
eMail: vijaykgummadi at yahoo.com

__________________________________________________
Do You Yahoo!?
Yahoo! GeoCities - quick and easy web site hosting, just $8.95/month.
http://geocities.yahoo.com/ps/info1
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com


This e-mail and any attachments are confidential and may also be privileged.
If you are not the named recipient, please notify the sender immediately and
do not disclose the contents to another person, use it for any purpose, or
store or copy the information in any medium. Any views or opinions presented
are solely those of the author and do not necessarily represent those of
Jobpartners.  Jobpartners is unable to guarantee the security (especially
that of attached files) of any email content outside of our own computer
systems.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20011121/da482054/attachment.htm


More information about the jdom-interest mailing list