0

Possible Duplicate:
Parse XML with Namespace using SimpleXML

I'm having difficult parsing a simpleXml string to get a value out. I've tried

$xml = new SimpleXmlElement($simpleXml);

$xml->registerXPathNamespace("ns0", "http://www.openapplications.org/oagis");
foreach($xml->xpath('//ns0:applicationarea/a:sender') as $n) {
  echo $n->creationdatetime . "success";
}

To get the creationdatetime field, but this is not returning anything for me. I'm familiar with parsing simple xml, but the namespaced xml throws me difficulty, especially because all examples always have namespaced xml embedded, rather than the whole thing being namespaced.

I tried this code.

$simpleXml = simplexml_load_string($xml);
    print $simpleXml->children('ns0', true)->applicationarea->creationdatetime;

    print print_r($simpleXml, TRUE);

But this didn't print out anything for the creationdatetime. When I print out $simpleXml, it prints this

SimpleXMLElement Object ( [@attributes] => Array ( [revision] => 1.0 [environment] => Production [lang] => en-US )

Below is the value for $simpleXml, printed out

<ns0:syncparty xmlns:ns0="http://www.openapplications.org/oagis" revision="1.0" environment="Production" lang="en-US">
<ns0:applicationarea>
    <ns0:sender>
        <ns0:authorizationid>QPIM</ns0:authorizationid>
    </ns0:sender>
    <ns0:creationdatetime>2012-11-12T09:58:32.257-08:00</ns0:creationdatetime>
</ns0:applicationarea>
<ns0:dataarea>
    <ns0:sync>
        <ns0:synccriteria expressionlanguage="XPath">
            <ns0:syncexpression action="Sync">GetContact</ns0:syncexpression>
        </ns0:synccriteria>
    </ns0:sync>
    <ns0:party active="false" onetime="false">
        <ns0:contacts>
            <ns0:primarycontact>
                <ns0:person>
                    <ns0:personcode>409090</ns0:personcode>
                    <ns0:personname>
                        <ns0:givenname>Erik</ns0:givenname>
                        <ns0:familyname>Sparrow</ns0:familyname>
                        <ns0:userarea>
                    </ns0:userarea></ns0:personname>
                    <ns0:userarea>
                </ns0:userarea></ns0:person>
                <ns0:qctelex>
                <ns0:emailaddress>artest02@qcomtest.com</ns0:emailaddress>
                <ns0:addresses>
                    <ns0:primaryaddress>
                        <ns0:country>UNITED STATES</ns0:country>
                        <ns0:qccountrycode>
                            <ns0:qcccode>US</ns0:qcccode>
                        </ns0:qccountrycode>
                        <ns0:userarea>
                    </ns0:userarea></ns0:primaryaddress>
                </ns0:addresses>
                <ns0:userarea>
                    <ns0:property>
                        <ns0:namevalue name="reg_id">172914</ns0:namevalue>
                        <ns0:description>QPIM Registration Id</ns0:description>
                    </ns0:property>
                    <ns0:property>
                        <ns0:namevalue name="qcOldDN">N/A</ns0:namevalue>
                        <ns0:description>QPIM Rename Values</ns0:description>
                    </ns0:property>
                    <ns0:useraccount>
                        <ns0:systemid>artest02@qcomtest.com</ns0:systemid>
                        <ns0:authorization>
                            <ns0:id>422</ns0:id>
                            <ns0:type>bus_role</ns0:type>
                            <ns0:status>
                                <ns0:code>A</ns0:code>
                                <ns0:change>
                                    <ns0:changedate>2012-02-06T11:20:34-08:00</ns0:changedate>
                                    <ns0:userarea>
                                        <ns0:property>
                                            <ns0:namevalue name="UPDATED_BY">QPIM</ns0:namevalue>
                                        </ns0:property>
                                    </ns0:userarea>
                                </ns0:change>
                            </ns0:status>
                            <ns0:dateauthorized>2012-02-06T11:20:34-08:00</ns0:dateauthorized>
                            <ns0:authorizationcode>CRD_AR</ns0:authorizationcode>
                            <ns0:userarea>
                                <ns0:property>
                                    <ns0:namevalue name="CREATED_BY">QPIM</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="DESCRIPTION">Provisioning Role for CRD_AR</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="TAG">ar</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_ID">484</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_NAME">CRD_Admins</ns0:namevalue>
                                </ns0:property>
                            </ns0:userarea>
                        </ns0:authorization>
                        <ns0:authorization>
                            <ns0:id>402</ns0:id>
                            <ns0:type>bus_role</ns0:type>
                            <ns0:status>
                                <ns0:code>A</ns0:code>
                                <ns0:change>
                                    <ns0:changedate>2012-07-18T13:55:41-07:00</ns0:changedate>
                                    <ns0:userarea>
                                        <ns0:property>
                                            <ns0:namevalue name="UPDATED_BY">arsrvc</ns0:namevalue>
                                        </ns0:property>
                                    </ns0:userarea>
                                </ns0:change>
                            </ns0:status>
                            <ns0:dateauthorized>2012-07-18T13:55:41-07:00</ns0:dateauthorized>
                            <ns0:authorizationcode>CRD_Alpha</ns0:authorizationcode>
                            <ns0:userarea>
                                <ns0:property>
                                    <ns0:namevalue name="CREATED_BY">arsrvc</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="DESCRIPTION">Provisioning role for CRD_ALPHA</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="TAG">alpha</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_ID">484</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_NAME">CRD_Admins</ns0:namevalue>
                                </ns0:property>
                            </ns0:userarea>
                        </ns0:authorization>
                        <ns0:authorization>
                            <ns0:id>22</ns0:id>
                            <ns0:type>policy_role</ns0:type>
                            <ns0:status>
                                <ns0:code>A</ns0:code>
                                <ns0:description>Approved</ns0:description>
                                <ns0:change>
                                    <ns0:changedate>2012-02-06T11:20:43-08:00</ns0:changedate>
                                    <ns0:userarea>
                                        <ns0:property>
                                            <ns0:namevalue name="UPDATED_BY">QPIM_VALID_DPL</ns0:namevalue>
                                        </ns0:property>
                                    </ns0:userarea>
                                </ns0:change>
                            </ns0:status>
                            <ns0:dateauthorized>2012-02-06T11:20:34-08:00</ns0:dateauthorized>
                            <ns0:authorizationcode>QPIM_VALID_DPL</ns0:authorizationcode>
                            <ns0:userarea>
                                <ns0:property>
                                    <ns0:namevalue name="CREATED_BY">QPIM</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="DESCRIPTION">This role is used to flag if a person has been DPL approved</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="TAG">dpl,validation</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_ID">82</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_NAME">QPIM_POLICY_ADMINS</ns0:namevalue>
                                </ns0:property>
                            </ns0:userarea>
                        </ns0:authorization>
                        <ns0:authorization>
                            <ns0:id>21</ns0:id>
                            <ns0:type>policy_role</ns0:type>
                            <ns0:status>
                                <ns0:code>A</ns0:code>
                                <ns0:change>
                                    <ns0:changedate>2012-02-06T11:21:24-08:00</ns0:changedate>
                                    <ns0:userarea>
                                        <ns0:property>
                                            <ns0:namevalue name="UPDATED_BY">arsrvc</ns0:namevalue>
                                        </ns0:property>
                                    </ns0:userarea>
                                </ns0:change>
                            </ns0:status>
                            <ns0:dateauthorized>2012-02-06T11:20:34-08:00</ns0:dateauthorized>
                            <ns0:authorizationcode>QPIM_VALID_EMAIL</ns0:authorizationcode>
                            <ns0:userarea>
                                <ns0:property>
                                    <ns0:namevalue name="CREATED_BY">QPIM</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="DESCRIPTION">This role is used to flag if a person has been email validated</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="TAG">email,validation</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_ID">82</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_NAME">QPIM_POLICY_ADMINS</ns0:namevalue>
                                </ns0:property>
                            </ns0:userarea>
                        </ns0:authorization>
                        <ns0:authorization>
                            <ns0:id>122</ns0:id>
                            <ns0:type>bus_role</ns0:type>
                            <ns0:status>
                                <ns0:code>A</ns0:code>
                                <ns0:change>
                                    <ns0:changedate>2012-02-06T11:24:46-08:00</ns0:changedate>
                                    <ns0:userarea>
                                        <ns0:property>
                                            <ns0:namevalue name="UPDATED_BY">brewsrvc</ns0:namevalue>
                                        </ns0:property>
                                    </ns0:userarea>
                                </ns0:change>
                            </ns0:status>
                            <ns0:dateauthorized>2012-02-06T11:24:46-08:00</ns0:dateauthorized>
                            <ns0:authorizationcode>REG_DEV</ns0:authorizationcode>
                            <ns0:userarea>
                                <ns0:property>
                                    <ns0:namevalue name="CREATED_BY">brewsrvc</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="DESCRIPTION">Provisioning group for Registered Developers</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="TAG">regdev</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_ID">182</ns0:namevalue>
                                </ns0:property>
                                <ns0:property>
                                    <ns0:namevalue name="OWNER_NAME">REG_DEV_ADMIN</ns0:namevalue>
                                </ns0:property>
                            </ns0:userarea>
                        </ns0:authorization>
                    </ns0:useraccount>
                </ns0:userarea>
            </ns0:qctelex></ns0:primarycontact>
        </ns0:contacts>
    </ns0:party>
</ns0:dataarea>

Community
  • 1
  • 1
Andrew Sparrow
  • 167
  • 2
  • 2
  • 12
  • Sorry to ask about the obvious: Where do you register `a` as xpath namespace? – hakre Nov 12 '12 at 18:10
  • alternative duplicates with better answers not involving unnecessary use of XPath: http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names http://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems – IMSoP Nov 12 '12 at 19:24
  • When I add `print print_r($simpleXml->children('ns0', true)->applicationarea, TRUE);` I just get `SimpleXMLElement Object ( )` – Andrew Sparrow Nov 12 '12 at 20:28
  • Nothing gets printed out for `print $simpleXml->children('ns0', true)->applicationarea->sender->creationdatetime ."
    ";`
    – Andrew Sparrow Nov 12 '12 at 20:29

1 Answers1

0

There is no need to use XPath just because you have namespaces. Please read up on the ->children() and ->attributes() methods.

In your case:

$xml->children('ns0', true)->applicationarea->creationdatetime

Or if you don't want to rely on the 'ns0' prefix, use the full namespace identifier:

$xml->children('http://www.openapplications.org/oagis')->applicationarea->creationdatetime

Also, note that in the snippet you show, the creationdatetime node is not in the sender node, and there is no a: namespace prefix, both of which your current code assumes.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • I tried this code. 'code'$simpleXml = simplexml_load_string($xml); print $simpleXml->children('ns0', true)->applicationarea->creationdatetime; print print_r($simpleXml, TRUE);'code' But this didn't print out anything for the creationdatetime. When I print out $simpleXml, it prints this 'code'SimpleXMLElement Object ( [@attributes] => Array ( [revision] => 1.0 [environment] => Production [lang] => en-US )'code' – Andrew Sparrow Nov 12 '12 at 20:03
  • I tried this code. `$simpleXml = simplexml_load_string($xml); print $simpleXml->children('ns0', true)->applicationarea->creationdatetime; print print_r($simpleXml, TRUE);` But this didn't print out anything for the creationdatetime. When I print out $simpleXml, it prints this `SimpleXMLElement Object ( [@attributes] => Array ( [revision] => 1.0 [environment] => Production [lang] => en-US )` – Andrew Sparrow Nov 12 '12 at 20:09
  • Firstly, never use `print_r` to inspect SimpleXML objects; see https://github.com/imsop/simplexml_debug for some functions which show what's actually there (also, you do realise `print print_r($x, true)` is the same as `print_r($x)`?) – IMSoP Nov 12 '12 at 22:54
  • Secondly, that exact code works fine for me, as long as I add the missing end tag from your XML sample: http://codepad.org/d7P0NXv2 Have you checked if you're getting any errors or warnings (using `error_reporting(E_ALL);`)? If you leave off the end tag as in your example, you should see this: http://codepad.org/u2PdmvPq – IMSoP Nov 12 '12 at 23:01