I have some trouble understanding parsing XML structures with SAX. Let’s say there is the following XML:
<root>
<element1>Value1</element1>
<element2>Value2</element2>
</root>
and a String variable myString.
Just going through with the methods startElement, endElement() and characters() is easy. But I don’t understand how I can achieve the following:
If the current element equals element1 store its value value1 in myString. As far as I understand there is nothing like:
if (qName.equals("element1")) myString = qName.getValue();
Guess I’m just thinking too complicated 🙂
Robert
With SAX you need to maintain your own stack. You can do something like this for very basic processing:
If you want code as in your example then you need to use the DOM model rather than SAX. DOM is easier to code up but is generally slower and more memory expensive than SAX.
I recommend using a third-party library rather than the built-in Java XML libraries for DOM manipulation. Dom4J seems pretty good but there are probably other libraries out there too.