PHP/XML

Associate
Joined
26 Jun 2003
Posts
1,140
Location
North West
Im trying to create an PHP/XML parser.

I have googled and looked at php.net/xml but I havnt found any tutorials which tell u how to do things in practice.

So for example take this simple xml file: (Simple news content).

Code:
<?xml version="1.0" encoding="iso-8859-1"?>

<news>
	<article id="1">
		<title>Heading 1</title>
		<author id="32">George Bush</author>
		<content>Bla bla bla.</content>
	</article>	
	<article id="2">
		<title>Heading 2</title>
		<author id="59">Tony Blair</author>
		<content>Bla bla bla.</content>
	</article>
</news>

Needs to be displayed for example like:

<div class="myDiv">
<h1><a href="/news/1">Heading 1</a></h1>
<h2><a href="/user/32">George Bush</a></h2>
<p>Bla bla bla.</p>
</div>

<div class="myDiv">
<h1><a href="/news/2">Heading 2</a></h1>
<h2><a href="/user/59">Tony Blair</a></h2>
<p>Bla bla bla.</p>
</div>

Can anyone point me in the right direction of how to do this?

Thanks a lot.
 
Oh and im trying to look at simplexml like:

simplexml_load_string("example.xml"); but it is not finding the function... :confused:

Fatal error: Call to undefined function: simplexml_load_string() in /home/jond/public_html/index.php on line 4
 
Hmm looks very interesting.

But is such a thing capable of creating quite complex sites? (Take ecommerce or forum software for example).
 
Augmented said:
SimpleXML is PHP5 only. If you only want to parse RSS/RDF, there are a number of prebuilt scripts/libraries to do the hard work for you e.g. Magpie. SimpleXML is a built in extension, so it's very fast and very easy to get to grips with. It does have a few odd issues with things like default namespaces and such that aren't very clear in the manual, but you may not come across them in normal usage.

Im using the latest version of PHP5 (With xammp) and it isnt finding any of its functions. Any ideas?
 
Yeh thats exacally what my phpinfo() says.

I will check how im calling the function when I get home from work.
 
Grrrr.

My localhost is running php5 but when i was uploading to my server that was running php4. All sorted now tho.

Thx
 
Anyway jsut for reference purposes here is how i did it:

Code:
foreach($xml->article as $a)
{

  $newsid   = $a->attributes()->id;	
  $heading  = $a->title;	
  $author   = $a->author;
  $authorid = $a->author->attributes()->id;	
  $content  = $a->title;

  echo '<h1><a href="'.$newsid.'">'.$heading.'</a></h1>';
  echo '<h2><a href="'.$authorid.'">'.$author.'</a></h2>';
  echo '<p>'.$content.'</p>';

}

Anyway now that is sorted so I can move on.

Is this considerably quicker than doing mysql connect, select data you want, looping around it and printing? There is considerably less lines of code anyway.

I plan to create the XML files whenever a new news article or whatever has been posted.

So is this better than using a mysql database to extract at run time?
 
Back
Top Bottom