Read XML files in PHP
XML is very nice format for storing and especially transferring data between applications. Instead of old plain TXT files which you must parse from a strictly defined format, XML files gives your data 2nd dimension – a structure, so you basically “know” what data represents without need for parsing and cleaning it from rubbish.
Everybody is using it, so why shouldn’t you? :)
Also, one more advantage of using XML files is the fact that if someone add extra information into the file, your application will (probably) work without need for any changes, and that is great for systems that requires business continuity.
XML file is formated as a set of nodes and it’s sub-nodes, so when you’re trying to access some information you basically “asks” for a logical address and not a physical address in file.
Here’s an example.xml file:
-
-
<?xml version="1.0" encoding="UTF-8"?>
-
<tasks>
-
<note>
-
<title>Buy bread</title>
-
<detail>White bread, in a local supermarket.</detail>
-
</note>
-
<note>
-
<title>Go to dentist</title>
-
<detail>Bring SSN.</detail>
-
</note>
-
</tasks>
-
A long time ago, PHP wasn’t supporting XML files, so you had to parse those XML files as they are plain TXT.
Nowadays, there is a set of built-in instructions in core PHP engine that allows you to easily read and write XML.
Following is a simple example that will print out the previous listed XML example in human readable format:
-
-
<?
-
$objDOM = new DOMDocument();
-
$objDOM->load("example.xml");
-
-
$note = $objDOM->getElementsByTagName("note");
-
-
foreach( $note as $value )
-
{
-
$title = $value->getElementsByTagName("title");
-
$strTitle = $title->item(0)->nodeValue;
-
-
$detail = $value->getElementsByTagName("detail");
-
$strDetail = $detail->item(0)->nodeValue;
-
";
-
}
-
?>
-
The output of running the code from above should be something like this:
Buy bread - White bread, in a local supermarket.
Go to dentist - Bring SSN.
For more information and how to write XML files look at SimpleXML for PHP.
Darko Martić - IT Engineer, eBusiness and eCommerce Professional

March 29, 2010 @ 8:41 am
nice example
May 25, 2011 @ 11:21 pm
hi there, great code, helped me a lot allready. But can you tell me how I can integrate attributes in these lines?
Kind regards,
Barry
May 26, 2011 @ 8:59 am
@Barry, what exactly do you mean by integrating attributes ? Give an example… Thanx
May 26, 2011 @ 9:37 pm
hello , nice you reply this fast.
I have the XML underneath. Woning has an attribute identificatie (dutch for key), but I don’t know how to retrieve this using your code above..?
-
http://hoekstraenvaneck.nl/detail/WoningDetail.aspx?RecID=173349
749000
kk
koop
te koop
herenhuis,halfvrijstaande woning,Woonhuis
nee
1917
1021 HR
Amsterdam
6
May 26, 2011 @ 9:38 pm
hmm i see the XML tags are not being posted
You might check the XML at URL :
http://hoekstraenvaneck.nl/export/export.xml ?
May 29, 2011 @ 12:04 am
well let’s say in your example:
#
Buy bread
would be:
#
Buy bread
how would I then retrieve the chaper?
Kind regards,
Barry
May 29, 2011 @ 12:05 am
I dont know how to paste the code:
Buy bread
would be
#
Buy bread
how to retrieve chapter value ?
May 29, 2011 @ 12:06 am
please can i mail it?
if yes, where to ?