【发布时间】:2011-05-13 02:48:55
【问题描述】:
我正在创建如下的 xml 文件,任何人都可以帮助如何使用 php 来获取它?
xml 文件:
<products>
<product id="123" />
</products>
php 文件:
我正在使用以下代码,但没有得到上述结果
$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$products= $xml->createElement('products');
$product = $xml->createElement('product');
$id = $xml->createElement('id');
$xml->appendChild($products);
$products->appendChild($product);
$product->appendChild($id);
$id->nodeValue = 123;
$xml->save("data.xml");
检索 xml 数据:
$xml = new DomDocument();
$xmlFile = "data.xml";
$xml= DOMDocument::load($xmlFile);
$product = $xml->getElementsByTagName("product");
foreach($product as $node)
{
$address = $node->getElementsByAttributeName("address");
$address = $address->item(0)->nodeValue;
echo"$address";
}
【问题讨论】: