脚本宝典收集整理的这篇文章主要介绍了使用PHP simpleXml将子项添加到xml,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?xml version="1.0" encoding="utf-8"?> <root> <ITems> <item>abc</item> <item>def</item> <item>ghi</item> </items> </root>
$xml = simplexml_load_file("myxml.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $newItem = $sxe->addChild("items"); $newItem->addChild("item",$newValue); $sxe->asXML("myxml.xml");
这是结果:
<?xml version="1.0" encoding="utf-8"?> <root> <items> <item>abc</item> <item>def</item> <item>ghi</item> </items> <items> <item>jkl</item> </items> </root>
这会创建新项目节点,但我想将项目添加到相同的现有项目节点.
$xml = simplexml_load_file("myxml.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $itemsnode = $sxe->items[0]; $itemsNode->addChild("item",$newValue); $sxe->asXML("myxml.xml");
以上是脚本宝典为你收集整理的使用PHP simpleXml将子项添加到xml全部内容,希望文章能够帮你解决使用PHP simpleXml将子项添加到xml所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。