0

可能重复:
使用 SimpleXML 使用命名空间解析 XML

我很难解析 simpleXml 字符串以获取值。我试过了

$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";
}

获取 creationdatetime 字段,但这并没有为我返回任何内容。我熟悉解析简单的 xml,但命名空间 xml 给我带来了困难,特别是因为所有示例总是嵌入命名空间 xml,而不是整个东西都被命名空间。

我试过这段代码。

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

    print print_r($simpleXml, TRUE);

但这并没有为创建日期时间打印出任何内容。当我打印出 $simpleXml 时,它会打印出这个

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

下面是 $simpleXml 的值,打印出来

<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>

4

1 回答 1

0

没有必要仅仅因为你有命名空间就使用 XPath。请阅读->children()->attributes()方法。

在你的情况下:

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

或者,如果您不想依赖“ns0”前缀,请使用完整的命名空间标识符:

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

另外,请注意,在您显示的代码段中,creationdatetime节点不在sender节点中,并且没有a:名称空间前缀,您当前的代码都假定这两者。

于 2012-11-12T19:18:40.823 回答