PHPを使用して2つのことをしようとしています
1)xpath()を使用して、すべてのProductタグを選択するxpathクエリを実行します。これにより、各Productタグを表すSimpleXMLElementsの配列が返されます。
2)xpath()を再度使用して、各ProductタグからProductCodeとProductNameを選択し、それらの内容を取得します。
私のXMLは
<All_Products>
<Product>
<ProductCode>9978H</ProductCode>
<ProductName>abc with Buckle in aBlack</ProductName>
<ProductURL>http://abc.com/abc with Buckle in aBlack-p/9978H.htm<ProductURL>
<StockStatus>2</StockStatus>
</Product>
<Product>
<ProductCode>99s</ProductCode>
<ProductName>abc with Buckle in aBlack</ProductName>
<ProductURL>http://abc.com/abc with Buckle in aBlack-p/9978H.htm</ProductURL>
<StockStatus>2</StockStatus>
</Product>
</All_Products>
私のPHPコードは次のようになりますが、製品タグや製品コードなどが返されません。私のコードの問題は何ですか
$xml = simplexml_load_file('http://www.abc.com/xml.php');
if ($xml === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($xml);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$result = $xml->xpath('/All_Products/Product');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}