PHP ODATA XML แยกวิเคราะห์ด้วย SimpleXMLElement

ฉันมีสิ่งต่อไปนี้ส่งคืนเป็น XML จากแหล่งที่มา:

<content type="application/xml">
  <m:properties>
    <d:ID>30</d:ID>
    <d:Name></d:Name>
    <d:ProfileImageUrl>default.png</d:ProfileImageUrl>
    <d:ThumbnailUrl>default.png</d:ThumbnailUrl>
    <d:FavoriteCount m:type="Edm.Int64">0</d:FavoriteCount>
    <d:ViewCount m:type="Edm.Int64">12030</d:ViewCount>
    <d:LastMonthViewCount m:type="Edm.Int64">1104</d:LastMonthViewCount>
    <d:LastWeekViewCount m:type="Edm.Int64">250</d:LastWeekViewCount>
    <d:LastDayViewCount m:type="Edm.Int64">21</d:LastDayViewCount>
    <d:CreationDate m:type="Edm.DateTime">2011-03-28T13:46:54.227</d:CreationDate>
    <d:Enabled m:type="Edm.Boolean">true</d:Enabled>
    <d:UrlSafeName>t-boz</d:UrlSafeName>
    <d:LastDayFavoriteCount m:type="Edm.Int64">0</d:LastDayFavoriteCount>
    <d:LastWeekFavoriteCount m:type="Edm.Int64">0</d:LastWeekFavoriteCount>
    <d:LastMonthFavoriteCount m:type="Edm.Int64">0</d:LastMonthFavoriteCount>
    <d:IsOnTour m:type="Edm.Boolean">false</d:IsOnTour>
    <d:TodayRank m:type="Edm.Int32">6272</d:TodayRank>
    <d:WeekRank m:type="Edm.Int32">6851</d:WeekRank>
    <d:MonthRank m:type="Edm.Int32">6915</d:MonthRank>
    <d:AllTimeRank m:type="Edm.Int32">7973</d:AllTimeRank>
  </m:properties>
</content>

ฉันกำลังดึงข้อมูลนี้ผ่าน file_get_contents จากนั้นสร้างผ่าน SIMPLEXMLElement อย่างไรก็ตาม ฉันไม่สามารถเข้าถึงฟิลด์เนื้อหา -> คุณสมบัติ (เช่น ID, ชื่อ, ProfileImageUrl ฯลฯ ) ทั้งหมดที่ฉันเห็นจาก SIMPLEXMLElement มีดังต่อไปนี้:

[content] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => application/xml ) )

มีความคิดเห็นเกี่ยวกับวิธีที่ฉันรับข้อมูลนี้หรือไม่

ขอบคุณ!


person Nikon0266    schedule 29.02.2012    source แหล่งที่มา
comment
XML ของคุณไม่ถูกต้อง จำเป็นต้องกำหนดคำนำหน้าเนมสเปซ m และ d ในเอกสาร   -  person salathe    schedule 01.03.2012
comment
นี่เป็น XML ที่หลบเลี่ยงมาก ใช้คำนำหน้าเนมสเปซกับองค์ประกอบ แต่ไม่ได้ผูกคำนำหน้าเนมสเปซเหล่านั้น   -  person Francis Avila    schedule 01.03.2012
comment
ฟีดมีสิ่งนี้อยู่ด้านบน.. ขออภัยฉันยกเว้นไว้ก่อนหน้านี้ ‹?xml version=1.0 encoding=utf-8 standalone=yes?› ‹feed xml:base=odata .vevo.com/v1/Catalog xmlns:d=schemas.microsoft com/ado/2007/08/dataservices xmlns:m=สคีมา .microsoft.com/ado/2007/08/dataservices/metadata xmlns=w3.org/ 2005/อะตอม  -  person Nikon0266    schedule 01.03.2012


คำตอบ (1)


การเข้าถึงองค์ประกอบเนมสเปซนั้นง่ายดายด้วย SimpleXML คุณเพียงแค่บอกวิธี children() ว่าจะดูเมธอดใด

ตัวอย่างพื้นฐานขั้นสูงจะมีลักษณะดังนี้:

$xml = <<<XML
<content type="application/xml" xmlns:m="urn:m" xmlns:d="urn:d">
  <m:properties>
    <d:ID>30</d:ID>
    <d:ProfileImageUrl>default.png</d:ProfileImageUrl>
  </m:properties>
</content>
XML;

$content      = simplexml_load_string($xml);

// Quick way
// $properties = $content->children('m', TRUE)->properties->children('d', TRUE);
// echo $properties->ProfileImageUrl;

// Step by step
$m_elements   = $content->children('m', TRUE);
$m_properties = $m_elements->properties;
$d_elements   = $m_properties->children('d', TRUE);
echo $d_elements->ProfileImageUrl;
person salathe    schedule 29.02.2012