XML processing with PHP



by Administrator author list

PHP and XML
PHP has its own built in functions for XML handling. To process an XML file first of all you need an XML parser. You can create it with the following code:

  $parser xml_parser_create();


This code creates an empty XML parser object which will process our XML file later. The parser requires the XML document as a string so we read the file as follows:

  $document file_get_contents("test.xml");


Now we have an XML document in string format and an XML parser. Let's process the file.

  xml_parse($parser$document);


After processing we free up the resources:

  xml_parser_free($parser);


The complete code looks like this at the moment:

<?php
   $parser 
xml_parser_create();
   
$document file_get_contents("test.xml");
   
xml_parse($parser$document); 
   
xml_parser_free($parser);
?>
  

And our test XML file (test.xml), which is located in the same directory as our PHP script:

<carlist>
  <car type="Mercedes">S 600</car>
  <car type="Mercedes">E 270 CDI</car>
  <car type="BMW">535 D</car>
  <car type="Lexus">IS 220</car>
</carlist>
  

Try to execute it!
What happened?
Nothing.
It is normal as we didn't define what to do with the results and how the result should look like. Then for example how can I list all Mercedeses?


article index
page 1 : untitled page
page 2 - current : Part 2
page 3 : Part 3
page 4 : Part 4
page 5 : Part 5
page 6 : Part 6


Tags:

Php Toys - 2006 - Php resources, scripts and tutorials - Privacy Policy
Insurance Index - Tutorial collection - Forex trading, brokers, reviews - Mortgage payment calculator
{THEMEDISCLAIMER}
Render time: 0.1283 sec, 0.0176 of that for queries. DB queries: 25.