get strings from XML using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

get strings from XML using php

Post by Sindarin »

I am trying to figure out how xml and php work together. It seems more complicated than fread().
I have several xml files in a folder which have the data I want to display on a page,
how can I read the sections and display them on the page using php?

Let's say the .xml file is:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<post>
<title><b>Welcome</b></title>
<description>welcome to my site.</description>
</post>
<post>
<title><b>2nd post</b></title>
<description>this is another post. <a href="http://www.google.com"> http://www.google.com </a></description>
</post>
<?xml>
So when read, it would display:

Welcome

welcome to my site.

2nd post

this is another post. http://www.google.com
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

(#10850)
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

So should I go with php 4 xml? Or it will become deprecated and replaced by simplexml?


Also the problem I have is that those examples are not easy.

I have found this one here: http://www.kirupa.com/web/xml_php_parse_beginner.htm
but I can't get html to work between each of the xml contents...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Try using the PHP DOM which follows the same standards as the W3C DOM. If you've ever used the DOM before (i.e. in JavaScript), it'll be very easy to work with.
Post Reply