0

As you can see in the below xml there are two duplicate fields or nodes called condition (one in <current_conditions> and the other in <forecast_conditions>. so whenever i use the startelement method to read the condition I always get the last last one but i want to read from the first node only).

<current_conditions>
<condition data="Mostly Cloudy"/>
<temp_f data="86"/>
<temp_c data="30"/>
<humidity data="Humidity: 25%"/>
<icon data="/ig/images/weather/mostly_cloudy.gif"/>
<wind_condition data="Wind: SE at 7 mph"/>
</current_conditions>

<forecast_conditions>
<day_of_week data="Sun"/>
<low data="73"/>
<high data="97"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>

I can I do that. I am using SAX parser to parse the document and here is the code for the handler I am using

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
   if (localName.equals("condition")) {
     String data=attributes.getValue("data");
}}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Nav
  • 10,304
  • 20
  • 56
  • 83
  • oops it didn't update properly let me repaste it – Nav Apr 15 '12 at 17:41
  • Perhaps this almost exact same [question](http://stackoverflow.com/questions/5796041/selecting-specific-xml-tags-with-sax-parser) (which even uses the same weather-related XML) might help. – Ken White Apr 15 '12 at 17:51
  • yup thank you Ken I do agree that the link to that question is really helpful but at the same time I was thinking if there was a more graceful solution to this problem rather than rewriting the reading logic with lots of if else statements. As this is only a simple example I gave but think about when the number of duplicate fields is large or when the outer nodes are dynamic and the internal nodes are duplicate – Nav Apr 15 '12 at 18:04

1 Answers1

0

couldn't find one so had to do it the hard way ....wrote the logic myself with modifications to the link provided by Ken...

Nav
  • 10,304
  • 20
  • 56
  • 83