foreach dom problem

Discussion in 'PHP' started by beermaker74, Feb 28, 2007.

  1. #1
    ok i am trying to parse some xml and it is becoming a royal pain in my arse.

    $base = 'http://google.com/base/feeds/snippets/-/';
    $params = 'rental+%5blocation:@%22San+Francisco,CA%22%2b50mi%5d&max-results=3';
    
    $url = $base . 'housing?bq=' . $params;
    
    $c = curl_init($url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($c);
    curl_close($c);
    
    $dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $errors);
    
      foreach ($dom->documentElement->childNodes as $books) {
         if (($books->nodeType == 1) && ($books->nodeName == "entry")) {
    
              foreach ($books->childNodes  as $theBook) {
                  if (($theBook->nodeType == 1) &&
                ($theBook->nodeName == "id")) {
                     $id = $theBook->textContent;
                }
    Code (markup):
    It gives me an error in the foreach statement. It works fine on my local machine with php5. But not on my server with php4.4.4

    here is the error
    Warning: Invalid argument supplied for foreach()
    before I had
    $doc = new DOMDocument();
    $doc->load( 'books1.xml' );
    to start the dom and I was getting errors. undefined function etc. dom needs a parameter. Once again worked fine on php5 but not on my server. So i changed that to
    $dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $errors);
    and that seemed to solve that problem but then the foreach problem came up. thanks
     
    beermaker74, Feb 28, 2007 IP
  2. PhatDV

    PhatDV Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The DOM XML extension is different in PHP4, from the PHP website:

    You can make it work, but it's not interchangable between PHP4 and PHP5, so your code:

    foreach ($dom->documentElement->childNodes as $books) {
    Code (markup):
    would become:

    foreach ($dom->document_element->child_nodes as $books) {
    Code (markup):
    in PHP4. Note that the above is untested - I don't have access to a server with the DOM XML extension loaded into PHP4 at present.
     
    PhatDV, Feb 28, 2007 IP
  3. beermaker74

    beermaker74 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok going thru the xml dom for php 4 i came up with this that works
    for anybody else having the same problem


    $dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $errors);
    $noderoot = $dom->document_element();
    $childnodes = $noderoot->child_nodes();
    
      foreach($childnodes as $books) {
    
         if (($books->node_type() == 1) && ($books->node_name() == "entry")) {
              foreach ($books->child_nodes()  as $theBook) {
                  if (($theBook->node_type() == 1) &&
                ($theBook->node_name() == "id")) {
                     $id = $theBook->get_content();
    				 
                }
    			
    			if (($theBook->node_type() == 1) &&
                 ($theBook->node_name() == "published")) {
                     $published = $theBook->get_content();
                 }
    			 
                 if (($theBook->node_type() == 1) &&
                 ($theBook->node_name() == "updated")) {
                     $updated = $theBook->get_content();
                 }
    			 
    			/* if (($theBook->node_type() == 1) &&
                 ($theBook->node_name() == "category")) {
                     $category = $theBook->get_attribute('term'); 
                 }*/
    Code (markup):
     
    beermaker74, Mar 1, 2007 IP