Re: Help Reading XML file
On 2006-11-01 15:01:18 -0800, petedawn@gmail.com said:
i have got this so far but it only reads the first level and doesnt go
any deeper.
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("configuration.xml"));
doc.getDocumentElement ().normalize ();
Node n;
NodeList nodes = doc.getDocumentElement().getChildNodes();
for( int i=0 ; i<nodes.getLength(); i++ )
{
n = nodes.item( i );
if( n.getNodeType() == Node.ELEMENT_NODE )
{
if( n.getNodeName().equals( "b" ) )
{
NamedNodeMap attrs = n.getAttributes();
String s = attrs.getNamedItem("name").getNodeValue();
String s2 =
attrs.getNamedItem("value").getNodeValue();
}
}
}
i am able to read the first bit, which is <b name="test0" value="0">
but how do i get to the next level .thanks.
Hi Pete. I see you're trying to build a DOM tree in memory. I -may-
be able to assist, but I should self-disclos that I have more practical
experience using JDOM (which is not to say I'm an expert by any means!).
Can you explain what you're trying to do? Is a DOM tree better for you
than sequentially parsing the file using SAX? How large is the XML
file? Do you need to bind data structures the information contained
within the XML?
Kevin