tags:

views:

69

answers:

4

What is the best way?

+2  A: 

SimpleXML and the PHP DOM are good bets.

Matthew Flaschen
A: 

By "get XML", do you mean retrieve it from a remote source, or parse a XML text that you already have?

For the former, either use PHP's built-in capability to load URL content (via the file_get_contents or similar functions applied to URLs), or use a library like cURL (which is included standard with most PHP distributions).

For the latter, libraries like SimpleXML (also included standard) can be quite useful.

Amber
First retrieve, then parse. And so that all the process is not error prone.
AleGore
+2  A: 

Use SimpleXML. It turns an XML document into an object that provides structured access to the XML.

To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.

As an example consider this:

$channel =<<<_XML_
<channel>    
 <title>Example title</title>    
 <link>http://example.com/&lt;/link&gt;    
 <description>Example desccription</description>    
</channel>    
_XML_;
// The XML that needs to be parsed

$xml = simplexml_load_string($channel); // create a SimpleXML object.

print "The $xml->title channel is available at $xml->link. ";

will print:

The Example title is available at http://example.com/.
codaddict
+1  A: 

You can use the following program also for creating the SimpleXml object. Using SimpleXml object you can access the xml tags.

test.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<employee>

        <name> Shyam </name>
        <age> 20 </age>
        <place> Chennai </place>
</employee>

file.php

<?php
$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
          {
            echo $child->getName() . ": " . $child . "<br />";
          }
?>
rekha_sri