How to parse an XML node with a colon tag using PHP -


i trying fetch value of following nodes [this url (takes quite time load)][1]. elements i'm interested in are:

title, g:price , g:gtin 

the xml starts this:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">   <channel>     <title>photospecialist.de</title>     <link>http://www.photospecialist.de</link>     <description/>     <item>       <g:id>ben107c</g:id>       <title>benbo trekker mk3 + kugelkopf + tasche</title>       <description>         benbo trekker mk3 + kugelkopf + tasche das benbo trekker mk3 ist eine leichte variante des beliebten benbo 1. sein geringes gewicht macht das trekker mk3 zum idealen stativ, wenn sie viel draußen fotografieren und viel unterwegs sind. sollten sie in eine situation kommen, in der maximale stabilität zählt, verfügt das benbo trekker mk3 über einen haken der mittelsäule. diesem können sie das stativ mit zusätzlichem gewicht bei bedarf beschweren. dank der zwei besonderen kamera-befestigungsschrauben können sie mit dem benbo trekker mk3 sehr nah boden fotografieren. nah, dass in vielen fällen die einzige einschränkung die größe ihrer kamera darstellt. in diesem set erhalten sie das benbo trekker mk3 zusammen mit einem kugelkopf, socket und einer tasche für den sicheren und komfortablen transport.       </description>       <link>         http://www.photospecialist.de/benbo-trekker-mk3-kugelkopf-tasche?dfw_tracker=2469-16       </link>       <g:image_link>http://static.fotokonijnenberg.nl/media/catalog/product/b/e/benbo_trekker_mk3_tripod_kit_with_b__s_head__bag_ben107c1.jpg</g:image_link>       <g:price>199.00 eur</g:price>       <g:condition>new</g:condition>       <g:availability>in stock</g:availability>       <g:identifier_exists>true</g:identifier_exists>       <g:brand>benbo</g:brand>       <g:gtin>5022361100576</g:gtin>       <g:item_group_id>0</g:item_group_id>       <g:product_type>tripod</g:product_type>       <g:mpn/>       <g:google_product_category>kameras & optik</g:google_product_category>     </item>   ...   </channel> </rss> 

to this, have written following code:

$z = new xmlreader; $z->open('https://my.datafeedwatch.com/static/files/1248/8222ebd3847fbfdc119abc9ba9d562b2cdb95818.xml');  $doc = new domdocument;  while ($z->read() && $z->name !== 'item')     ;  while ($z->name === 'item') {     $node = new simplexmlelement($z->readouterxml());     $a = $node->title;     $b = $node->price;     $c = $node->gtin;     echo $a . $b . $c . "<br />";     $z->next('item'); } 

this returns me title...price , gtin not showing.

the elements you're asking not part of default namespace in different one. can see because have prefix in name separated colon:

  ...   <channel>     <title>photospecialist.de</title>     <!-- title in default namespace, no colon in name -->     ...     <g:price>199.00 eur</g:price>     ...     <g:gtin>5022361100576</g:gtin>     <!-- price , gtin in different namespace, colon in name , prefixed "g" -->   ... 

the namespace given prefix, here "g" in case. , prefix namespace stands defined in document element here:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"> 

so namespace "http://base.google.com/ns/1.0".

when access child-elements name simplexmlelement do:

$a = $node->title; $b = $node->price; $c = $node->gtin; 

you're looking in default namespace. first element contains text, other 2 created on-thy-fly , yet empty.

to access namespaced child-elements need tell simplexmlelement explicitly children() method. creates new simplexmlelement children in namespace instead of default one:

$google = $node->children("http://base.google.com/ns/1.0");  $a = $node->title; $b = $google->price; $c = $google->gtin; 

so isolated example (yes, that's already).

a full example (including node-expansion on reader, code had bit rusty):

<?php /**  * how parse xml node colon tag using php  *  * @link http://stackoverflow.com/q/29876898/367456  */ const http_base_google_com_ns_1_0 = "http://base.google.com/ns/1.0";  $url = 'https://my.datafeedwatch.com/static/files/1248/8222ebd3847fbfdc119abc9ba9d562b2cdb95818.xml';  $reader = new xmlreader; $reader->open($url);  $doc = new domdocument;  // move first item element while (($valid = $reader->read()) && $reader->name !== 'item') ;  while ($valid) {     $default    = simplexml_import_dom($reader->expand($doc));     $googlebase = $default->children(http_base_google_com_ns_1_0);     printf(         "%s - %s - %s<br />\n"         , htmlspecialchars($default->title)         , htmlspecialchars($googlebase->price)         , htmlspecialchars($googlebase->gtin)     );      // move next item element     $valid = $reader->next('item'); }; 

i hope both gives explanation , broadens view little on xmlreader use well.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -