Jump to content

Displaying Data From An Xml On A Web Page, Best Way?


Recommended Posts

I've skimmed through the W3schools tutorials, and it seems the whole XML DOM thing uses Java Script in addition to PHP and HTML.I'd like to avoid using Java Script if at all possible.Whats the best way to do this? Should I just bite the bullet and use Java Script? Or is there a better/different way?[EDIT]: A friend suggested using a PHP script to pass the data in the XML to a MySQL database and then pulling the data into the webpage from there. How easy would that be? taking into account that I know virtually nothing of MySQL.Some details on exactly what I wish to achieve:I want to have a link on a web page that when someone clicks it, it will open a new window, and in that window it will display some data that is retrieved from an XML document. The data will be nicely laid out and easy to read. I have already done the HTML and CSS for the new window that opens (for displaying the data nicely) now what I have to do is do the code for taking the data from the XML and putting it into the correct places in the style sheet.I also want it to be able to do this for more than one XML document, depending on which link the user clicks on.I was thinking of using the PHP '$_GET['p']' function for getting a text string at the end of the URL to determine which XML file to load.Thanks, Koiju

Edited by koiju
Link to comment
Share on other sites

You must bite the bulltet and use DOM. However, using DOM doesn't imply using JavaScript.PHP has a DOM API, which is very complete and easy to learn if you take it from the top.And here's the top.And here are just a few things you might want to do, some of which are not available in JavaScript (or are available, but not in the same fashion), but are available in other DOM implmentations:

$dom = new DOMDocument;$dom->load('file/to/load.xml');//output the XML file "as is"echo $dom->saveXML();

$dom = new DOMDocument;$dom->load('file/to/load.xml');$dom->formatOutput = false;//Remove empty whitespaces from the document//output the XML file "as is"echo $dom->saveXML();

$dom = new DOMDocument;$dom->load('file/to/load.xml');echo $dom->getElementsByTagName('title')->item(0)->nodeValue;//Ouptuts the vaue of the first "title" element in the document.

$dom = new DOMDocument;$dom->load('file/to/load.xml');//To effectively use this, you'll have to read the XPath tutorial$xpath = new DOMXPath($dom);echo $xpath->evaluate('/*/*[2]/text()');//Output the contents of the second element under the root element

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...