-5

I know how to parse XML with a SAX parser. My XML is like this:

-<address_component>
   <long_name>Chambers St</long_name>
   <short_name>Chambers St</short_name>
   <type>route</type>
</address_component>
-<address_component> 
   <long_name>Downtown</long_name>
   <short_name>Downtown</short_name>
   <type>neighborhood</type>
   <type>political</type>
</address_component>

My problem: When XML has two tags with same name, like 'type' in this case, my code only gets the value of last tag. In this case, 'type' gets "political" and it skips "neighborhood".

How do I deal with this?

2

3 Answers 3

2

It sounds like your code is not designed to handle multiple type elements, so it only saves the last one it encounters. That is a limitation of your code (which you have not shown). You need to re-write the code to accept multiple type elements, such as by storing every type encountered into a String[] array instead of a single String variable.

0

XPath maybe (not sure). Check this out (it's for python but it's generic enough): How to make XPath select multiple table elements with identical id attributes?

0

Considering, that you are, indeed, using a SAX parser, your callbacks (startElement/endElement, etc) should fire for both 'type' elements. Perhaps, if you could post some code, the problem would become more apparent.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.