Deal with missing tags in XML file

You must Login before you can answer or comment on any questions.

Hey guys, I need to read information from a xml file. However, in this file, for some nodes, it contains more subchildnodes than other nodes, here's the file:

<?xml version="1.0" encoding="utf-8"?>
<Green>
<ROWSET>
 <ROW>
   <A>text goes here</A>
   <B>text goes here</B>
   <C>text goes here</C>
 </ROW>
 <ROW>
   <A>text goes here</A>
   <B>text goes here</B>
 </ROW>
 <ROW>
   <A>text goes here</A>
   <B>text goes here</B>
   <C>text goes here</C>
 </ROW>
</ROWSET>
</Green>
Note that the 1st&3rd "ROW" has tag <C></C> while the 2nd doesn't. So I use Xpath and successfully recognize the missing tag in the 2nd <ROW> tag. Here's my code:
var row1 = xmlDoc.evaluate("/GreenSpot/ROWSET/ROW["+1+"]/C/text()")?"row1 exist!":"row1 NOT exist!";
var row2 = xmlDoc.evaluate("/GreenSpot/ROWSET/ROW["+2+"]/C/text()")?"row2 exist!":"row2 NOT exist!";
var row3 = xmlDoc.evaluate("/GreenSpot/ROWSET/ROW["+3+"]/C/text()")?"row3 exist!":"row3 NOT exist!";
So I get "row1 exist! row2 NOT exist! row3 exist!". This works for iPhone. When I put the same code on Android, it says "row2 exist!". I have also try
xmlDoc.documentElement.getElementsByTagName("TEST").item(n)
When n=3, it throws nullPointerError. But what I want is when n=2, I can know the 2nd doesn't have <C></C>.
Is there any way to do this on android?

— asked 2 years ago by Shane Huang
1 Comment
  • If you have a variable number of elements in your rows it would probably be wiser to iterate through the children of those rows, rather than try to XPath directly to them.

    — commented 2 years ago by Tony Lukasavage

Your Answer

Think you can help? Login to answer this question!