Parsing XML with PHP..need some advice!

Associate
Joined
30 Dec 2005
Posts
415
Ok i've never done this before, but i've suddenly realised its the only solution.

I've got an xml file, which defines form field names, types, descriptions and sizes, as well as to what category they belong to and a few other parameters.
What I want to do is have PHP parse this xml file, and create the form fields accordingly on the page..

So say you have this:

<category="design">
<item="colours" />
<format="dropdown" />
<maxpoints="3" />
<text="Colours of the page" />
</item>
</category>

And the PHP would generate:
Code:
<p>Colours of the page: <select name="design_colours"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option> points. Comments: <input type="text" name="comments_design_colours" />

There would be multiple items within each category, and several categories.
Could anyone give me some help on generating this PHP from the XML file?
 
Which version of PHP are you on, 4 or 5? This is relatively simple on PHP5, as you can use SimpleXML. It's more tricky in PHP4 as you either need to compile in specific extensions, use 3rd-party packages or write the parser yourself.
 
Augmented said:
Which version of PHP are you on, 4 or 5? This is relatively simple on PHP5, as you can use SimpleXML. It's more tricky in PHP4 as you either need to compile in specific extensions, use 3rd-party packages or write the parser yourself.

Ah damn.

My home server is running PHP 5, but the one its going to be on is PHP 4.
 
I'm developing some XML stuff at the minute, although im doing it in .ASP
One of the developers i am working with has the entire thing in PHP and i took this out from one of his files:

PHP:
// A function to transform xml to html using an xsl stylesheet.
// It requires the PHP XSLT module to be enabled.
// The module uses the Expat open-source XML parser and Sablotron open-source XSLT processor,
// so these must be installed too.
function transform_it($xml, $xsl) { 
 // Create an XSLT processor 
 $xsltproc = xslt_create(); 

 // Perform the transformation 
 $html = xslt_process($xsltproc, $xml_doc, $xsl_stylesheet); 

 // Detect errors 
 if (!$html) die('XSLT processing error: '.xslt_error($xsltproc)); 

 // Destroy the XSLT processor 
 xslt_free($xsltproc); 

 // Output the resulting HTML 
 return $html;
}

In the comments it appears to tell you what you need installed on it to make it work. You will need to use XSL(t) to display it on a xHTML page.

I personally found XSL to be very tricky to get the hang of, but others pick it up quickly. It's probably the best way to make any sense of your data.
 
Back
Top Bottom