how to read XML?

hi, i am passing a string as below to my program.

<PERSON><id>1</id><country>Australia</country></PERSON>

but how to read this string to get the id and country? i have done by write it to a file.xml and read it. but can it done by no need write to file and read it? anyone can giv me a simple example? thanks

[345 byte] By [gracebabya] at [2007-11-27 11:09:05]
# 1

check the xcerces apis and read about sax, dom parsing as of now.. google out... m sure u'll find lots of help out there...

good luck

@@CKM@@a at 2007-7-29 13:33:11 > top of Java-index,Java Essentials,New To Java...
# 2

Also try looking at JAXB as that parses the XML and gives you a Java object you can manipulate etc.

c0demonk3ya at 2007-7-29 13:33:11 > top of Java-index,Java Essentials,New To Java...
# 3

Commons Digester is also a good choice.

http://jakarta.apache.org/commons/digester/

manuel.leiriaa at 2007-7-29 13:33:11 > top of Java-index,Java Essentials,New To Java...
# 4

> hi, i am passing a string as below to my program.

> <PERSON><id>1</id><country>Australia</country><> PERSON>

> but how to read this string to get the id and

> country? i have done by write it to a file.xml and

> read it. but can it done by no need write to file and

> read it? anyone can giv me a simple example? thanks

Create a Document object from an InputStream rather than from a file.

Here's something I found:

http://www.bubble-media.com/cgi-bin/articles/archives/000038.html

public java.io.InputStream parseStringToIS(String xml){

if(xml==null) return null;

xml = xml.trim();

java.io.InputStream in = null;

try{

in = new java.io.ByteArrayInputStream(xml.getBytes("UTF-8"));

}catch(Exception ex){

}

return in;

}

PatrickFinnigana at 2007-7-29 13:33:11 > top of Java-index,Java Essentials,New To Java...