Sign in to follow this  

(Java) reading file with XML DOM problem

This topic is 3502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

This topic is 3502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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