Parsing XML

Well today is the firs today I'm working with XML in Java, and I managed to get pretty far [ in my opinion. ] However, I have seem to hit a stumbling block that I haven't been able to surmount.

I have a very basic XML document [ stats.xml ]

<?xml version='1.0' encoding='utf-8'?>

<stats>

<item>Sidewinder</item>

<item>11-45-88</item>

</stats>

And my Java code parses the xml and prints:

Sidewinder

11-45-88

To the console, however I want each element to be stored in a list / array and I haven't been able to figure out how to do that. Here is my code:

package test;

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

publicclass ParseXML{

staticpublicvoid main(String[] arg){

try{

String xmlFile ="test/stats.xml";

File file =new File(xmlFile);

if(file.exists()){

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(xmlFile);

NodeList list = doc.getElementsByTagName("*");

Element element = (Element)list.item(0);

System.out.println(element.getTextContent());

}else{

System.out.print("File not found!");

}

}catch (Exception e){

System.exit(1);

}

}

}

If anyone could help me, I would appreciate it.

Thank you.

[2392 byte] By [SidewinderXa] at [2007-11-27 10:19:27]
# 1

> To the console, however I want each element to be

> stored in a list / array and I haven't been able to

> figure out how to do that.

Can u elaborate more on this? n by the way, lots n lots of dom example are available on the internet... check this out:

http://www.exampledepot.com/egs/org.w3c.dom/pkg.html

@@CKM@@a at 2007-7-28 16:57:46 > top of Java-index,Java Essentials,Java Programming...
# 2

Well lets say this is my XML file:

<?xml version = "1.0" ?>

<Employee-Detail>

<Employee>

<Emp_Id> E-001 </Emp_Id>

<Emp_Name> Vinod </Emp_Name>

<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>

</Employee>

</Employee-Detail>

The code above would print

E-001

Vinod

Vinod1@yahoo.com

To the console. I want each value to be stored in an array. ie)

value[0] = E-001

value[1] = Vinod

value[2] = Vinod1@yahoo.com

Obviously there needs to be a loop / iterator but the getTextContent method just returns every value in a single string and I don't know how to iterate through that.

SidewinderXa at 2007-7-28 16:57:46 > top of Java-index,Java Essentials,Java Programming...
# 3

Well I got a bit farther, managed to do what I want in my "test" file, however the real implementation will have hundreds of elements, and i really dont feel like having to use my code because I would have to write 3 lines of code for every element in my xml doc. Is there anyway I can make the endElement method into a loop so I dont have to statically add every element?

package xml;

import java.io.CharArrayWriter;

import java.io.FileReader;

import org.xml.sax.Attributes;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.helpers.XMLReaderFactory;

public class parseXML extends DefaultHandler {

public static String[] stats = new String[50];

private static CharArrayWriter contents = new CharArrayWriter();

public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {

contents.reset();

}

public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {

if ( localName.equals( "Emp_Id" ) ) {

stats[0] = contents.toString();

}

if ( localName.equals( "Emp_Name" ) ) {

stats[1] = contents.toString();

}

if ( localName.equals( "Emp_E-mail" ) ) {

stats[2] = contents.toString();

}

}

public void characters( char[] ch, int start, int length ) throws SAXException {

contents.write( ch, start, length );

}

public static void main( String[] argv ){

try {

XMLReader xr = XMLReaderFactory.createXMLReader();

parseXML pxml = new parseXML();

xr.setContentHandler( pxml );

xr.parse( new InputSource(new FileReader("doc.xml")));

for(int i = 0; i < 3; i++){

System.out.println( stats[i]);

}

}catch ( Exception e ) {

e.printStackTrace();

}

}

}

SidewinderXa at 2007-7-28 16:57:46 > top of Java-index,Java Essentials,Java Programming...
# 4

I don't know about a "loop". You could certainly have a Map where the key was the element name and the value was the text content of the element, that way you wouldn't have to hard-code any element names at all.

DrClapa at 2007-7-28 16:57:46 > top of Java-index,Java Essentials,Java Programming...
# 5

How would one go about doing that? :)

SidewinderXa at 2007-7-28 16:57:46 > top of Java-index,Java Essentials,Java Programming...