Hi, I am parsing XML with Java using SAX. I want to print the XML, but only to print the outer nodes not any inner nodes. What is the best way to approach this within the start element handler?
Thanks
Printable View
Hi, I am parsing XML with Java using SAX. I want to print the XML, but only to print the outer nodes not any inner nodes. What is the best way to approach this within the start element handler?
Thanks
Supply the parser with your own DefaultHandler; the parser will call your startElement method when it enters a node and call endElement when it leaves a node. Keep track of a counter and increment it when entering a node and decrement it when the parse leaves a node. Only print the node content when the counter == 1.
kind regards,
Jos
hi, thanks I have achieved this now but I have an xml file with many of one 'item' with many inner nodes. It only seems to count the first 'item' so will only print from one 'item' not all. Am I doing something wrong
e.g.
<Items>
<Item>
<example node></example node>
<example node></example node>
<example node></example node>
</Item>
<Item>
<example node></example node>
<example node></example node>
<example node></example node>
</Item>
</Items>
Can you show us some of your (relevant) code? (keep it as short as possible please).
kind regards,
Jos