Click to See Complete Forum and Search --> : Help Parsing XML with PHP


wjaram
05-09-2009, 10:17 PM
I need some help parsing XML with PHP. Below is a sample of the XML I'm using.

<?xml version="1.0" encoding="UTF-8"?>
<response>
<requestinformation code="100">
<codemessage>RECORDS FOUND</codemessage>
<inputs>
<firstname>test</firstname>
<lastname>test</lastname>
<dob year="1976" month="12" day="2"/>
<ssn>123456789</ssn>
<state/>
<addressinfo type="Current">
<streetnumber/>
<streetname/>
<unitnumber/>
<municipality/>
<state/>
<postalcode/>
</addressinfo>
<addressinfo type="Previous">
<streetnumber/>
<streetname/>
<unitnumber/>
<municipality/>
<state/>
<postalcode/>
</addressinfo>
<endusercode>ABC</endusercode>
<enduserstate>FL</enduserstate>
<reportusestate>FL</reportusestate>
<permissiblepurpose>00</permissiblepurpose>
<version>2.0</version>
<product>Essential Report</product>
<quoteback/>
</inputs>
</requestinformation>
<addressinformation>
<ssn-validation>
<message>SSN is valid.Issued in Florida</message>
<year>Issued In Year 1959 And 1960</year>
</ssn-validation>
<records count="1">
<record>
<firstname>TEST</firstname>
<middlename>THE</middlename>
<lastname>CASE</lastname>
<namesuffix>MR</namesuffix>
<address>
<street-number>1111</street-number>
<street-pre-direction>N</street-pre-direction>
<street-name>TABLE</street-name>
<street-post-direction>NE</street-post-direction>
<street-suffix>RD</street-suffix>
<unit-designation>APT</unit-designation>
<unit-number>1</unit-number>
<city>TAMPA</city>
<state>FL</state>
<zip>98765</zip>
<zip4>4321</zip4>
<county>CITRUS</county>
</address>
</record>
</records>
</addressinformation>
<criminalinformation></criminalinformation>
<globalinformation></globalinformation>
</response>


I've used the folliwng PHP code successfully at getting the values from the elements inside "<requestinformation>":

$response = $doc->getElementsByTagName("response")->item(0);

$request_info = $response->getElementsByTagName("requestinformation")->item(0);
$code = $request_info->getAttributeNode("code")->value;
$codemessage = $request_info->getElementsByTagName("codemessage")->item(0)->nodeValue;

$inputs = $request_info->getElementsByTagName("inputs")->item(0);
$firstname = $inputs->getElementsByTagName("firstname")->item(0)->nodeValue;
$lastname = $inputs->getElementsByTagName("lastname")->item(0)->nodeValue;


But I keep on getting the error "Fatal error: Call to a member function getElementsByTagName() on a non-object in /essential.php on line 26" when I try to get information inside of "<addressinformation>":


$address_info = $response->getElementsByTagName("addressinformation")->item(0);
$ssn_validation = $address_info->getElementsByTagName("ssn-validation")->item(0);
$ssn_message = $ssn_validation->getElementsByTagName("message")->item(0)->nodeValue; //THIS IS THE LINE THAT PRODUCES THE ERROR


What am I doing wrong? Why does it work for the element "<requestinformation>" but not for "<addressinformation>"?

dmboyd
05-10-2009, 07:01 AM
It works fine for me:
<?php

$doc = new DOMDocument;
$doc->load('response.xml');
$response = $doc->getElementsByTagName('response')->item(0);

/* request_info */
$request_info = $response->getElementsByTagName('requestinformation')->item(0);

$code = $request_info->getAttributeNode('code')->value;
$codemessage = $request_info->getElementsByTagName('codemessage')->item(0)->nodeValue;

$inputs = $request_info->getElementsByTagName('inputs')->item(0);
$firstname = $inputs->getElementsByTagName('firstname')->item(0)->nodeValue;
$lastname = $inputs->getElementsByTagName('lastname')->item(0)->nodeValue;

/* address_info */
$address_info = $response->getElementsByTagName('addressinformation')->item(0);

$ssn_validation = $address_info->getElementsByTagName('ssn-validation')->item(0);

$ssn_message = $ssn_validation->getElementsByTagName('message')->item(0)->nodeValue;

?>
<html>
<head>
<title>Testing</title>
</head>
<body>
<div id="PageContainer">
<h1>SSN Message</h1>
<p><?php echo $ssn_message; ?></p>
</div>
</body>
</html>

The XML is well-formed, and your PHP code has no problems based upon the code you provided. I'd say it's an issue with PHP honestly. Does my code above work for you or does it fail (remember to change the path to the XML document for the $doc->load() function!!)? If it fails, where does PHP say it fails? Also, what version of PHP are you using?