views:

356

answers:

2

Hello

I have this code

  <?PHP
      $content = '<html>
      <head>
         <title></title>
      </head>
      <body>
         <ul>
            <li style="border:0px" class="list" id="list1111">
            <a href="http://www.example.com/" style="font-size:10px" class="mylinks">
            <img src="logo.gif" width="235" height="97" alt="logo example" border="0"/>
            </a>
            </li>

            <li style="border:0px" class="list" id="list2222">
            <a href="http://www.example.com/2222222" class="mylinks">
            second link
            </a>
            </li>                                 
          </ul>
        </body>
        </html> ';

    $doc = new DOMDocument;
    $doc->loadhtml($content);
    $xpath = new DOMXPath($doc);
    $hrefs = $xpath->evaluate("/html/body//a");
    for ($i = 0; $i < $hrefs->length; $i++) {
            $href = $hrefs->item($i);
            $url = $href->getAttribute('href');                
            echo $url ."<br />";
    }
    ?>

this code is very simple it just retrieve all anchor tags from an HTML document I found it here

what I want is more complex :)

I want to retrieve all anchor tags + all children and parents and their attributes for every anchor tag

for example the result I want is when retrieving the first anchor tag is something like this

         1-html 
         2-body 
         3-ul 
         4-li(class:list,id:list1111,style:etc....) 
         5-a(href:www.example.com etc..) 
         6-img(width:257 etc)

I want to iterate from the top level to the lowest level for every anchor tag and I want to be able retrieve the attributes for each tag

It is very difficult for me because of "DOMXPath" :( however it might be easy for some of you

do you have any question?

do you know how to solve this problem?

Thanks in advance

+1  A: 

XPaths should make it so you don't need to iterate. To pull the important attributes of li use an XPath like:

//li/@class

or

//li/@id

which should give you an iterable object you can use.

Here's some more information on XPaths

Jweede
thanks, I am not looking for just the properties of the tags, the most important thing I am looking for is to find the parents and children of the "anchor" tag.
ahmed
you might consider using the 'parent::' and 'child::' axis' to make your iteration.
Jweede
A: 

Maybe you should write a simple XSLT stylesheet. Match the <a> tag, and then ancestor::* would give all parent nodes, child::* would give you all the children - you would have a lot more power using simple XPath syntax via XSLT.

Thiyagaraj