Jump to content


Photo

XML Parsing


  • Please log in to reply
8 replies to this topic

#1 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 18 October 2006 - 01:36 AM

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:

<?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>

and here is the PHP

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

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

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


does anyone know what's up?
fuck it or fight it

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 03:03 AM

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.

#3 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 18 October 2006 - 02:12 PM

I'm not exactly sure, but could you show me which version uses what so I can try both?
fuck it or fight it

#4 edg322

edg322
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 October 2006 - 02:26 PM

yeah create a file called info.php and use <?php phpinfo(); ?> this will tell you the version and DOM/XML info too

#5 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 19 October 2006 - 04:59 AM

okay here's my php info http://www.snapskate.com/info.php

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
fuck it or fight it

#6 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 20 October 2006 - 03:26 AM

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/m.../ref.domxml.php

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

Good luck :)

#7 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 03:32 AM

so what would i replace my $dom = new DomDocument; with, in this case?
fuck it or fight it

#8 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 20 October 2006 - 04:15 AM

Look at how the domDocument object is created here:  http://www.php.net/m...omxml.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.

#9 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 04:40 AM

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.
fuck it or fight it




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users