3 回答
TA贡献1829条经验 获得超7个赞
如果您使用DOMDocument并且DOMXPath可以使用 XPath 查询从 XML 中找到您需要的信息。
$xml ='<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/" xmlns:ns2="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns2:GetProductResponse>
<ns2:Product>
<ns1:productId>1322059</ns1:productId>
<ns2:productName>Smart WiFi Security Camera</ns2:productName>
<ns2:productBrand>AAkron Line</ns2:productBrand>
<ns2:export>false</ns2:export>
<ns2:ProductCategoryArray>
<ns2:ProductCategory>
<ns2:category>Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019</ns2:category>
<ns2:subCategory>N/A</ns2:subCategory>
</ns2:ProductCategory>
</ns2:ProductCategoryArray>
</ns2:Product>
</ns2:GetProductResponse>
</env:Body>
</env:Envelope>';
$dom=new DOMDocument;
$dom->loadXML( $xml );
$xp=new DOMXPath( $dom );
$xp->registerNamespace('ns1','http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/');
$xp->registerNamespace('ns2','http://www.promostandards.org/WSDL/ProductDataService/1.0.0/');
$xp->registerNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance');
$xp->registerNamespace('env','http://www.w3.org/2003/05/soap-envelope');
$products=$xp->query('//ns2:Product');
if( $products->length > 0 ){
foreach( $products as $product ){
$id=$xp->query( 'ns1:productId', $product )->item(0)->nodeValue;
$name=$xp->query( 'ns2:productName', $product )->item(0)->nodeValue;
$brand=$xp->query( 'ns2:productBrand', $product )->item(0)->nodeValue;
$export=$xp->query( 'ns2:export', $product )->item(0)->nodeValue;
$category=$xp->query( 'ns2:ProductCategoryArray/ns2:ProductCategory/ns2:category', $product )->item(0)->nodeValue;
$subcategory=$xp->query( 'ns2:ProductCategoryArray/ns2:ProductCategory/ns2:subCategory', $product )->item(0)->nodeValue;
echo $id, $name, $brand, $category, $subcategory;
}
}
输出:
1322059Smart WiFi Security CameraAAkron LineCameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019N/A
TA贡献1875条经验 获得超5个赞
您还可以使用 SimpleXML 和Xpath。
function getNsValue($xml, $path, $index = 0){
return (string)$xml->xpath($path)[$index];
}
xml测试数据:
$xml ='<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/SharedObjects/" xmlns:ns2="http://www.promostandards.org/WSDL/ProductDataService/1.0.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<ns2:GetProductResponse>
<ns2:Product>
<ns1:productId>1322059</ns1:productId>
<ns2:productName>Smart WiFi Security Camera</ns2:productName>
<ns2:productBrand>AAkron Line</ns2:productBrand>
<ns2:export>false</ns2:export>
<ns2:ProductCategoryArray>
<ns2:ProductCategory>
<ns2:category>Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019</ns2:category>
<ns2:subCategory>N/A</ns2:subCategory>
</ns2:ProductCategory>
</ns2:ProductCategoryArray>
</ns2:Product>
</ns2:GetProductResponse>
</env:Body>
</env:Envelope>';
$data_xml = simplexml_load_string($xml);
例子:
$category = getNsValue($data_xml,'//ns2:category');
$productId = getNsValue($data_xml,'//ns1:productId');
var_dump($category, $productId);
/*
string(91) "Cameras, Safety, Health Care, Insurance, Finance, Tech Industry, NEW for 2018, New for 2019"
string(7) "1322059"
*/
TA贡献1850条经验 获得超11个赞
我已经使用以下代码解决了我的问题-
$response = file_get_contents($xml);
$res=simplexml_load_string($response);
print_r($res);
- 3 回答
- 0 关注
- 155 浏览
添加回答
举报