1

I can read XML files or strings but not the next:

$str = <<<XML
    <Output xmlns="nice.uniform://" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Data i:nil="true"/>
        <Result xmlns:a="http://nice.uniform/CLSAPI3">
            <a:ResultCode>SUCCESS</a:ResultCode>
            <a:ResultMessage>OK</a:ResultMessage>
            <a:ResultCodeEx>CLS_SE_SUCCESS</a:ResultCodeEx>
        </Result> 
        <Exception i:nil="true" xmlns:a="http://schemas.datacontract.org/2004/07/System"/>
    </Output>
XML;

My PHP code to read a node of XML file is:

$Output = new SimpleXMLElement($str);
echo $Output->Result->{'a:ResultCodeEx'};

Also, I've tried:

$xmlResponse = simplexml_load_file('file.xml');
foreach($xmlResponse->Result as $xmlEntry)
{
 echo $xmlEntry->{'a:ResultCodeEx'};
}

//or

$blocks = $xmlResponse->xpath('//Output');
print_r($blocks);

Can you help me?

3
  • I'd say that is not a valid xml file. Commented Apr 11, 2014 at 11:49
  • @arkascha, I've validated xml file. It's valid Commented Apr 11, 2014 at 11:52
  • 1
    My PHP is rusty to say the least but; The XML is defined with a default namespace (nice.uniform://). the 3 deepest nodes are from another namespace (nice.uniform/CLSAPI3). When selecting items from this XML you must include the namespaces in the selection Commented Apr 11, 2014 at 11:52

1 Answer 1

1

The document uses several namespaces. The element ResultCodeEx belongs to the namespace a. You can use XPath, but you need to register the namespace a before the query:

$Output = new SimpleXMLElement($str);
$Output->registerXPathNamespace ('a', 'http://nice.uniform/CLSAPI3');
$result =  $Output->xpath('//a:ResultCodeEx/text()');
echo $result[0]; // CLS_SE_SUCCESS
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.