homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
XML Parsing Error
pemba76




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

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.

 

optik




msg:4193858
 10:04 pm on Aug 29, 2010 (gmt 0)

DOMDocument is for PHP5 you need to look up DOM XML for PHP4

pemba76




msg:4194041
 3:04 pm on Aug 30, 2010 (gmt 0)

Thanks for the response.
But a few lines of code for PHP4 related to my code above that is for PHP5 will be helpful.

pemba76




msg:4194135
 7:07 pm on Aug 30, 2010 (gmt 0)

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.

optik




msg:4194192
 10:11 pm on Aug 30, 2010 (gmt 0)

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


?>

pemba76




msg:4194295
 4:51 am on Aug 31, 2010 (gmt 0)

Thanks for the response.
I tried the above but it does not help.
Anyone please guide.

optik




msg:4194356
 9:15 am on Aug 31, 2010 (gmt 0)

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

?>

optik




msg:4194357
 9:18 am on Aug 31, 2010 (gmt 0)

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.

pemba76




msg:4195577
 2:26 pm on Sep 2, 2010 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved