Posted by: Draško Sarić | June 12, 2008

JDom: read/parse XML from String

JDOM as you (Java developers) all know is excellent package for dealing with XML. Its interface is very simple to use, and yet it’s very fast. What else do you want?

Now, I needed to read/parse XML from String, not from file and I wandered around a bit and finally made it work. Let’s just say that we have XML structure like:

<?xml version="1.0" encoding="UTF-8"?>
<transDocument>
<commResponse>
<meta>
<code>0</code>
<description>Success</description>
</meta>
</commResponse>
</transDocument>

and we have it all in String variable called xmlResponse and we want values code and description tags. Those values will be found in String variables code and description respectively. Here’s the way how to do it:

import java.net.*;
import java.io.*;</pre>
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

SAXBuilder builder = new SAXBuilder();
Reader in = new StringReader(xmlResponse);
Document doc = null;
Element root = null;
Element meta = null;
Element _code = null;
Element _description = null;
String code = null;
String description = "";
try
{
 doc = builder.build(in);
 root = doc.getRootElement();
 meta = root.getChild("commResponse").getChild("meta");
 _code = meta.getChild("code");
 _description = meta.getChild("description");
 code = _code.getText();
 description = _description.getText();

} catch (JDOMException e)
 {
 // do what you want
 } catch (IOException e)
 {
 // do what yo want
 } catch (Exception e)
 {
 // do what you want
 }
System.out.println("code: " + code + "\ndescription: " + description);

Make money with this! 🙂

PS more info about JDOM can be found at: http://www.jdom.org/

Advertisements

Responses

  1. Useful thanks 🙂

  2. That’s so cool! Thank! 🙂

  3. Thank you so much! 😉

  4. Good stuff, thanks

  5. Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM

    vtd-xml

    • No, I haven’t. I’ll check that out.

  6. Gracias, me fue de mucha ayuda…

    Thank you…

  7. Gracias, realmente me saco de apuros… thanks..

  8. 😀 .Thanks

  9. thank u

  10. Thanks dude

  11. Thanks!

  12. I have a rtf document which I need to convert to HTML Text form . I did it but it has “\n” character in it . Any suggestion

  13. Sorry missed the rtf whcih I did convert
    Rtf text
    ———-

    {\rtf1\ansi\ansicpg1250\deff0\deflang1029{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 this is pore rtf\par
    \par
    in word pad after the new line\par
    }

  14. Thank you man. So nice tutorial. So helpfull!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: