For PHP XML support accessibility standards evolved, along with subsequent versions. For version 3 was available SAX interface, which has not changed up to now (compatibility issues may be related only to support the namespace for the elements, if you have the libxml2 library earlier than version 2.6).
In Version 4 added support for DOM (domxml extension) and XSLT (xslt extension). The first of these, however, did not meet the W3C guidelines, resulted in a lot of memory leaks, which was not too popular. The second requires the Sablotron library activities or Expat.
breakthrough resulted in version 5 Support for DOM has been rewritten, in accordance with the standards and placed in the W3C DOM extension. Added support for XPath and DTD validation according to the file (the link contained in a XML file), XSchema and RelaxNG (file name defined in the function call).
was based on XSLT libxslt library (the fastest on the market) and placed in the XSL extension. Both also gained
truly object-oriented nature. Moreover
added SimpleXML interface for easy modification of XML file without a significant burden system characteristic of the DOM.
SAX
SAX parser by calling the create xml_parser_create (); The next step is to create functions corresponding to the XML events and registering them. At http://pl.php.net/manual/en/ref.xml.php # id7330424 available example of such a parser (including the verification of external entities.)
DOM and SimpleXML
For PHP 5 using the DOM is the same as in other programming languages \u200b\u200b(JavaScript, Java, ...). Work with him start by creating a DOMDocument object. SimpleXML
other hand, is a very interesting intefejsem. Based on the XML file it creates something like a PHP class stdClass (for other object-oriented languages, you can compare it to the class which is a collection of objects). Thanks to that refer to an element in a very pleasant way (Zend intefejs used this to create Zend_Config_Xml).
worth noting that, by function and dom_import_simplexml simplexml_import_dom can easily convert data from one standard to another (this is particularly useful for saving the XML document to a file, which is a SimpleXML function does not).
Example
Finally, I present an example that retrieves data from the Customers table (the base Sample XML for IBM DB2 9) and draw from them the name of the client. I want to draw attention to two elements. First, differences in the volume of SimpleXML and DOM performing the same operation. Secondly, it is worth noting that the methods of class DOMDocument in PHP and Java Document for the same:
Summary
error_reporting (E_ALL
$port = 50000;
$connect_string = "DRIVER={IBM DB2 ODBC DRIVER};" .
"DATABASE=$database;" .
"HOSTNAME=$hostname;" .
"PORT=$port;" .
"PROTOCOL=TCPIP;" .
"UID=$user;" .
"PWD=$password;";
$connection = db2_connect($connect_string,'','');
if ($connection){
echo "Connection successful.<br />";
$sql = "SELECT INFO FROM CUSTOMER WHERE CID = ?";
$statement = db2_prepare($connection, $sql);
db2_execute($statement,array(1001));
while ($customer = db2_fetch_object($statement)){
$info = $customer->INFO;
//SimpleXML
$simpleXML = new SimpleXMLElement($info);
echo $simpleXML->name."<br />";
//DOM
$domDocument = new DOMDocument('1.0','UTF-8');
$domDocument->loadXML($info);
$names = $domDocument->getElementsByTagName('name');
foreach ($names as $name) {
echo $name->nodeValue.'<br /> ';}
}
db2_close ($ connection);
} else {echo "Connection failed."}
shown in both examples represent only tip of the iceberg relating to the processing of XML documents in Java and PHP. However, I can be a useful prelude to further exploration.
All kinds of comments and suggestions are welcome
0 comments:
Post a Comment