【问题标题】:PHP - Parsing XML ChallengePHP - 解析 XML 挑战
【发布时间】:2018-01-03 22:38:55
【问题描述】:

我正在尝试解析类似于以下内容的提要:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://api4.mysite.com/api/" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Customers</title>
  <id>https://api4.mysite.com/api/Customers/</id>
  <updated>2017-07-27T08:00:15Z</updated>
  <link rel="self" title="Customers" href="Customers" />
  <entry>
    <id>https://api4.mysite.com/api/Contacts('00000001-A000-0000-1000-030102101201')</id>
    <title type="text"></title>
    <updated>2017-07-27T08:00:15Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Contact" href="Contacts('00000001-A000-0000-1000-030102101201')" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactType" type="application/atom+xml;type=feed" title="ContactType" href="Contacts('00000001-A000-0000-1000-030102101201')/ContactType" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactPhone" type="application/atom+xml;type=entry" title="ContactPhone" href="Contacts('00000001-A000-0000-1000-030102101201')/ContactPhone" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Job" type="application/atom+xml;type=feed" title="Job" href="Contacts('00000001-A000-0000-1000-030102101201')/Job" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Inquiry" type="application/atom+xml;type=feed" title="Inquiry" href="Contacts('00000001-A000-0000-1000-030102101201')/Inquiry" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Address" type="application/atom+xml;type=entry" title="Address" href="Contacts('00000001-A000-0000-1000-030102101201')/Address" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AdditionalContact" type="application/atom+xml;type=feed" title="AdditionalContact" href="Contacts('00000001-A000-0000-1000-030102101201')/AdditionalContact" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Company" type="application/atom+xml;type=entry" title="Company" href="Contacts('00000001-A000-0000-1000-030102101201')/Company" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CustomField" type="application/atom+xml;type=feed" title="CustomField" href="Contacts('00000001-A000-0000-1000-030102101201')/CustomField" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Activity" type="application/atom+xml;type=feed" title="Activity" href="Contacts('00000001-A000-0000-1000-030102101201')/Activity" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FutureInterest" type="application/atom+xml;type=feed" title="FutureInterest" href="Contacts('00000001-A000-0000-1000-030102101201')/FutureInterest" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Note" type="application/atom+xml;type=feed" title="Note" href="Contacts('00000001-A000-0000-1000-030102101201')/Note" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/C800ResponseLead" type="application/atom+xml;type=feed" title="C800ResponseLead" href="Contacts('00000001-A000-0000-1000-030102101201')/C800ResponseLead" />
    <category term="MSharpModel.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:id>00000001-A000-0000-1000-030102101201</d:id>
        <d:companyId>2199</d:companyId>
        <d:firstName>Shannon</d:firstName>
        <d:lastName>Hall</d:lastName>
        <d:middleInitial m:null="true" />
        <d:title m:null="true" />
        <d:incomeCode m:null="true" />
        <d:ageCode m:null="true" />
        <d:structureAgeCode m:null="true" />
        <d:reference m:null="true" />
        <d:businessName m:null="true" />
        <d:contactPhoneId>1d32d3ad-c710-4963-a425-eb7853c4fdf9</d:contactPhoneId>
        <d:primaryAddressId>b8a78bb8-3895-492f-a4a2-f6e287081dcf</d:primaryAddressId>
        <d:website1 m:null="true" />
        <d:website2 m:null="true" />
        <d:website3 m:null="true" />
        <d:email1 m:null="true" />
        <d:isActive m:type="Edm.Boolean">true</d:isActive>
        <d:lastUpdate m:type="Edm.DateTime">2015-05-15T14:49:45.143-04:00</d:lastUpdate>
        <d:createdDate m:type="Edm.DateTime">2015-05-15T14:49:45.143-04:00</d:createdDate>
      </m:properties>
    </content>
  </entry>
  <entry>
    <id>https://api4.mysite.com/api/Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')</id>
    <title type="text"></title>
    <updated>2017-07-27T08:00:15Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Contact" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactType" type="application/atom+xml;type=feed" title="ContactType" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/ContactType" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactPhone" type="application/atom+xml;type=entry" title="ContactPhone" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/ContactPhone" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Job" type="application/atom+xml;type=feed" title="Job" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Job" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Inquiry" type="application/atom+xml;type=feed" title="Inquiry" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Inquiry" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Address" type="application/atom+xml;type=entry" title="Address" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Address" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AdditionalContact" type="application/atom+xml;type=feed" title="AdditionalContact"  ▶
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Company" type="application/atom+xml;type=entry" title="Company" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Company" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CustomField" type="application/atom+xml;type=feed" title="CustomField" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/CustomField" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Activity" type="application/atom+xml;type=feed" title="Activity" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Activity" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FutureInterest" type="application/atom+xml;type=feed" title="FutureInterest" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/FutureInterest" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Note" type="application/atom+xml;type=feed" title="Note" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Note" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/C800ResponseLead" type="application/atom+xml;type=feed" title="C800ResponseLead" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/C800ResponseLead" />
    <category term="MSharpModel.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:id>992ABCSA-1231-8381-JKL8-38KDLALNA881</d:id>
        <d:companyId>2100</d:companyId>
        <d:firstName>Jane</d:firstName>
        <d:lastName>Fonda</d:lastName>
        <d:middleInitial m:null="true" />
        <d:title>Mrs.</d:title>
        <d:incomeCode m:null="true" />
        <d:ageCode m:null="true" />
        <d:structureAgeCode m:null="true" />
        <d:reference m:null="true" />
        <d:businessName m:null="true" />
        <d:contactPhoneId>427C367B-F490-412A-B9B0-ABABABABABAB</d:contactPhoneId>
        <d:primaryAddressId>f7668325-ba95-4bde-b28d-0226bbd4d9c7</d:primaryAddressId>
        <d:website1 m:null="true" />
        <d:website2 m:null="true" />
        <d:website3 m:null="true" />
        <d:email1 m:null="true" />
        <d:email1CanMail m:type="Edm.Boolean">false</d:email1CanMail>
        <d:email2 m:null="true" />
        <d:email2CanMail m:type="Edm.Boolean">false</d:email2CanMail>
        <d:email3 m:null="true" />
        <d:email3CanMail m:type="Edm.Boolean">false</d:email3CanMail>
        <d:isTagged m:type="Edm.Boolean" m:null="true" />
        <d:structureValueCode m:null="true" />
        <d:doNotMail m:type="Edm.Boolean" m:null="true" />
        <d:yearHomeBuilt m:null="true" />
        <d:maritalStatus m:null="true" />
        <d:lengthOfResidence m:null="true" />
        <d:styleOfHome m:null="true" />
        <d:mailMergeName>Jesse Peters</d:mailMergeName>
        <d:source>Access - KGuard</d:source>
        <d:creationDate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:creationDate>
        <d:qbSyncDate m:type="Edm.DateTime" m:null="true" />
        <d:qbId m:null="true" />
        <d:isActive m:type="Edm.Boolean">true</d:isActive>
        <d:lastUpdate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:lastUpdate>
        <d:hasDNCPhone m:type="Edm.Boolean">false</d:hasDNCPhone>
        <d:hasDNEEmail m:type="Edm.Boolean">false</d:hasDNEEmail>
        <d:qbEditSequence m:null="true" />
        <d:qbSyncData m:type="Edm.Boolean" m:null="true" />
        <d:qbName m:null="true" />
        <d:createdDate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:createdDate>
      </m:properties>
    </content>
  </entry>
