How to Process XML using DOM
Document object Model (DOM) for parsing an XML document is essentially just a
method for turning your XML document into an object tree. Because all XML documents
are essentially built like a tree, accessing an individual element by its branch
seems a logical step.
The XML : : Grove module provides an easy way to work with an entire XML document by loading an XML document into memory and then converting it into a tree of objects that can be accessed just like any other set of nested references. To demonstrate the tree format offered by XML: Grove lets look at a sample XML document.
Well use a contact entry within an address book, a structure most people are familiar with. If we think about a single record within a contact database, then the base of the XML document will be the contact. Well use a fictional version of me for our example.
A contact Record Written in XML
The grove. Pl sample script that comes with the XML: Grove module kit can convert this document into a textual tree. This version has been modified slightly so that it also output the array reference number of each branch.
Because we can access individual tags within a DOM-parsed XML document, DOM parsers are particularly useful when we want to update the contents of an XML document. Using SAX to process the document sequentially rather than using the tree model offered by a DOM parser is far from ideal, because it means reading in the content, identifying which bits you want to change as they are triggered, and then regenerating the result.
For example, if we wanted to update my holiday Chalet address using SAX, wed have to read in the content, identify first that we were in the address branch, and then that were we in the correct addressline branch. Then we could replace the information in the output.
Using DOM, we parse the entire document; update the address within the branch we want to update, and then dump the XML document back out again. Updating the branch is just a case of referencing the branchs location within the DOM structure.
XML: Grove converts your XML document into a series of nested arrays and hashes. The arrays contain a list of elements within the current branch, and the hashes are used to soppy the element type, name, and data (if applicable) for that branch. Because there are different element types, the numbers dont always match what you would normally expect.
listing you will notice the array reference numbers required to accesses each
branch. To access the contents of a branch, you access the contents element form
the enclosed hash and get the data continued in a branch using the Data key. Finally
the name key returns the tag name for a given branch, and the attributes key returns
the attributes for the tag.
Copyright - © 2004 - 2018 - All Rights Reserved.