0

i need to parse the following code

<ul class="zg_hrsr">
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">#15</span>
<span class="zg_hrsr_ladder">
in 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/ref=pd_zg_hrsr_kstore_1_1">Kindle Store</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/154606011/ref=pd_zg_hrsr_kstore_1_2">Kindle eBooks</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/157325011/ref=pd_zg_hrsr_kstore_1_3">Nonfiction</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/292975011/ref=pd_zg_hrsr_kstore_1_4">Lifestyle & Home</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156699011/ref=pd_zg_hrsr_kstore_1_5">Home & Garden</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156828011/ref=pd_zg_hrsr_kstore_1_6">Gardening & Horticulture</a>
 > 
<b>
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156847011/ref=pd_zg_hrsr_kstore_1_7_last">Greenhouses</a>
</b>
</span>
</li>
<li class="zg_hrsr_item">
<span class="zg_hrsr_rank">#26</span>
<span class="zg_hrsr_ladder">
in 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/ref=pd_zg_hrsr_kstore_2_1">Kindle Store</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/154606011/ref=pd_zg_hrsr_kstore_2_2">Kindle eBooks</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/157325011/ref=pd_zg_hrsr_kstore_2_3">Nonfiction</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/292975011/ref=pd_zg_hrsr_kstore_2_4">Lifestyle & Home</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156699011/ref=pd_zg_hrsr_kstore_2_5">Home & Garden</a>
 > 
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156828011/ref=pd_zg_hrsr_kstore_2_6">Gardening & Horticulture</a>
 > 
<b>
<a href="http://www.amazon.com/gp/bestsellers/digital-text/156849011/ref=pd_zg_hrsr_kstore_2_7_last">House Plants</a>
</b>
</span>
</li>
</ul>

and the output i desire is ,

  • Sellers Rank: #266,715 Paid in Kindle Store (See Top 100 Paid in Kindle Store)
  • #15 in Kindle Store > Kindle eBooks > Nonfiction > Lifestyle & Home > Home & Garden > Gardening & Horticulture > Greenhouses
  • #26 in Kindle Store > Kindle eBooks > Nonfiction > Lifestyle & Home > Home & Garden > Gardening & Horticulture > House Plants
  • how can i achieve this? All I know is that, i should get 'nodeValue' for each 'a' tag, but i am confused in getting them all in my required format, I guess i should use array, but i am un-able to implement it becuase of my low level of experty..

    Guideline and Help Please. I need only structure of xPath and array(if this could be done using array) or alternative to array..

    Zaffar Saffee
    • 6,167
    • 5
    • 39
    • 77
    • 1
      I would recommend [`SimpleXML`](http://php.net/manual/en/book.simplexml.php) over DOM here because it's much easier to use. I'm not going to write your code for you, however, if you view the SimpleXML documentation you'll see examples for how to use it. If you can't make it work from examples you might need to hire a programmer :) –  Feb 02 '12 at 20:03

    1 Answers1

    0
    //create XPath from you DOM object:
    $xpath = new DOMXPath($dom);
    foreach($xpath->query("//span[@class='zg_hrsr_rank']") as $rank){
        $rank = $rank->textContent;
        $trail = array();
        foreach($xpath->query('//a',$rank) as $step){
            $trail[] = $step->textContent;
        }
        echo $rank.' '.implode(' > ',$trail)."\n";
    }
    
    Wrikken
    • 69,272
    • 8
    • 97
    • 136
    • _sigh_, once again, it works here, I even tested it this time, I don't know how you do it ;) Are you sure you have that exact HTML? – Wrikken Feb 05 '12 at 22:09