我有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>
这会创建新项目节点,但我想将项目添加到相同的现有项目节点.
那么,你不应该创建新的项目节点:
原文链接:https://www.f2er.com/php/130418.html$xml = simplexml_load_file("myxml.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $itemsNode = $sxe->items[0]; $itemsNode->addChild("item",$newValue); $sxe->asXML("myxml.xml");