Morpheous Posted January 16, 2012 Share Posted January 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255172-trying-to-access-xml-data-using-simplexmlelement/ Share on other sites More sharing options...
Morpheous Posted January 16, 2012 Author Share Posted January 16, 2012 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 />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/255172-trying-to-access-xml-data-using-simplexmlelement/#findComment-1308354 Share on other sites More sharing options...
gizmola Posted January 16, 2012 Share Posted January 16, 2012 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/255172-trying-to-access-xml-data-using-simplexmlelement/#findComment-1308358 Share on other sites More sharing options...
Morpheous Posted January 16, 2012 Author Share Posted January 16, 2012 Hello. Thanks for the reply. I quite like your coding as it's much shorter. I've just tested it and it works. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/255172-trying-to-access-xml-data-using-simplexmlelement/#findComment-1308361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.