Jump to content

foreach dom problem


beermaker74

Recommended Posts

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;
            }

 

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.

Somebody mentioned that the syntax I am using is for php 5. They recomended changing'

foreach ($dom->documentElement->childNodes as $books) {

 

to

foreach ($dom->document_element->child_nodes as $books) {

it is still giving me the same error

 

my main goal in this is to display feeds from google base. So all i need to do is parse and display the feed in my own format. This code does exactly what I want in php 5. but im stuck woth 4.4.4

Link to comment
Share on other sites

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'); 
             }*/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.