Using DOM Classes


DOM provides various built-in classes that let you parse the XML documents in PHP. The PHP DOM parser represents the XML document by creating standard objects that are instances of the following classes:

  • DomDocument Class

  • DomNode Class

  • DomElement Class

  • DomText Class

  • DomAttribute Class

You can add, delete, or modify the structured content of the XML document by using various properties and methods . The DomDocument object stores the properties of an XML document, such as its name and version.

Listing 3-2 shows the contents of the DOM.xml file:

Listing 3-2: Properties of an XML File
start example
 DomDocument Object (     [name] => #document     [url] => DOM.xml     [version] => 1.0     [encoding] => UTF-8     [standalone] => -1     [type] => 9     [compression] => -1     [charset] => 1     [0] => 1     [1] => 136451608     [doc] => Resource id #0 ) 
end example
 

The above listing specifies the values of various properties of the DOM.xml file. You can display the values of the properties of the XML document using the print_r() function. The listing specifies values, such as the version, node type, and character set of the XML document. It also specifies whether the DOM.xml file is compressed or not.

The DomDocument Class

The DomDocument object is the object created after an XML document is created by the DOM parser.

Listing 3-3 shows the structure of the DomDocument class:

Listing 3-3: Structure of the DomDocument Class
start example
 class DomDocument {    Properties:    version     encoding     standalone    type    Methods:    root()     children()     add_root( $node )     dtd()     dumpmem() } 
end example
 

The above listing shows the structure of the DomDocument class, which contains various properties and methods defined by the DomDocument class.

The properties defined in the DomDocument class specify the version of the XML document, the text encoding, and the type of the document. The standalone property of the DomDocument object specifies the Boolean value whether or not the document is a standalone document. The methods provided by the DomDocument class are:

  • root() : Returns the root element.

  • children() : Returns the child node of the document.

  • addroot() : Creates a new document element and returns the DomElement object.

  • dtd() : Returns the Document Type Definition (DTD) object. The DTD object contains the basic information of the document and encapsulates properties, such as systemId and name. The systemId property contains the file name of the DTD document.

  • dumpmem() : Stores the XML structure in a string variable.

Listing 3-4 shows how to parse an XML string using the xmldoc() function:

Listing 3-4: Parsing the XML string
start example
 <?php $xml_str = "<?xml version='1.0'?><name>John</name>"; if (!$doc=xmldoc($xml_str)) {   die("Error in XML"); } else if ($doc->version > 1.0) {    die("Unsupported XML version"); } else {    echo "No Error"; } ?> 
end example
 

The above listing parses the string, John, using the xmldoc() function. The xmldoc() function accepts the XML string as an argument and generates the DOM object that represents the XML data.

You can also create the DomDocument object of an XML file. The syntax to create the DomDocument object using the xmldocfile() function is:

 $doc = xmldocfile("<filename>"); 

In the above code, the xmldocfile() function creates an object of the DomDocument class that represents the XML file.

Listing 3-5 shows how to parse an XML file using the xmldocfile() function:

Listing 3-5: Parsing XML File
start example
 <?php $xml_str = "/var/www/html/vishi/test.xml"; if (!$doc = xmldocfile($xml_str)) {    die("Error in XML"); } else if ($doc->version > 1.0) {    die("Unsupported XML version"); } else {    echo "No Error in Parsing the XML file"; } ?> 
end example
 

The above listing parses the xml file, test.xml, using the xmldocfile() function.

The DomNode Class

The DomNode class contains properties, such as name, content, and type of the node. The name property specifies the tag name of the node. The content property specifies the contents stored in the node. The type property represents the integer that refers to the object type. The DomNode class also encapsulates various methods, such as the lastchild(), children(), and parent() methods.

Listing 3-6 shows the structure of the DomNode class:

Listing 3-6: Structure of the DomNode Class
start example
 class DomNode {    properties:    name    content    type    methods:    lastchild()     children()     parent()     new_child( $name,$content )     getattr( $name )     setattr( $name,$value )     attributes() } 
end example
 

In the above listing, the structure of the DomNode class is defined. The listing defines various properties and methods that are encapsulated in the DomNode class. The methods defined in the DomNode class are:

  • lastchild() : Returns the last child node for the node.

  • parent() : Returns the parent node.

  • children() : Returns an array of child nodes.

  • new_child() : Adds a new DomNode to its children.

  • attributes() : Returns the array of the DomAttribute objects.

  • getattr() : Retrieves the attributes from the XML document.

  • setattr() : Sets the attribute value.

The DomElement Class

The DomElement class defines elements of the XML document. A DomElement object represents the name and type of an element of the XML document. The methods contained in the DomElement class are:

  • tagname () : Returns the element name. The syntax is:

     string DomElementName->tagname(void); 
  • get_attribute() : Returns the name and value of the attribute in the element. If no attribute is specified with the method, it returns an empty string. The syntax of the get_attribute() method is:

     objectDomElement ->get_attribute(string); 
  • set_attribute() : Adds a new attribute to the node. The syntax of the set_attribute() method is:

     bool DomElement->set_attribute ( string name, string value) 
  • remove_attribute() : Removes an attribute from the element structure. The syntax of the remove_attribute() method is:

     bool DomElement->remove_attribute (string); 
  • children() : Returns an array of the DomElement object containing the child nodes of the element.

  • parent() : Returns a DomElement object that is the parent of a node.

  • last_child() : Returns the last entry of a node child. If no last child is found in the DOM tree, a NULL value is returned.

  • attributes() : Returns an array of the DomAttribute objects that represent the attributes of a node.

You can distinguish the node type using the type property that contains specific integer values for each node.

Table 3-2 lists various predefined node types that identify the nodes:

Table 3-2: DOM Node Types

Node Type

Specification

Integer Value

XML_ELEMENT_NODE

Specifies an element.

1

XML_ATTRIBUTE_NODE

Specifies an attribute.

2

XML_TEXT_NODE

Specifies text.

3

XML_ENTITY_REF_NODE

Represents an entity reference.

5

XML_PI_NODE

Represents a processing instruction.

7

XML_COMMENT_NODE

Represents a comment.

8

XML_DOCUMENT_NODE

Represents the XML document.

9

XML_NOTATION_NODE

Represents the notation node.

12

XML_CDATA_SECTION_NODE

Represents the cdata section.

4

You can identify the elements of the XML documents using the methods of the DomElement class. You can also count the number of nodes or elements contained in an XML document.

Listing 3-7 shows how to create an XML file that stores employee information:

Listing 3-7: Creating an XML File
start example
 <?xml version="1.0" encoding="UTF-8"?> <EMPLOYEEINFORMATION> <EMPLOYEE> <NAME ID="E001">George</NAME> <AGE>35</AGE> <DEPARTMENT>RESEARCH AND DEVELOPMENT</DEPARTMENT> <DESIGNATION>BRANCH MANAGER</DESIGNATION> </EMPLOYEE> <EMPLOYEE> <NAME ID="E002">John</NAME> <AGE>45</AGE> <DEPARTMENT>HUMAN RESOURCE</DEPARTMENT> <DESIGNATION>MANAGER</DESIGNATION> </EMPLOYEE> </EMPLOYEEINFORMATION> 
end example
 

The above listing shows how to create an XML file containing employee information. Employee information, such as employee name, employee ID, and employee designation, is stored in the emp.xml file. You can generate the DOM tree using the DOM parser that represents the structure of the XML document.

Listing 3-8 shows how to use the classElement objects to generate the structure of the emp.xml file:

Listing 3-8: Structuring an XML Document
start example
 <?php $xmlfile = "emp.xml"; if (!$doc = xmldocfile($xmlfile)) {    die("Error in XML document"); } $root = $doc->root(); $children = getChildren($root); $count = 1; printTree($children); function printTree($nodeCollection) {    global $count;    echo "<ul>";    for ($t=0; $t<sizeof($nodeCollection); $t++)    {       $count++;       echo "<li>". $nodeCollection[$t]->tagname;       $nextCollection = getChildren($nodeCollection[$t]);       printTree($nextCollection);    }    echo "</ul>"; } function getChildren($node) {    $temp1 = $node->children();    $store = array();    for ($t=0; $t<sizeof($temp1); $t++)    {       if ($temp1[$t]->type == XML_ELEMENT_NODE)       {          $store[] = $temp1[$t];       }    }    return $store; } echo "Total number of elements in document: $count"; ?> 
end example
 

In the above listing, the DOM tree of the emp.xml file is generated. The root node is obtained using the root() method. After retrieving the root node of the document, the listing generates other node elements and attributes. The total number of elements in the XML document is also calculated in the above listing. Figure 3-3 shows the output of Listing 3-8:

click to expand: this figure shows the dom tree structure of the emp.xml document, displaying the nodes and elements of the xml document. the figure also shows the number of elements contained in the xml document.
Figure 3-3: The DOM Tree

The DomText Class

You can represent the character data in an XML document using the DomText class. The syntax of the DomText class is:

 DomText Object {    [type]    [content]; } 

In the above syntax, the type property specifies the node type, such as XML_TEXT_NODE. You can specify either the node number or the node name with the type. The content property stores the character data. The DOM parser identifies the whitespaces as character data and creates DomText objects for the whitespaces.

Listing 3-9 shows how to retrieve the character data from an XML document using the DomText object:

Listing 3-9: Using the DomText Object
start example
 <?php $xml_str = "<?xml version='1.0'?> <STUDENT> <NAME>John</NAME> <NAME>Ronan</NAME> <ID>OO2</ID> <NAME>Daniel</NAME> <ID>003</ID> <NAME>Mark</NAME> <ID>004</ID> <NAME>William</NAME> <ID>005</ID> </STUDENT> "; $data = array(); if(!$doc = xmldoc($xml_str)) {  die("Error parsing XML"); } $root = $doc->root(); $nodes = $root->children(); foreach ($nodes as $t) {    $text = $t->children();    if($text[0]->type ==XML_TEXT_NODE)    {        if ($text[0]->content != "")        {          $data[] = $text[0]->content;        }    }  } print_r($data); ?> 
end example
 

The above listing retrieves the text data from an XML document. The DOM parser creates an array to hold the name and the ID of the students. The foreach loop iterates the <STUDENT> elements and adds the character data found while traversing through the DOM tree in the, $data array. Figure 3-4 shows the output of Listing 3-9:

click to expand: this figure shows the output obtained after executing the above listing. the figure shows the data retrieved from the xml document.
Figure 3-4: The Retrieved Character Data

The DomAttribute Class

The DomAttribute class creates the attr object that returns the attribute of an element.

Listing 3-10 shows the structure of the DomAttribute class:

Listing 3-10: Structure of the DomAttribute Class
start example
 class DomAttribute {    Properties    name    value    Method     name() } 
end example
 

The above listing specifies the method and properties of the DomAttribute class. The name property sets the attribute name. The value property specifies the value stored in the attribute node.

Listing 3-11 explains the DomAttribute class:

Listing 3-11: Using the DomAttribute Class
start example
 <?php $xml_string = "<?xml version='1.0'?> <EmployeeInfo> <NAME ID='E001'>George</NAME>, <NAME ID='E002'>John</NAME>, <NAME ID='E003'>Angel</NAME> </EmployeeInfo>"; if (!$doc = xmldoc($xml_string)) {    die("Error in XML document"); } // get the root node $root = $doc->root(); // get its children $children = $root->children(); // Iterate through child list for ($x=0; $x<sizeof($children); $x++) {    if($children[$x]->type == XML_ELEMENT_NODE)    {       // Retrieving the text        $text = $children[$x]->children();       $cdata = $text[0]->content;       if($children[$x]->get_attribute("ID"))       {          echo "<br><Employee=" . $children[$x]->get_attribute("ID").">" .          $cdata .          "</Employee></br>";       }       else       {          echo $cdata;       }    }    // if text node    else if ($children[$x]->type == XML_TEXT_NODE)    {       // simply print the content       echo $children[$x]->content;    } } ?> 
end example
 

The above listing shows how to implement the DomAttribute class. In the above listing the get_attribute() function retrieves the attribute value of the attribute nodes of the DOM tree. The above listing parses the XML document and then identifies the root and child nodes of the XML document. The DOM parser iterates through the child nodes and verifies the presence of the element node in the DOM tree. The listing retrieves the text node and the contents stored in it. Figure 3-5 shows the output of Listing 3-11:

click to expand: this figure shows the name of the employees.
Figure 3-5: Output of using the DomAttribute Class



Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net