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

I'd use XSLT to produce HTML based on the XML document. Something like:

Code:
<xsl:template match="article">
    <div class="mydiv">
        <xsl:apply-templates/>
    </div>
</xsl:template>

<xsl:template match="title">
    <h1><a href="/news/>
        <xsl:value-of select="."/>
    </a></h1>
</xsl:template>

In the above each of the templates match parts of the news document (using XPath queries). The HTML tags are produced in the output together with the values etc. put in by the XSLT from the source document. Don't know if the above is syntactically accurate but it gives an idea.

You could try looking at the libxslt library, using DOMDocument and XSLTProcessor. The O'Reilly "PHP in a Nutshell book" contains an example of using the above (which is where I got that info from). There are a few good tutorials on XSLT around, such as on the w3c site. It's not easy stuff to learn but IMHO it's well worth the effort.

Hope that helps.

Jim
 
Hmm looks very interesting.

But is such a thing capable of creating quite complex sites? (Take ecommerce or forum software for example).
 
I'd say XSLT can be used to do complex stuff given the appropriate XML document. It's pretty powerful stuff, though not exactly intuitive.

I'm not doing the most complex XSLT things ever but have done some interesting stuff. For example, taking an XML document describing a tree structure, converting it into HTML, producing markup that is then picked up by JavaScript on the page and turned into a tree field.

Another example is transforming into HTML with XML data islands in it so as to have data on hand without unnecessarily going back to the server for it.

You can also do stuff such as merging documents together. Have a look at a few books for ideas (yet again the O'Reilly one is worth looking out for).

One thing I do find is that it's better to hold styling information for a page in CSS rather than writing it using the XSLT. That way you're keeping the HTML produced fairly simple, whilst also not binding the XSLT to producing HTML with a particular style.

You could also take the approach of only translating fragments for your document, linking them into an overall HTML document.

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

On PHP4, there's the DOMXML extension, but it's not compiled in by default and is not as straightforward as SimpleXML.

XSLT is a very good solution too. Personally I prefer to do the parsing server-side as it suits my methods of caching output and the like.

There are quite a few threads on parsing XML in PHP, such as this one and this one.
 
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?
 
It's enabled by default. You have to explicitly disable it for it not to be available. Is there a SimpleXML box in your phpinfo() output?
 
That's strange. Sorry, I don't have an answer for that at hand. Is it Windows or *nix?

phpinfo() should say something like:
Code:
Simplexml support	enabled
Revision 		$Revision: 1.151.2.22 $
Schema support		enabled
?

And the code:
Code:
$xml = simplexml_load_string('<foo></foo>');
echo $xml->asXML();
complains about undefined function? Make sure you're not auto_prepend'ing any scripts or inside another function's scope etc. that might be cause for issues.
 
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?
 
JonD said:
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?

Probably marginally slower, but you should be caching things anyway :k
 
I'd agree. SimpleXML introduces the additional overhead of loading the file and then parsing the nodes into an object before you can iterate over the result set. DB access doesn't have the same kind of overhead, and frequent queries are likely to be cached, providing a increase in speed for subsequent queries.

FYI, you can access node attributes just like you would an array key. So
Code:
$authorid = $a->author->attributes()->id;
can also be accessed with:
Code:
$authorid = $a->author['id']
Also be aware that data coming out of SimpleXML functions/calls is normally a SimpleXML object, so you must cast it appropriately, e.g. (int)$a->foo or (string)$a->bar, unless it's automagically converted.
 
Last edited:
Back
Top Bottom