简体   繁体   中英

Filter php simple_xml load results with xpath

I need a little help to figure out XPath search inside XML output simple_xml_load in PHP. I have this XML:

<PRODUCTS>
  <PRODUCT>
    <CODE>5009444</CODE>
    <NAME>Prova</NAME>
    <IMG>prova.jpg</IMG>
  </PRODUCT>
  ....
  ....
</PRODUCTS>

I want to filter and iterate through this data to find and return all occurrences with a variable code. I used this syntax but didn't work

$id = 1;
$struct = \App\Models\Structures::where('id', $id)->first();
$url = 'http://demo.villaggissimi.it/public/xml/CMP_' . $struct->operators->pk .'.xml';
$xc = simplexml_load_file($url) or die("Non sono stati trovati risultati");
$xml2 = $xc->xpath("/PRODUCTS/PRODUCT[CODE='$struct->code']");
return response()->json($xml2);

The result of the Xpath Expression is a node list, SimpleXMLElement:xpath() only supports that kind of result, it will always return an array of SimpleXMLElement objects (for a valid Xpath expression).

So you still have to use foreach()/if() to avoid an error message if no element has been found.

You can however limit the result to a list with only a single node.

foreach ($xc->xpath("/PRODUCTS/PRODUCT[CODE='$struct->code'][1]") as $productNode) {
  return response()->json($productNode);
}
return FALSE;

$productNodes = $xc->xpath("/PRODUCTS/PRODUCT[CODE='$struct->code'][1]");
if (count($productNodes) > 0) {
  return response()->json($productNodes[0]);
}
return FALSE;

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