Martic.net

Talking in binary since 1980.

Read XML files in PHP

Darko at 1:34 pm on Wednesday, August 12, 2009

PHP with XMLXML 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:

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <tasks>
  4. <note>
  5. <title>Buy bread</title>
  6. <detail>White bread, in a local supermarket.</detail>
  7. </note>
  8. <note>
  9. <title>Go to dentist</title>
  10. <detail>Bring SSN.</detail>
  11. </note>
  12. </tasks>
  13.  

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:

  1.  
  2. <?
  3.   $objDOM = new DOMDocument();
  4.   $objDOM->load("example.xml");
  5.  
  6.   $note = $objDOM->getElementsByTagName("note");
  7.  
  8.   foreach( $note as $value )
  9.   {
  10.     $title = $value->getElementsByTagName("title");
  11.     $strTitle  = $title->item(0)->nodeValue;
  12.  
  13.     $detail = $value->getElementsByTagName("detail");
  14.     $strDetail  = $detail->item(0)->nodeValue;
  15.     echo $strTitle." – ".$strDetail."
  16. ";
  17.   }
  18. ?>
  19.  

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.

 

8 Comments »

  1. Comment by som
    March 29, 2010 @ 8:41 am

    nice example

  2. Comment by barry sakkers
    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

  3. Comment by Darko
    May 26, 2011 @ 8:59 am

    @Barry, what exactly do you mean by integrating attributes ? Give an example… Thanx

  4. Comment by barry sakkers
    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

  5. Comment by barry sakkers
    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 ?

  6. Comment by barry
    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

  7. Comment by 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 ?

  8. Comment by barry
    May 29, 2011 @ 12:06 am

    please can i mail it?
    if yes, where to ?

RSS feed for comments on this post. TrackBack URL

Leave a comment

Enter this code

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 
Close
E-mail It
Socialized through Gregarious 42