Jump to content

Archived

This topic is now archived and is closed to further replies.

Skatecrazy1

XML Parsing

Recommended Posts

Hey i'm kind of new to the whole XML/PHP thing, and I was trying some code out of a book that surprisingly didn't work. Here is the short XML File:

[code]
<?xml version = "1.0" encoding = "iso-8859-1" ?>

<books>

<title>
  <topic>JavaScript</topic>
  <series>in easy steps</series>
</title>

<title>
  <topic>C++Programming</topic>
  <series>in easy steps</series>
</title>

</books>
[/code]

and here is the PHP

[code]
<html><head><title>Reading DOM</title></head><body>

<?php
//load xml document into the DOM
$dom = new DomDocument;
$dom->load( "books.xml" );

//print out the root element name
echo("Root element name is: ");
echo( $dom -> documentElement -> nodeName );
echo("of the people in the XML file.");
echo( "<hr>" );

//print a list of names
echo("Topics include:<ul>");
$topics = $dom -> getElementsByTagName( "topic" );
foreach($topics as $node)
{
  echo("<li>" . $node -> textContent . "</li>");
}
echo( "</ul>" );
?>

</body>
</html>
[/code]

and when i try to use that code to parse the XML, i get this error

[quote]
Warning: domdocument() expects at least 1 parameter, 0 given in /home/content/s/n/a/snapskate/html/v2/pg/xmltest.php on line 5

Fatal error: Call to undefined function: load() in /home/content/s/n/a/snapskate/html/v2/pg/xmltest.php on line 6
[/quote]

does anyone know what's up?

Share this post


Link to post
Share on other sites
Which version of PHP are you using?  PHP4 uses the DOM XML extension, PHP5 uses the DOM extension, each which appear to have the same object names with different interfaces.

Share this post


Link to post
Share on other sites
I'm not exactly sure, but could you show me which version uses what so I can try both?

Share this post


Link to post
Share on other sites
yeah create a file called info.php and use <?php phpinfo(); ?> this will tell you the version and DOM/XML info too

Share this post


Link to post
Share on other sites
okay here's my php info [url=http://www.snapskate.com/info.php]http://www.snapskate.com/info.php[/url]

i can't really get anything from it as to why my code to parse the xml isn't working, so if anyone knows I'd appreciate some insight :P

apparently goDaddy still uses php 4

Share this post


Link to post
Share on other sites
Ok, php4 will be using the DOM XML extension, but that code you are trying is for DOM, which is with php5.  There's some examples for using DOM XML here:

http://www.php.net/manual/en/ref.domxml.php

Take a look in the user comments at the bottom of that page for some examples.

Good luck :)

Share this post


Link to post
Share on other sites
Look at how the domDocument object is created here:  http://www.php.net/manual/en/ref.domxml.php#61088

Instead of using "new", it's returned from a function call.  eg $domDocument = domxml_open_mem($string) will create a domDocument from a string.

Share this post


Link to post
Share on other sites
okay, thanks for the help, but

called godaddy, found out upgrading to the new server configuration (and PHP 5) was an option i had not yet enabled.

but, I used what you said and it worked fine.

thanks though.

Share this post


Link to post
Share on other sites

×

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.