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:前綴。
請注意,根據我追加到我的查詢,任何相關的<鏈接...部分可能有數據。
感謝您的幫助!
我需要測試;我無法訪問API生成,因此我必須查看是否可以將NS信息動態插入輸入流。如果可以的話,我會很樂意回答。如果我無法完成這項工作,您是否有其他解決方案? –
我不知道另一種方式來訪問PHP中的名稱空間標籤屬性。 – marekful
沒關係,我將流讀入數組,然後在...中預置屬性,除非我不必;該API已更新爲包含命名空間! ;) 謝謝您的幫助! –