简体   繁体   中英

How to extract tags from XML

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!

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

Check out the basic examples page .

If PHP 5 you can use SimpleXML.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM