tags:

views:

91

answers:

3

Hi,

I have a simple XML extraction issue that should be solvable with straight PHP and not require any libraries.

All I need to do is extract the values of one tag. For example, given the string of XML:

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ....>
 <Result>Foo</Result>
 <Result>Bar</Result>
</ResultSet>

I just need to put Foo and Bar in an array. What is the easiest way to do this?

Thanks!

+7  A: 

There's simpleXML in PHP 5 that should get you there quickly.

Check out the basic examples page.

Pekka
+2  A: 

If PHP 5 you can use SimpleXML.

$xml = simplexml_load_string($data);
foreach ($xml->ResultSet as $val)
{
  echo $val->Result.' ';
}
JeremySpouken
+1  A: 
$xml = '<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
<Result>Foo</Result><Result>Bar</Result></ResultSet>';
$obj = new SimpleXMLElement($xml);
$result_array = array();
foreach ($obj->Result as $value) {
  $result_array[] = (string)$value;
}
jwhat