XML to Element via JDOM?

P

Peter the Swede

I have an XML-file that contains of elements and subelements. How do I
convert the XML-file to an JDOM Element-tree?

The XML can look like this:

<PERSON>
<NAME>Peter</NAME>
<AGE>23</AGE>
</PERSON>

What I want is to be able to get Peter and 23 out of this XML-file.
Preferably by first converting it to an Element and then use the Element to
get the values.

Cheers, Peter
 
C

Christophe Vanfleteren

Peter said:
I have an XML-file that contains of elements and subelements. How do I
convert the XML-file to an JDOM Element-tree?

The XML can look like this:

<PERSON>
<NAME>Peter</NAME>
<AGE>23</AGE>
</PERSON>

What I want is to be able to get Peter and 23 out of this XML-file.
Preferably by first converting it to an Element and then use the Element to
get the values.

Cheers, Peter

you might want to read the JDOM chapters at
http://www.cafeconleche.org/books/xmljava/
 
R

robert

Peter the Swede said:
I have an XML-file that contains of elements and subelements. How do I
convert the XML-file to an JDOM Element-tree?

The XML can look like this:

<PERSON>
<NAME>Peter</NAME>
<AGE>23</AGE>
</PERSON>

What I want is to be able to get Peter and 23 out of this XML-file.
Preferably by first converting it to an Element and then use the Element to
get the values.

Cheers, Peter

In JDOM use the SAXbuilder to make a new DOM from an XML stream. here
is a snippet ...

ServletInputStream in = request.getInputStream();
Document xmlDoc = null;
SAXBuilder builder = new SAXBuilder();
if (builder == null) {
xmlDoc = null;
} else {
xmlDoc = builder.build(in);
}
........
of course this if for reading the input stream for a servlet, but the
builder also supports write so you could
String strBuffer = " your XML string "
StringWriter in = strBuffer.getWriter();

havefun, -rb
 
P

Peter the Swede

Thanks. I did it that way and it worked well, some modifications of course
:)

Cheers, Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top