how to read XML element and its value?

i am parsing text in xml format to java. i search from internet, mostly example is create a xml file to keep the text then read from it. but i dun want to create a xml file to keep the text but parse directly to java. are there have any solution about it? thanks for your help.

below is the example of file i parse to java

<ID>001</ID><AREA>city</AREA><COUNTRY>US</COUNTRY>

[410 byte] By [gracecheaha] at [2007-11-27 10:40:20]
# 1

Check the Javadoc for DocumentBuilder. There is a parse() methods that should do what you want.

sabre150a at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 2

yes, what u said is someting like this right?

String strFile = info.xml;

File ff = new File(strFile);

if (ff.exists()){

ff.delete();

}

FileOutputStream tempFile = new FileOutputStream( info.xml);

tempFile.write( strTrimMsg.getBytes());

tempFile.flush();

DocumentBuilderFactory docBF = DocumentBuilderFactory.newInstance();

DocumentBuilder Bldr = docBF.newDocumentBuilder();

Document Doc = Bldr.parse(new FileInputStream(info.xml) );

Element Root = Doc.getDocumentElement();

String strRoot = Root.getTagName();

if( strRoot.compareTo( "ID" ) ==0 ){

}

but code above wl build a doc in our directory right?besides of build another file in ourdiretory, are there hav another way to read the text in xml format?what i need is directly read the text i pass in where the text is in xml format without build another document to keep it and read from it. thanks

the text i pass in is

<ID>001</ID><AREA>city</AREA><COUNTRY>US</COUNTRY>

gracecheaha at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 3

> yes, what u said is someting like this right?

No! I said look at the Javadoc for the DocumentBuilder.parse() methods!

P.S. There are other ways one can convert a String to an InputStream without writing it to a file first.

Message was edited by:

sabre150

sabre150a at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 4

i am not comfortable with what u write before u edit ur reply. anyway, thanks

gracecheaha at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 5

> i am not comfortable with what u write before u edit

> ur reply. anyway, thanks

Since you did not post this in 'New To Java Programming' I assumed more knowledge than you had.

A ByteArrayInputStream is an InputStream. A ByteArrayInputStream can be constructed from a byte array (byte[]). A byte array can be constructed from your XML String using

byte[] XMLBytes = xmlString.getBytes(encoding);

You can choose any encoding you want as long as you create an XML header that mirrors this encoding. For example, "utf-8" for both.

sabre150a at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 6

do u hav example code?

gracecheaha at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 7

InputStream is = new ByteArrayInputStream(xmlString.getBytes("utf-8"));

sabre150a at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...
# 8

thanks

gracecheaha at 2007-7-28 19:06:13 > top of Java-index,Java Essentials,Java Programming...