Sign in to follow this  
anda

(Java) reading file with XML DOM problem

Recommended Posts

Never mind it's solved. Hi, I'm trying to read an xml file that looks like:
<root>
   <item>
      <someinfo>a</someinfo>
   </item>
   <item>
      <someinfo>a</someinfo>
      <otherinfos></otherinfos>
   </item>
   <item>
      <someinfo>b</someinfo>
      <otherinfos>
         <otherinfo>otherstuff</otherinfo>
      </otherinfos>
   </item>
   <item>
      <someinfo>b</someinfo>
      <otherinfos>
         <otherinfo>otherstuff2</otherinfo>
         <otherinfo>otherstuff3</otherinfo>
      </otherinfos>
   </item>
</root>


I don't know how to access the 'otherinfo' nodes. Heres my code so far:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("myfile.xml");

NodeList itemList = doc.getElementsByTagName("item");

for(int i=0;i<itemList.getLength();i++) {
   Element someinfoElement = (Element)doc.getElementsByTagName("someinfo").item(i); //probably not the best way to read nodes.

   //now how to check if an 'item' node contains any 'otherinfo' nodes and then read them?
}


Thank you. [Edited by - anda on May 14, 2008 12:24:22 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this