0

I am using the following code

    $page_data=array();//$title_links is array having urls
     $nodearr=array();
    foreach ($title_links as $b_url)
    {
        $page_data[]= mycurl($b_url);//my curl function, it is okay

        $dom2 = new DOMDocument();//creating new domdocument object inside a domdocument object
        @$dom2->loadHTML($page_data[]); 
        $xpath_cat = new DOMXPath($dom2);//same as domdocument , nesting xpath
        $nodelist = $xpath_cat->query('//span[@class="zg_hrsr_ladder"]', $dom2);
//echo nodeslist;

foreach ($nodelist as $node) {
   $nodearr[] = $node->textContent;
 }


    }

so the scenerio is ,

  • I am nesting a 'domdocument' inside an other 'DomDocument'
  • using 'foreach loop', i am inserting Array Element in DomDocument
  • aslo, as i am nestind domdocument inside domdocument , so i am declaring new xpath query
  • The Question is ...

  • Doing so is okay? Is it a acceptable Programming technique?
  • Pros and Cons?
  • Any ALternative to it?

    Clarification:

  • $title_links is obtained using curl, it is urls extracted from web page using domdocument and xpath query.
  • so $title_links is at first level of xpath and domdocument, and $nodelist is at the 2nd level of nesting od xpath and dom-document
  • Zaffar Saffee
    • 6,167
    • 5
    • 39
    • 77
    • What exactly is the end goal? And how are you nesting DOMDocuments exactly - are you extending that class and this is code one of the methods of your extension? – prodigitalson Feb 05 '12 at 20:58
    • No, I am not using OOP or classes in the code, its simple php code, and the final thing, what i have is, i want to run xpath query on the pages brought by "mycurl()", failing in running the query. So i decided to make the concept clear before proceding further – Zaffar Saffee Feb 05 '12 at 21:01
    • 1
      unfortunately, neither concept nor scenario is clear to me. Why are you using cURL to fetch the URLs, instead of http://php.net/manual/en/domdocument.loadhtmlfile.php? And what do you mean by nesting XPath? The code above is not helping to illustrate your goals. If you want to query multiple documents, do it one document at a time. – Gordon Feb 05 '12 at 23:20

    1 Answers1

    0

    Your reason its failing is this: @$dom2->loadHTML($page_data[]);

    [] can only be used for assignment, not reading. You need to actullay have a key you can reference. I would probably do it like this:

    $page_data=array();//$title_links is array having urls
    foreach ($title_links as $key => $b_url)
    {
        $page_data[$key]= mycurl($b_url);//my curl function, it is okay
    
        $dom2 = new DOMDocument();
        $dom2->loadHTML($page_data[$key]); // error suppression is evil!
        $xpath_cat = new DOMXPath($dom2);
    
    }
    

    Whats still unclear to me is how exactly there is any nesting going on here, youre not nesting anything you are just iterating over an array.

    prodigitalson
    • 60,050
    • 10
    • 100
    • 114