Ich versuche, ein Futter ähnlich den folgenden zu analysieren:PHP - Parsen von XML Herausforderung
<?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>
aber ich habe Probleme, die Namespace Unter < Eintrag> -Tags greifen. Es ging gut, als ich nur 1 zurück bekam, aber jetzt, wo ich mehrere Male ziehe, habe ich wirklich Mühe, eine gute Auswahl an Objekten zu bekommen.
Leider habe ich so viele verschiedene Formen meiner Funktion geschrieben, ich bin mir nicht einmal sicher, welcher dem nächsten kommt.
Beispiel 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;
}
Beispiele 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; }
Hoffentlich können Sie sehen, was ich zu tun werde versuchen. Wenn es darauf ankommt, kommt dies über einen Stream mit Guzzle zustande. Grundsätzlich brauche ich ein Array von Objekten, wobei jedes Objekt die verschiedenen Eigenschaften von < m: properties> enthält, idealerweise ohne ihr d: -Präfix.
Beachten Sie, dass basierend auf dem, was ich an meine Abfrage anfügen, jeder der relevanten < Link ... Abschnitte Daten haben könnte.
Danke für die Hilfe!
Ich muß Prüfung; Ich habe keinen Zugriff auf die API-Generierung. Daher muss ich sehen, ob ich die NS-Informationen dynamisch in den Eingabestream einfügen kann. Wenn ich kann, werde ich gerne als beantwortet markieren. Haben Sie alternative Lösungen, wenn ich das nicht machen kann? –
Ich kenne keine andere Möglichkeit, auf namespace-Tag-Attribute in PHP zuzugreifen. – marekful
Es ist in Ordnung, ich lese den Strom in ein Array und dann preg das Attribut in .... außer ich muss nicht; Die API wurde aktualisiert und enthält jetzt den Namespace! ;) Danke für die Hilfe! –