Search code examples
javasax

Reading an XML using SAXParser


I want to read such a file:

<level1 a="1" b="2" c="3">
  <level2 a2="1" b2="2" c2="3">
    <level3 a3="1" b3="2" c3="3">
       <level4 a4="1" b4="2" c4="3">
    </level3>
  </level2>
  <level2a a2a="1" b2a="2" c2a="3">
    <level3a>
      <level3 id="0"/>
      <level3 id="2"/>
      <level3 id="7"/>
      <level3 id="11"/>
    </level3a>
  </level2a>
</level1>    

Note: level3 is actually used twice in different contexts

I have some pseudocode mixed up where I wish to use SAXParser in Java:

create new parser
loop to get tags
if tag is level 1
get its properties(a b and c)
save the values of them into level1_a,level1_b,level1_c
if tag is level 2
...
if ..
...
end loop
done

But i have never used it and I'm not clear onto how it works, can you help me?


Solution

  • Using SAX parsing, you will need to handle a context on your side. You issue here is to differentiate level3 node when it is below level2 or below level3a. The best way is to handle a stack while decoding, of what is the current node you are in:

    • push node (name?) on the stack after a startElement
    • pop top node (name?) after the endElement.

    In your pseudo code, your "loop to get tags" is actually a SAX callback that you register to the SAXParser. Each time you start it you can also push the node name into a Stack. Then when reading a level3 node, you simply have to read the name of the top stack node to know if you need to read id or the triplet a3 b3 c3 attributes in current node.