1

I was going through this tutorial and I noticed that startElement method is called twice but i do not see any method call for this... it seems to be happening automatically... can you explain to me how this method is called ?

user3134565
  • 965
  • 1
  • 7
  • 14

2 Answers2

1

The callback method is called by the Parser Object when it reaches the start of an object. For example, to parse an xml file using an SAX parser you will have :

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
saxParser.parse(new File(sourceFile), this);

where "this" refers to the current class that implements the interface ContentHandler. We override the callback methods such as startElement, and they will be called when your saxParser reads through certain events.

also please refer to this page about callback functions if interested.

Community
  • 1
  • 1
0

startElement is started when new tag occurs, and when you close this tag endElement is called. So if you have something like this:

<jobs>
      <job>
          <id>4</id>
          ...
          ...
    </job>
</jobs>

first xml parser opens jobs tag and then job. When he finish, he calls first job and then jobs

deadfish
  • 11,996
  • 12
  • 87
  • 136