Jump to content

XML Parsing


Skatecrazy1

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?
Link to comment
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
Link to comment
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 :)
Link to comment
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.
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.