2017-07-28 77 views
0

我試圖解析類似如下的飼料:PHP - 解析XML挑戰

<?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; } 

希望你能看到我想要做的事。如果它很重要,則通過Stream使用Guzzle進入。基本上我需要一個對象數組,其中每個對象包含< m:properties>的各個屬性,理想情況下不帶d:前綴。

請注意,根據我追加到我的查詢,任何相關的<鏈接...部分可能有數據。

感謝您的幫助!

回答

1

使用Xpath。

d命名空間不是XML字符串中定義但加入xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"<feed>標籤,下面的工作時:

<?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 
------------------------------------------------------- 
+0

我需要測試;我無法訪問API生成,因此我必須查看是否可以將NS信息動態插入輸入流。如果可以的話,我會很樂意回答。如果我無法完成這項工作,您是否有其他解決方案? –

+0

我不知道另一種方式來訪問PHP中的名稱空間標籤屬性。 – marekful

+0

沒關係,我將流讀入數組,然後在...中預置屬性,除非我不必;該API已更新爲包含命名空間! ;) 謝謝您的幫助! –