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/
Useful thanks 🙂
By: Ena on July 16, 2008
at 14:21
That’s so cool! Thank! 🙂
By: widnu on December 12, 2008
at 13:55
Thank you so much! 😉
By: rené on February 4, 2009
at 16:55
Good stuff, thanks
By: Remi on February 12, 2009
at 14:08
Have you looked at vtd-xml? it is a lot faster and memory efficient than DOM4J and JDOM
vtd-xml
By: anon on November 24, 2009
at 03:51
No, I haven’t. I’ll check that out.
By: Draško Sarić on November 24, 2009
at 13:19
Gracias, me fue de mucha ayuda…
Thank you…
By: Roberto on June 30, 2010
at 06:54
Gracias, realmente me saco de apuros… thanks..
By: Marvin on September 20, 2010
at 20:49
😀 .Thanks
By: Charith soori on July 17, 2011
at 01:10
thank u
By: Narendra on September 20, 2011
at 07:19
Thanks dude
By: Hugo Allexis Cardona on September 23, 2011
at 03:18
Thanks!
By: Robert Glandu on May 25, 2012
at 14:48
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
By: Jacob Thomas on February 6, 2013
at 05:45
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
}
By: Jacob Thomas on February 6, 2013
at 05:46
Thank you man. So nice tutorial. So helpfull!
By: Felipe on November 2, 2015
at 12:46