我创建xml文件如下,任何人都可以帮助如何使用PHP?创建xml文件
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";
}
是的,它正是我想要的。但如果我想在创建xml文件后回显“id”值,那么在加载xml文件后应该做什么修改 – rick 2011-05-13 03:39:36
@rick:您可以在末尾添加'echo $ product-> getAttribute('id');'行我的示例代码来回显'id'。 – Asaph 2011-05-13 03:42:32
我已经给出了上面的'检索XML数据'的例子,我可以用我的例子中的这条线......对不起,我是xml的新手 – rick 2011-05-13 03:50:37