If you need to use XML with PHP, look no further than the awesome <phpXML> class written by Michael P. Mehl, available from http://www.phpxml.org.
Why? Because it allows you to use the XPath expression syntax to access data. WAY easier than mucking around with, say the DOM.
An example of XPath’s functionality: say I have a CDATA tag called “ACCOUNT” which I wish to know the value of. In XPath, this is written “//ACCOUNT”. For an awesome tutorial on using XPAth, go to http://www.zvon.org/xxl/XPathTutorial/General/examples.html
Unfortunately, there is only limited information available on how to actually use <phpXML>, and none of it is particularly user-friendly. I searched high and wide for a simple tutorial, but to no avail. So here it is!
<? include('xml.php'); $xml = new XML('test.xml'); // Evaluate the XPath expression. $zzz = $xml->evaluate('//ACCOUNT', ''); //aliter: to output all CDATA values in the XML // $zzz = $xml->evaluate('//*', ''); // Now display the content of the XML file. foreach($zzz as $result) { $final = $xml->get_content($result); echo "Value:$final
"; } $final=$xml->get_file($zzz); ?>
Here’s the example XML file (test.xml)
<?xml version="1.0"?> <!--DTD--> <!DOCTYPE TRANSDETAIL [ <!ELEMENT TRANSDETAIL (RESPONSE)> <!ELEMENT RESPONSE (ACCOUNT, GOODRESPONSE, WHYBAD, DATE, PAYMENTMETHODS)> <!ELEMENT ACCOUNT (#PCDATA)> <!ELEMENT DATE (#PCDATA)> <!ELEMENT GOODRESPONSE (#PCDATA)> <!ELEMENT WHYBAD (#PCDATA)> <!ELEMENT PAYMENTMETHODS (CREDITCARD?,CHECK?,WEB900?)> <!ELEMENT CREDITCARD (CREDITCARDATTEMPT*)> <!ELEMENT CHECK (CHECKATTEMPT*)> <!ELEMENT WEB900 (WEB900ATTEMPT*)> <!ELEMENT CREDITCARDATTEMPT (TRANS, AMOUNT, FIRSTNAME, LASTNAME, CITY, STATE, ZIPCODE, COUNTRY, EMAILADDRESS, IPADDRESS, PHONENUMBER, APPROVED, PINCODE, USERNAME, PASSWORD, WHYDECLINED, REFUNDED, CHARGEDBACK, TRANSACTIONTIME, EVENTTYPE, REVSHARERID, SUBSCRIPTIONID)> <!ELEMENT CHECKATTEMPT (TRANS, AMOUNT, FIRSTNAME, LASTNAME, CITY, STATE, ZIPCODE, EMAILADDRESS, IPADDRESS, PHONENUMBER, APPROVED, PINCODE, USERNAME, PASSWORD, WHYDECLINED, REFUNDED, CHARGEDBACK, REVOKED, TRANSACTIONTIME, EVENTTYPE, REVSHARERID, SUBSCRIPTIONID)> <!ELEMENT WEB900ATTEMPT (TRANS, AMOUNT, CODE1, CODE2, REFUNDED, TRANSACTIONTIME)> <!ELEMENT TRANS (#PCDATA)> <!ELEMENT PINCODE (#PCDATA)> <!ELEMENT USERNAME (#PCDATA)> <!ELEMENT PASSWORD (#PCDATA)> <!ELEMENT AMOUNT (#PCDATA)> <!ELEMENT FIRSTNAME (#PCDATA)> <!ELEMENT LASTNAME (#PCDATA)> <!ELEMENT CITY (#PCDATA)> <!ELEMENT STATE (#PCDATA)> <!ELEMENT ZIPCODE (#PCDATA)> <!ELEMENT COUNTRY (#PCDATA)> <!ELEMENT EMAILADDRESS (#PCDATA)> <!ELEMENT IPADDRESS (#PCDATA)> <!ELEMENT PHONENUMBER (#PCDATA)> <!ELEMENT APPROVED (#PCDATA)> <!ELEMENT WHYDECLINED (#PCDATA)> <!ELEMENT REFUNDED (#PCDATA)> <!ELEMENT CHARGEDBACK (#PCDATA)> <!ELEMENT REVOKED (#PCDATA)> <!ELEMENT TRANSACTIONTIME (#PCDATA)> <!ELEMENT EVENTTYPE (#PCDATA)> <!ELEMENT REVSHARERID (#PCDATA)> <!ELEMENT SUBSCRIPTIONID (#PCDATA)> <!ELEMENT CODE1 (#PCDATA)> <!ELEMENT CODE2 (#PCDATA)> ]> <TRANSDETAIL> <RESPONSE> <ACCOUNT>99999100</ACCOUNT> <GOODRESPONSE>YES</GOODRESPONSE> <WHYBAD>NO ERROR</WHYBAD> <DATE>01/01/1999</DATE> <PAYMENTMETHODS> </PAYMENTMETHODS> </RESPONSE> </TRANSDETAIL> <?END TRANSDETAIL?>