Jump to content

Trying to access XML data using SimpleXMLElement


Morpheous

Recommended Posts

Hello,

I have been working with PHP for many years, but have avoided XML parsing - now I know why. I am having some difficultly. In the following XML example, I am trying to read domain:name and domain:reason. I have struggled for the past 3 nights.

 

<?xml version="1.0" encoding="utf-8"?> 
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<response>
	<result code="1000">
		<msg>Command completed successfully</msg>
	</result>
	<resData>
		<domain:chkData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
		<domain:cd>
			<domain:name avail="0">foo.com</domain:name>
			<domain:reason>registered</domain:reason>
		</domain:cd>
		<domain:cd>
			<domain:name avail="0">foo.net</domain:name>
			<domain:reason>registered</domain:reason>
		</domain:cd>
		<domain:cd>
			<domain:name avail="0">foo.org</domain:name>
			<domain:reason>registered</domain:reason>
		</domain:cd>
		</domain:chkData>
	</resData>
	<trID>
		<svTRID>test-0000-0000</svTRID>
	</trID>
</response>
</epp> 

 

The code I am using is below. This assumes the above XML data is in a variable called $xmldata

 

$xml = new SimpleXMLElement($xmldata);
foreach ($xml->response->resData as $entry){
  $namespaces = $entry->getNameSpaces(true);
  $domain = $entry->children($namespaces['domain']); 
  echo "Domain : " . $domain->name. "<br />";
  echo "Domain : " . $domain->reason . "<br />";
}

 

Can anyone advise where I am going wrong?

 

Thanks in advance.

Link to comment
Share on other sites

Interestingly, I have managed to solve it.

Here is the code.

 

foreach ($xml->response->resData as $entry){
//Use that namespace
$namespace1 = $entry->getNameSpaces(true);
//Now we don't have the URL hard-coded
$domains = $entry->children($namespace1['domain']); 
$namespace2 = $domains->getNameSpaces(true);
$domainitems = $domains->children($namespace2['domain']);
foreach ($domainitems as $domain) {
	echo "Domain : " . $domain->name . "<br />";
	echo "Domain : " . $domain->reason . "<br />";
}
}

Link to comment
Share on other sites

Glad you sorted it out.  I was in the process of replying when I saw your update.  Another way to access child elements would be:

$xml = new SimpleXMLElement($xmldata);
foreach ($xml->response->resData as $entry){
  $namespaces = $entry->getNameSpaces(true);
  $domain = $entry->children($namespaces['domain']);
  $cds = $domain->chkData->cd;
  foreach ($cds as $cd) {
        echo "$cd->name 
\n";
        echo "$cd->reason 
\n";
  }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.