How to read values from XML document <a:int> in php

Viewed 39

Here is my problem :

I receive a xml string response from an api call an I need to read data. I use the function simplexml_load_string() to convert my string into a simpleXMLElement but some data don't appear to be correctly convert during the process.

Here is the exemple :

<JobList xmlns="http://webservice.blabla.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <jobCount>1</jobCount>
    <JobSet>
        <Job>
            ...
            <JobDataSet>
                <JobData>
                    ...
            </JobDataSet>
            ...
            <departmentKeys xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
                <a:int>1608</a:int>
            </departmentKeys>
        </Job>
    </JobSet>
</JobList>

I want to access to 1608 in departmentKeys but when I parse it using simplexml_load_string() I have all data except the one in departmentKeys

result of my xmlelementand I don't understand why ?

Can someone help me ? Sorry for my poor english.

1 Answers

You need to deal with namespaces:

$xmlDoc = simplexml_load_string($xmlString);#this assumes your xml string is well formed, of course
$xmlDoc->registerXPathNamespace("a", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
$target = $xmlDoc->xpath('//a:int/text()');
echo ($target[0]);

Output should be

1608
Related