0

I am trying to extract information out of a return xml epp messages from SIDN

But i am not abbel to get some of the variables out of this messages. I manage to get the result code and messages.

$domaininfo = xml messages that can be seen at : http://pastebin.com/HbXMkdD3

    $xml = new SimpleXMLElement($domeininfo);
// check result code
    if (isset($xml->response->result))
        { foreach($xml->response->result->attributes() as $name => $value) {
            if ($name === 'code')
            { $code = $value; }
          }
        }

if ($code == '1000')
{
    $domeinnaamuitxml = $xml->response->{'resData'}->{'domain:infData'}->{'domain:name'};
    $techcuitxml = $xml->response->{'resData'}->{'domain:infData'}->{'domain:contact type="tech"'};
    $admincuitxml = $xml->response->{'resData'}->{'domain:infData'}->{'domain:contact type="admin"'};
    echo "Domein naam             :    $domeinnaamuitxml \n";
    echo "Admin C                 :    $admincuitxml \n";
    echo "Tech C                  :    $techcuitxml \n";
}

What is it that i am doing wrong

It seam as soon as there is a : - = or " in the tag there is a problem

all help is surely welkom

  • SimpleXML does not play nicely with namespaced elements. Use a different XML parser, f.e. DOMDocument. – CBroe Feb 14 '14 at 20:08
  • 1
    @CBroe That statement is just plain wrong. The `->children()` and `->attributes()` methods allow you to use namespaces just fine. – IMSoP Feb 15 '14 at 19:02
  • possible duplicate of [PHP namespace simplexml problems](http://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems) – IMSoP Feb 15 '14 at 19:09
  • @IMSoP: Thanks, I should update my knowledge on that part then. The explanation in your user profile is helpful! – CBroe Feb 15 '14 at 22:27
  • … as are your SimpleXML dump functions on GitHub! – CBroe Feb 15 '14 at 22:35

1 Answers1

1

use xpathto select namespaced elements with simplexml:

$domeinnaamuitxml = (string)$xml->xpath("//domain:name"}[0];

Comment: The above code requires PHP >= 5.4 because of the [0] (array dereferencing). In an older version of PHP, do:

 $domeinnaamuitxml = $xml->xpath("//domain:name"};
 $domeinnaamuitxml = (string)$domeinnaamuitxml[0];

see it working: https://eval.in/101915

michi
  • 6,565
  • 4
  • 33
  • 56
  • While a useful tool, there is no particular reason to use XPath rather than the main accessors of SimpleXML just because namespaces are involved. The `->children()` and `->attributes()` methods are the simplest way to traverse namespaces. – IMSoP Feb 15 '14 at 19:11
  • Thanks, work great even to get attributes :-D just do ` $admincuitxml = $xml->xpath("//domain:contact[contains(@type, 'admin')]");` – Bas van den Dikkenberg Feb 15 '14 at 21:25
  • @IMSoP I find it clumsy to use `children` and then loop through the results to find a specific node, so I prefer `xpath` – michi Feb 15 '14 at 23:17
  • @michi Fair enough, but my point is that's completely orthogonal to the presence of namespaces - it's you can use XPath to search a non-namespaced document, and SimpleXML's traversal methods with a namespaced one. – IMSoP Feb 16 '14 at 16:15