PPT Slide
3.2 DOM : Document Object Model
- The SAX interface allows you to parse an XML file and execute particular actions whenever certain structures (like tags) appear in the input. This is great for a lot of applications, however you might want to completely alter whole sections of the tree structure, restructure or completely build from scratch an Object structure and then save the whole thing in XML format.
- For this purpose we use the DOM API, where instead of reading XML and carrying-out call backs to a user-defined class, a file as a tree of objects is returned. Note at this point we have checked the file for formatting and semantic validity and built a complete hierarchical object structure which exactly represents the XML file. We may then add further nodes and leafs, update information, move information, delete information and when we are content with our alterations, we can then tell the top node to print itself to another XML file, and the new document object is created.
- Methods such as these are available to effectively navigate and alter the XML architecture :
-
Nodes Elements
- getNodeType() createElement()
- getNodeValue() createTextNode()
- getParentNode() createCDATASection() (for DTDs)
- getChildNodes() createAttribute()
- setNodeValue() createEntityReference()
SAX and DOM are really quite large APIs and should therefore studied as a separate exercise.