</feed>

但我在获取命名空间子 标记时遇到问题。当我只返回 1 时一切正常,但现在我正在拉多个,我真的很难得到一个好的对象数组。

不幸的是,我已经编写了这么多不同形式的函数,我什至不确定现在哪一个最接近。

示例 1:

public function parseMultipleEntriesIntoAnObject($xml) {
        $z = new XMLReader();
        $z->xml($xml);
        //while ($z->read() && $z->name !== 'entry');
        // <content>
        // m:properties
        // d:<property_name
        $results = [];
        $key = "";
        while( $z->read() ) {
            //print($z->name);
            if ($z->nodeType == XMLReader::ELEMENT && $z->name === 'entry') {
                $r = new \stdClass;
                //dd("Yeup");
                while ($z->read()) {
                    // reached the </entry>. skip to next                   
                    if ($z->nodeType === XMLReader::END_ELEMENT && $z->name === 'entry') {
                        $results[] = $r;
                        $z->next('entry');
                        $r = new \stdClass;
                    }
                    // this may mess up on nested elements. Oh well.
                    if ($z->nodeType === XMLReader::ELEMENT) {
                        $key = $z->name;
                    }
                    if ($z->nodeType === XMLReader::TEXT) {
                        //echo "$key = $z->value <br/>";
                        $r->{$key} = $z->value;
                    }   
                }
            }
        }
        $z->close();

        return $results;
    }

