使用PHP往一个XML添加一个节点并给节点一个属性值 10
使用如下代码出错$doc=newDOMDocument();$doc->load("newtext.xml");$r=$doc->getElementsByTagName...
使用如下代码出错
$doc=new DOMDocument();
$doc->load("newtext.xml");
$r=$doc->getElementsByTagName("EVENTLIST")->item(0);
foreach ($keys as $index){
// echo $index."<br>";
$id++;
$event=$doc->createElement("EVENT");
$event->appendChild($doc->createTextNode($index));
$eventid=$doc->createAttribute("id");
$content="E".$id;
$eventid->appendChild($doc->createTextNode($content));
$event->appendChild($eventid);
$r->appendChild($event);
}
$doc->save("newtext.xml");
XML文件格式如下
<EVENTLIST>
</EVENTLIST>
以上程序哪里有问题 展开
$doc=new DOMDocument();
$doc->load("newtext.xml");
$r=$doc->getElementsByTagName("EVENTLIST")->item(0);
foreach ($keys as $index){
// echo $index."<br>";
$id++;
$event=$doc->createElement("EVENT");
$event->appendChild($doc->createTextNode($index));
$eventid=$doc->createAttribute("id");
$content="E".$id;
$eventid->appendChild($doc->createTextNode($content));
$event->appendChild($eventid);
$r->appendChild($event);
}
$doc->save("newtext.xml");
XML文件格式如下
<EVENTLIST>
</EVENTLIST>
以上程序哪里有问题 展开
1个回答
2015-04-13
展开全部
$xml =<<<EOT
<?xml version="1.0"?>
<root>
<section name="Section1">
<category id="Category1" name="google">
</section>
</root>
EOT;
$objXML=simplexml_load_string($xml);
$objXML->section->addAttribute('city', 'abc');
echo $objXML->asXML();
<?xml version="1.0"?>
<root>
<section name="Section1">
<category id="Category1" name="google">
</section>
</root>
EOT;
$objXML=simplexml_load_string($xml);
$objXML->section->addAttribute('city', 'abc');
echo $objXML->asXML();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询