Welcome to WebmasterWorld Guest from 54.166.46.226

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

XML Parsing Error

   
6:02 pm on Aug 29, 2010 (gmt 0)

5+ Year Member



Hello,
I have a script that parses XML file.
This script works perfectly in one server and but shows the following error in the other server :

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')' in /file.php on line 9

My PHP code is below :

<?php
$doc = new DOMDocument();
$doc->load('feed.xml');

$arrFeeds = array();
$count = 0;
foreach ($doc->getElementsByTagName('item') as $node)
{
$itemRSS = array ('id' => $node->getElementsByTagName('id')->item(0)->nodeValue, 'name' => $node->getElementsByTagName('name')->item(0)->nodeValue);

array_push($arrFeeds, $itemRSS);
$count = $count + 1;
}

for($i = 0; $i < $count; $i++)
{
echo $arrFeeds [$i]['id'];
echo "<br>";
echo $arrFeeds [$i]['name'];
echo "<br>";
}
?>

The server that runs the script well is with PHP 5 and the one that gives error is PHP 4.9.

Not sure if the version can cause this problem.

Please guide me. i need to run this in both the servers.
10:04 pm on Aug 29, 2010 (gmt 0)

5+ Year Member



DOMDocument is for PHP5 you need to look up DOM XML for PHP4
3:04 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



Thanks for the response.
But a few lines of code for PHP4 related to my code above that is for PHP5 will be helpful.
7:07 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



I want to parse the following file (lets call it products.xml) :

<?xml version="1.0" encoding="iso-8859-1" ?>
<products>
<item>
<name>Apples</couponid>
<price>10</merchantname>
</item>
<item>
<name>Oranges</couponid>
<price>11</merchantname>
</item>
<item>
<name>Grapes</couponid>
<price>12</merchantname>
</item>
</products>



<?php

$dom = domxml_open_file("products.xml");
$root = $dom->document_element();

$node_array = $root->get_elements_by_tagname("item");

for ($i = 0; $i<count($node_array); $i++) {
$node = $node_array[$i];
echo "The Item[$i] is: " . $node->get_content();
}

?>
This code is not working the way i need.

How do i get the data by <Tag> name so that i display it with the echo command anywhere in the page, like i have above done for php5. I have been able to do it in php5 but being able to get it done in php4.
Please help.
10:11 pm on Aug 30, 2010 (gmt 0)

5+ Year Member



Sorry I've not used used the PHP4 class before, best you check the PHP documentation, it looks pretty similar, this is from php.net

<?php

include("example.inc");

if (!$dom = domxml_open_mem($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}

$root = $dom->document_element();
echo $root->tagname(); // chapter
echo $root->tagname; // chapter
echo $root->node_name(); // chapter


?>
4:51 am on Aug 31, 2010 (gmt 0)

5+ Year Member



Thanks for the response.
I tried the above but it does not help.
Anyone please guide.
9:15 am on Aug 31, 2010 (gmt 0)

5+ Year Member



sorry this is more like what you want

<?php
if (!$dom = domxml_open_mem($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}

$root = $dom->document_element();

$node_array = $root->get_elements_by_tagname('element');

foreach ($node_array as $node) {
echo ' - ' . $node->get_content() . "\n";
}

?>
9:18 am on Aug 31, 2010 (gmt 0)

5+ Year Member



Btw if there is anyway you can upgrade to PHP5 I'd do that, as you've noticed XML in PHP4 is pretty bad, and you should be looking at using simple xml in PHP 5 it's a total breeze, honestly.
2:26 pm on Sep 2, 2010 (gmt 0)

5+ Year Member



I did the code and it work in PHP4.
Thanks for the help.