示例 2 和 3:

private function getXMLEntries($xml) {
        $resultsArray = [];
        $str = trim(preg_replace('/\s\s+/', '', $xml));
        $z = new XMLreader;
        $doc = new \DOMDocument;

        $z->xml($str);
        $props = [];
        while ($z->read() && $z->name !== 'entry');
        while ($z->name === 'entry') {
            $x = simplexml_import_dom($doc->importNode($z->expand(), true));
            var_dump($x->content);
            $resultsArray[] = $x;
            //$resultsArray[] = $z->expand();
            $z->next('entry');
        }
        return $resultsArray;
    }
    private function processXMLProperties($xml) {
        $str = trim(preg_replace('/\s\s+/', '', $xml));
        $z = new XMLreader;
        $z->xml($str);
        $props = [];
        while ($z->read() && $z->name !== 'm:properties');
        $key = "";
        while ($z->read()) {
            if ($z->nodeType === XMLReader::ELEMENT) {
                $key = $z->name;
                $z->read();
            }
            if ($z->nodeType === XMLReader::TEXT) {
                $props[$key] = $z->value;
            }
        } return $props; }

希望你能看到我想要做什么。如果重要的话,这可以通过使用 Guzzle 的 Stream 来实现。基本上我需要一个对象数组,其中每个对象都包含 的各种属性,理想情况下没有它们的 d: 前缀。

请注意,根据我附加到查询中的内容,任何相关的

感谢您的帮助!

