Appendix C: The PHP.XPath Class


PHP.XPath is a Hypertext Preprocessor (PHP) class that searches and retrieves data from an eXtensible Markup Language (XML) document using XPath. The PHP.XPath class uses the Document Object Model (DOM) Application Programming Interface (API) to modify an XML document.

This appendix introduces the PHP.XPath class, and describes the types of objects in this class. It also explains the public and private methods of the objects in the PHP.XPath class.

Introducting the PHP.XPath Class

The PHP.XPath class accepts XML data as its argument, parses the data, and creates an object that represents XML data. This object uses the methods defined in the PHP.XPath class to create XML nodesets on the basis of XPath expressions. You can add, delete, or edit the nodes from an XML document tree, and retrieve specific information from within an XML document.

If the PHP module on your server is not compiled with the XML library, you can use the PHP.XPath class to provide XML functionality. The PHP.XPath class consists of objects of three base classes, which are:

  • XpathBase: Contains debugging functions.

  • XpathEngine: Contains XML import and export methods. The XpathEngine base class is made up of the XPath specification.

  • Xpath: Contains methods that modify an XML document.

Note  

You need not install the DOM XML PHP library to modify an XML document using the PHP.XPath class.

The XPathBase Class

The XPathBase class consists of public and private methods. A public method declared in a class is accessible to other classes. The public methods in the XPathBase class are:

  • XPathBase() method : Represents the constructor of the XPathBase class.

  • reset() method : Resets the XPathBase class object and enables the object to accept new XML data.

  • setVerbose() method : Alters the details of error level reporting in an XML document. The syntax of the setVerbose() method is:

 setVerbose($level=1) 

In the above syntax, the $level parameter is set to the default value, 1. To turn off the error-level reporting, set the value of the $level parameter to 0. If the $level parameter has an integer value greater than 1, error-level reporting is turned on.

  • getLastError() method : Returns the recent error message as a string value. This method returns a null value if there is no error message.

Note  

The higher the value of the $level parameter, the higher is the level of error.

A private method is not accessible outside the class definition. The private methods in the XPathBase class are:

  • _searchString() method : Searches a string within another string in an XML document. The syntax of the _searchString() method is:

     _searchString($term, $expression) 

In the above syntax, the $expression parameter denotes the string to be searched; and the $term parameter indicates the string in which the _searchString() method searches the specified expression.

The _searchString() method returns an integer value. It returns the offset value at which the string is found. The _searchString() method returns “1, if the $expression parameter is not a substring of the string in the $term parameter.

  • _bracketsCheck() method : Checks if there is an ending bracket corresponding to each starting bracket in an XPath expression. The syntax of the _bracketsCheck() method is:

     _bracketsCheck($term) 

In the above syntax, the $term parameter denotes the XPath string, in which the _bracketsCheck() method checks for a matching bracket.

  • _prestr() method : Retrieves the substring positioned before a delimiter . The syntax of the _prestr() method is:

     _prestr($string, $delimiter, $offset=0) 

In the above syntax, the $string parameter denotes the string from which the _prestr() method retrieves a substring. The $delimiter parameter denotes the string that contains the delimiter for the _prestr() method. If the $offset parameter is set to 0, the method searches the $string string, starting from the first character, until it finds the delimiter for the first instant.

If the $offset parameter is set to a value other than 0, the _prestr() method searches the $string string from the specified offset value. The _prestr() method returns a substring that precedes the specified delimiter in the original string. An example of the _prestr() method is:

 _prestr(Park:Town, 


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