Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Parsing XML with PHP

Status
Not open for further replies.

wjaram

Programmer
Joined
Apr 21, 2009
Messages
2
Location
US
I need some help parsing XML with PHP. Below is a sample of the XML I'm using.
Code:
<?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>":
Code:
$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>":

Code:
$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>"?
 
Apparently no reason. Maybe due to some typos in your working copy of xml/php?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top