【问题标题】:Getting values from XML file using simplexml_load_string使用 simplexml_load_string 从 XML 文件中获取值
【发布时间】:2023-03-29 20:02:01
【问题描述】:

我有以下 XML 代码

<epp:epp xmlns:epp="urn:ietf:params:xml:ns:epp-1.0">
  <epp:response>
    <epp:result code="1000">
      <epp:msg>Contact Info Command completed successfully</epp:msg>
    </epp:result>
    <epp:resData>
      <contact:infData xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
        <contact:id>con12</contact:id>
        <contact:status s="ok"/>
        <contact:postalInfo type="loc">
          <contact:name>Name</contact:name>
          <contact:org/>
          <contact:addr>
            <contact:street>streer</contact:street>
            <contact:street>street</contact:street>
            <contact:street/>
            <contact:city>City</contact:city>
            <contact:sp/>
            <contact:pc>186</contact:pc>
            <contact:cc>AA</contact:cc>
        </contact:addr>
      </contact:postalInfo>
        <contact:voice>0000000</contact:voice>
        <contact:fax>000000000</contact:fax>
        <contact:email>support@email.com</contact:email>
    </contact:infData>
    </epp:resData>

  </epp:response>
</epp:epp>

我在使用 simplexml_load_string 从 XML 字符串中获取值时遇到了困难

我可以得到所有以epp开头的节点的结果:比如epp:msg uinsg下面的代码

$objects = simplexml_load_string($result);
$objects->children('epp', true)->response->result->msg;

我似乎无法获得以下值

<contact:infData xmlns:contact="urn:ietf:params:xml:ns:contact-1.0">
        <contact:id>con12</contact:id>
        <contact:status s="ok"/>
        <contact:postalInfo type="loc">
          <contact:name>Name</contact:name>
          <contact:org/>
          <contact:addr>
            <contact:street>streer</contact:street>
            <contact:street>street</contact:street>
            <contact:street/>
            <contact:city>City</contact:city>
            <contact:sp/>
            <contact:pc>186</contact:pc>
            <contact:cc>AA</contact:cc>
        </contact:addr>
      </contact:postalInfo>
        <contact:voice>0000000</contact:voice>
        <contact:fax>000000000</contact:fax>
        <contact:email>support@email.com</contact:email>
    </contact:infData>

我还需要知道如何获取&lt;epp:result code="1000"&gt; 中的代码值

任何帮助/指针将不胜感激,

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    你已经完成了 99%(除非我遗漏了什么)。基本上,每当你想“改变”命名空间时,你都可以使用-&gt;children() 或-&gt;attributes() 方法。

    所以你可以说$objects-&gt;children('epp', true)-&gt;response-&gt;result-&gt;resData-&gt;children('contact', true)-&gt;infData-&gt;id

    &lt;epp:result code="1000"&gt; 中的code 属性为technically not in any namespace at all,因此您必须选择 NULL 命名空间才能访问它:$objects-&gt;children('epp', true)-&gt;response-&gt;result-&gt;attributes(NULL)-&gt;code。

    也不要忘记,通过使用别名('epp'、'contact'),您依赖于数据源而不更改它们,而技术上它们可以在不更改文件“含义”的情况下进行更改。为了防止这种情况,您必须改为硬编码 URI(urn:ietf:params:xml:ns:epp-1.0 和 urn:ietf:params:xml:ns:contact-1.0)。

    【讨论】:

      【解决方案2】:

      你快到了。以类似的方式访问更深层的元素:

      $contact = $objects->children('epp', true)->response->resData->children('contact',tee);
      $name = $contact->infData->postalInfo->name;
      

      对于代码值,我假设您的意思是&lt;epp:result code="1000"&gt;

      $resultAttributes = $objects->children('epp', true)->response->result->attributes();
      $code = $resultAttributes['code'];
      

      注意:$name 和 $code 最终都是 SimpleXMLElements 而不是字符串,但您可以将它们转换为字符串:

      $a = (string)$name;
      

      【讨论】:

        猜你喜欢
        • 2011-11-29
        • 2012-12-30
        • 2015-02-18
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 2018-06-03
        • 2023-03-29
        相关资源
        最近更新 更多