Jump to content

How to display XML with XHTML?


LifeInBinary
 Share

Recommended Posts

I looked thru the XML tutorials on W3Schools, and feel fairly confident as to how to set up XML - but how difficult is it to tell my XHTML to display XML stuff? I mean, lets say that I wanted to display a long list of information - say information about maps for a game (IE: map_title, map_pic-01, map_description, map_download, map_download-link, etc...) - if I want to display the information in the SAME format every time - is there a way that I could use XML almost as a 'database' and tell the XHTML to display the information from the XML? This is a great example of what I am trying to do: http://www.mapraider.com/maps/search/index...e=&process=I would like to set up each map in almost a 'record' - and store info on everything to display under XML tags.To be honest, I know I probably need PHP and MySQL - but I have no experience with those, so I am hoping for an efficient 'work-around' here :/

Link to comment
Share on other sites

You need to use a third language to display XML data. It could even become "forth" if you add XSLT into the mix.It could be PHP or JavaScript. It's easier with PHP. You can just have something like

<?php$dom = new DOMDocument;$dom->load('file.xml');$items = $dom->documentElement->childNodes;foreach($items as $node) {echo $node->nodeValue,"\n\n";}?>

to write down the text content of any element right under the root element. Read the DOM tutorial, as well as the DOM reference on PHP.net for more info on that.The situation with JavaScript is almost the same, but the loading of the XML file is a little bit more tricky. W3Schools has a loadXMLDoc() function made for that.

Link to comment
Share on other sites

Thank you SO much boen_robot - I will certainly read those tutorials ASAP. That code snippet means nothing to me yet lol - but hopefully it will :)Would you be specifically opposed to me creating a very simple webpage that requires something very similar and getting you to help me make this work on a small scale, so that know I understand it?

Link to comment
Share on other sites

Thank you SO much boen_robot - I will certainly read those tutorials ASAP. That code snippet means nothing to me yet lol - but hopefully it will :)Would you be specifically opposed to me creating a very simple webpage that requires something very similar and getting you to help me make this work on a small scale, so that know I understand it?
Try to do that yourself, and ask if you have trouble making it. The above PHP code snippet should work with PHP5, and an XML like
<*><*>Text1</*><*>Text2</*></*>

Where "*" means "anything". With those text nodes, the output of the PHP code should be

Text1Text2

Link to comment
Share on other sites

Thanks for the explanation man, and yes I will give this a shot, thank you for your support.I've been reading up on PHP and XML on W3Schools, it seems like they don't 'hold your hand' as much for PHP as they did HTML lol, so I am left with lots of questions. Lucky for me tho, I really am serious about learning PHP (just hard on my own) - so I have a copy of the PHP5 & MySQL Bible laying on my desk that fills in the gaps on W3Schools pretty decently.I would ask tho, in your code, I gather that "\n\n" is responsible for the 2 blank lines between the text nodes? So would I be correct in saying that \n is the PHP equivelent to <br /><br />?

Link to comment
Share on other sites

I would ask tho, in your code, I gather that "\n\n" is responsible for the 2 blank lines between the text nodes? So would I be correct in saying that \n is the PHP equivelent to <br /><br />?
Sort of. If you have
echo 'Text1<br /><br />Text2';

That outputs

Text1<br /><br />Text2

which is displayed by the browser as

Text1Text2
because the browser believes you have HTML. When you have
echo "Text1\n\nText2";

the output will be

Text1Text2

but if the browser expects you to have an HTML document, you'll see

Text1 Text2
on the screen (because as you know, HTML truncates all whitespace to a single space).But considering the fact that PHP never deals with the browser like HTML does, I'd say that's sort of a correct statement.
Link to comment
Share on other sites

  • 2 weeks later...

If you send the file with a text/plain extension (e.g. .txt) then the browser will show the text verbatim. You can also send the file making the content-type header text/plain.

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
 Share

×
×
  • Create New...