【问题讨论】:

    标签: php xpath xml-parsing simplexml xml-attribute


    【解决方案1】:

    使用 Xpath。

    d 命名空间未在 XML 字符串中定义,但在将 xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 添加到 &lt;feed&gt; 标记时,以下工作:

    <?php
    
    $str = <<<EOF
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <feed xml:base="https://api4.mysite.com/api/"  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
      <title type="text">Customers</title>
      <id>https://api4.mysite.com/api/Customers/</id>
      <updated>2017-07-27T08:00:15Z</updated>
      <link rel="self" title="Customers" href="Customers" />
      <entry>
        <id>https://api4.mysite.com/api/Contacts('00000001-A000-0000-1000-030102101201')</id>
        <title type="text"></title>
        <updated>2017-07-27T08:00:15Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Contact" href="Contacts('00000001-A000-0000-1000-030102101201')" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactType" type="application/atom+xml;type=feed" title="ContactType" href="Contacts('00000001-A000-0000-1000-030102101201')/ContactType" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactPhone" type="application/atom+xml;type=entry" title="ContactPhone" href="Contacts('00000001-A000-0000-1000-030102101201')/ContactPhone" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Job" type="application/atom+xml;type=feed" title="Job" href="Contacts('00000001-A000-0000-1000-030102101201')/Job" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Inquiry" type="application/atom+xml;type=feed" title="Inquiry" href="Contacts('00000001-A000-0000-1000-030102101201')/Inquiry" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Address" type="application/atom+xml;type=entry" title="Address" href="Contacts('00000001-A000-0000-1000-030102101201')/Address" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AdditionalContact" type="application/atom+xml;type=feed" title="AdditionalContact" href="Contacts('00000001-A000-0000-1000-030102101201')/AdditionalContact" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Company" type="application/atom+xml;type=entry" title="Company" href="Contacts('00000001-A000-0000-1000-030102101201')/Company" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CustomField" type="application/atom+xml;type=feed" title="CustomField" href="Contacts('00000001-A000-0000-1000-030102101201')/CustomField" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Activity" type="application/atom+xml;type=feed" title="Activity" href="Contacts('00000001-A000-0000-1000-030102101201')/Activity" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FutureInterest" type="application/atom+xml;type=feed" title="FutureInterest" href="Contacts('00000001-A000-0000-1000-030102101201')/FutureInterest" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Note" type="application/atom+xml;type=feed" title="Note" href="Contacts('00000001-A000-0000-1000-030102101201')/Note" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/C800ResponseLead" type="application/atom+xml;type=feed" title="C800ResponseLead" href="Contacts('00000001-A000-0000-1000-030102101201')/C800ResponseLead" />
        <category term="MSharpModel.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:id>00000001-A000-0000-1000-030102101201</d:id>
            <d:companyId>2199</d:companyId>
            <d:firstName>Shannon</d:firstName>
            <d:lastName>Hall</d:lastName>
            <d:middleInitial m:null="true" />
            <d:title m:null="true" />
            <d:incomeCode m:null="true" />
            <d:ageCode m:null="true" />
            <d:structureAgeCode m:null="true" />
            <d:reference m:null="true" />
            <d:businessName m:null="true" />
            <d:contactPhoneId>1d32d3ad-c710-4963-a425-eb7853c4fdf9</d:contactPhoneId>
            <d:primaryAddressId>b8a78bb8-3895-492f-a4a2-f6e287081dcf</d:primaryAddressId>
            <d:website1 m:null="true" />
            <d:website2 m:null="true" />
            <d:website3 m:null="true" />
            <d:email1 m:null="true" />
            <d:isActive m:type="Edm.Boolean">true</d:isActive>
            <d:lastUpdate m:type="Edm.DateTime">2015-05-15T14:49:45.143-04:00</d:lastUpdate>
            <d:createdDate m:type="Edm.DateTime">2015-05-15T14:49:45.143-04:00</d:createdDate>
          </m:properties>
        </content>
      </entry>
      <entry>
        <id>https://api4.mysite.com/api/Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')</id>
        <title type="text"></title>
        <updated>2017-07-27T08:00:15Z</updated>
        <author>
          <name />
        </author>
        <link rel="edit" title="Contact" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactType" type="application/atom+xml;type=feed" title="ContactType" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/ContactType" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContactPhone" type="application/atom+xml;type=entry" title="ContactPhone" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/ContactPhone" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Job" type="application/atom+xml;type=feed" title="Job" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Job" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Inquiry" type="application/atom+xml;type=feed" title="Inquiry" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Inquiry" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Address" type="application/atom+xml;type=entry" title="Address" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Address" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AdditionalContact" type="application/atom+xml;type=feed" title="AdditionalContact"  />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Company" type="application/atom+xml;type=entry" title="Company" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Company" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CustomField" type="application/atom+xml;type=feed" title="CustomField" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/CustomField" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Activity" type="application/atom+xml;type=feed" title="Activity" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Activity" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FutureInterest" type="application/atom+xml;type=feed" title="FutureInterest" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/FutureInterest" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Note" type="application/atom+xml;type=feed" title="Note" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/Note" />
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/C800ResponseLead" type="application/atom+xml;type=feed" title="C800ResponseLead" href="Contacts('992ABCSA-1231-8381-JKL8-38KDLALNA881')/C800ResponseLead" />
        <category term="MSharpModel.Contact" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
          <m:properties>
            <d:id>992ABCSA-1231-8381-JKL8-38KDLALNA881</d:id>
            <d:companyId>2100</d:companyId>
            <d:firstName>Jane</d:firstName>
            <d:lastName>Fonda</d:lastName>
            <d:middleInitial m:null="true" />
            <d:title>Mrs.</d:title>
            <d:incomeCode m:null="true" />
            <d:ageCode m:null="true" />
            <d:structureAgeCode m:null="true" />
            <d:reference m:null="true" />
            <d:businessName m:null="true" />
            <d:contactPhoneId>427C367B-F490-412A-B9B0-ABABABABABAB</d:contactPhoneId>
            <d:primaryAddressId>f7668325-ba95-4bde-b28d-0226bbd4d9c7</d:primaryAddressId>
            <d:website1 m:null="true" />
            <d:website2 m:null="true" />
            <d:website3 m:null="true" />
            <d:email1 m:null="true" />
            <d:email1CanMail m:type="Edm.Boolean">false</d:email1CanMail>
            <d:email2 m:null="true" />
            <d:email2CanMail m:type="Edm.Boolean">false</d:email2CanMail>
            <d:email3 m:null="true" />
            <d:email3CanMail m:type="Edm.Boolean">false</d:email3CanMail>
            <d:isTagged m:type="Edm.Boolean" m:null="true" />
            <d:structureValueCode m:null="true" />
            <d:doNotMail m:type="Edm.Boolean" m:null="true" />
            <d:yearHomeBuilt m:null="true" />
            <d:maritalStatus m:null="true" />
            <d:lengthOfResidence m:null="true" />
            <d:styleOfHome m:null="true" />
            <d:mailMergeName>Jesse Peters</d:mailMergeName>
            <d:source>Access - KGuard</d:source>
            <d:creationDate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:creationDate>
            <d:qbSyncDate m:type="Edm.DateTime" m:null="true" />
            <d:qbId m:null="true" />
            <d:isActive m:type="Edm.Boolean">true</d:isActive>
            <d:lastUpdate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:lastUpdate>
            <d:hasDNCPhone m:type="Edm.Boolean">false</d:hasDNCPhone>
            <d:hasDNEEmail m:type="Edm.Boolean">false</d:hasDNEEmail>
            <d:qbEditSequence m:null="true" />
            <d:qbSyncData m:type="Edm.Boolean" m:null="true" />
            <d:qbName m:null="true" />
            <d:createdDate m:type="Edm.DateTime">2015-05-15T14:49:27.06-04:00</d:createdDate>
          </m:properties>
        </content>
      </entry>
    </feed>
    EOF;
    
    
    $xml = simplexml_load_string($str);
    
    $xml->registerXPathNamespace( 'd', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' );
    $xpath = $xml->xpath( '//m:properties/d:*' );
    
    foreach($xpath as $key => $node)
    {
        echo $node->getName() . ' = ' . (string)$node . "\n";
    
        $subx = $node->xpath('@*');
    
        foreach($subx as $subnode) {
            echo "\t" . $subnode->getName() . ' = ' . (string)$subnode . "\n";
        }
    
        echo "-------------------------------------------------------\n";
    }
    

    这会产生以下输出:

    id = 00000001-A000-0000-1000-030102101201
    -------------------------------------------------------
    companyId = 2199
    -------------------------------------------------------
    firstName = Shannon
    -------------------------------------------------------
    lastName = Hall
    -------------------------------------------------------
    middleInitial = 
            null = true
    -------------------------------------------------------
    title = 
            null = true
    -------------------------------------------------------
    incomeCode = 
            null = true
    -------------------------------------------------------
    ageCode = 
            null = true
    -------------------------------------------------------
    structureAgeCode = 
            null = true
    -------------------------------------------------------
    reference = 
            null = true
    -------------------------------------------------------
    businessName = 
            null = true
    -------------------------------------------------------
    contactPhoneId = 1d32d3ad-c710-4963-a425-eb7853c4fdf9
    -------------------------------------------------------
    primaryAddressId = b8a78bb8-3895-492f-a4a2-f6e287081dcf
    -------------------------------------------------------
    website1 = 
            null = true
    -------------------------------------------------------
    website2 = 
            null = true
    -------------------------------------------------------
    website3 = 
            null = true
    -------------------------------------------------------
    email1 = 
            null = true
    -------------------------------------------------------
    isActive = true
            type = Edm.Boolean
    -------------------------------------------------------
    lastUpdate = 2015-05-15T14:49:45.143-04:00
            type = Edm.DateTime
    -------------------------------------------------------
    createdDate = 2015-05-15T14:49:45.143-04:00
            type = Edm.DateTime
    -------------------------------------------------------
    id = 992ABCSA-1231-8381-JKL8-38KDLALNA881
    -------------------------------------------------------
    companyId = 2100
    -------------------------------------------------------
    firstName = Jane
    -------------------------------------------------------
    lastName = Fonda
    -------------------------------------------------------
    middleInitial = 
            null = true
    -------------------------------------------------------
    title = Mrs.
    -------------------------------------------------------
    incomeCode = 
            null = true
    -------------------------------------------------------
    ageCode = 
            null = true
    -------------------------------------------------------
    structureAgeCode = 
            null = true
    -------------------------------------------------------
    reference = 
            null = true
    -------------------------------------------------------
    businessName = 
            null = true
    -------------------------------------------------------
    contactPhoneId = 427C367B-F490-412A-B9B0-ABABABABABAB
    -------------------------------------------------------
    primaryAddressId = f7668325-ba95-4bde-b28d-0226bbd4d9c7
    -------------------------------------------------------
    website1 = 
            null = true
    -------------------------------------------------------
    website2 = 
            null = true
    -------------------------------------------------------
    website3 = 
            null = true
    -------------------------------------------------------
    email1 = 
            null = true
    -------------------------------------------------------
    email1CanMail = false
            type = Edm.Boolean
    -------------------------------------------------------
    email2 = 
            null = true
    -------------------------------------------------------
    email2CanMail = false
            type = Edm.Boolean
    -------------------------------------------------------
    email3 = 
            null = true
    -------------------------------------------------------
    email3CanMail = false
            type = Edm.Boolean
    -------------------------------------------------------
    isTagged = 
            type = Edm.Boolean
            null = true
    -------------------------------------------------------
    structureValueCode = 
            null = true
    -------------------------------------------------------
    doNotMail = 
            type = Edm.Boolean
            null = true
    -------------------------------------------------------
    yearHomeBuilt = 
            null = true
    -------------------------------------------------------
    maritalStatus = 
            null = true
    -------------------------------------------------------
    lengthOfResidence = 
            null = true
    -------------------------------------------------------
    styleOfHome = 
            null = true
    -------------------------------------------------------
    mailMergeName = Jesse Peters
    -------------------------------------------------------
    source = Access - KGuard
    -------------------------------------------------------
    creationDate = 2015-05-15T14:49:27.06-04:00
            type = Edm.DateTime
    -------------------------------------------------------
    qbSyncDate = 
            type = Edm.DateTime
            null = true
    -------------------------------------------------------
    qbId = 
            null = true
    -------------------------------------------------------
    isActive = true
            type = Edm.Boolean
    -------------------------------------------------------
    lastUpdate = 2015-05-15T14:49:27.06-04:00
            type = Edm.DateTime
    -------------------------------------------------------
    hasDNCPhone = false
            type = Edm.Boolean
    -------------------------------------------------------
    hasDNEEmail = false
            type = Edm.Boolean
    -------------------------------------------------------
    qbEditSequence = 
            null = true
    -------------------------------------------------------
    qbSyncData = 
            type = Edm.Boolean
            null = true
    -------------------------------------------------------
    qbName = 
            null = true
    -------------------------------------------------------
    createdDate = 2015-05-15T14:49:27.06-04:00
            type = Edm.DateTime
    -------------------------------------------------------
    

    【讨论】:

    • 我需要测试;我无权访问 API 生成,所以我必须看看是否可以将 NS 信息动态插入到输入流中。如果可以的话,我很乐意将其标记为已回答。如果我做不到,你们有其他解决方案吗?
    • 我不知道在 PHP 中访问命名空间标签属性的另一种方法。
    • 没关系,我将流读入一个数组,然后将属性preg in....除非我不必; API 已更新为包含命名空间! ;) 感谢您的帮助!
    • 这个答案虽然侧重于一次获取所有 d 命名空间的标签及其属性 (//m:properties/d:*)。您的实现可能需要一种不同的方法,例如遍历 s,然后在每个 节点上使用直接 xpath 选择器,即 m:properties/d:* 不带前导 //
    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2010-10-25
    相关资源
    最近更新 更多