php dom xml在某节点下怎么新建节点

balabalala 发布于 2012/07/18 09:57
阅读 388
收藏 0
PHP

<?xml version="1.0" encoding="UTF-8"?>
<username update="2012-07-17" copyright="awangba.com">
   <uid>123</uid>
   <progress>Lisexe</progress>
   <business>
      <pop name="广告">
         <bid>3</bid>
         <ips/>
         <url/>
         <status>0</status>
      </pop>
 </business>
</username>

xml  是这样格式的 , 我想再 ips 的节点加内容改怎么写,用dom

加完后是

<ips>
  <i>123</i>
  <i>456</i>
  <i>789</i>
</ips>

加载中
0
皮总
皮总
<?php

$xmlstring = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<username update="2012-07-17" copyright="awangba.com">
   <uid>123</uid>
   <progress>Lisexe</progress>
   <business>
      <pop name="广告">
         <bid>3</bid>
         <ips/>
         <url/>
         <status>0</status>
      </pop>
 </business>
</username>
XML;

$xml = simplexml_load_string($xmlstring);

$xml ->business->pop->ips->addChild("i",123);
$xml ->business->pop->ips->addChild("i",456);
$xml ->business->pop->ips->addChild("i",789);

echo $xml->asXML();

0
balabalala
balabalala

引用来自“皮总”的答案

<?php

$xmlstring = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<username update="2012-07-17" copyright="awangba.com">
   <uid>123</uid>
   <progress>Lisexe</progress>
   <business>
      <pop name="广告">
         <bid>3</bid>
         <ips/>
         <url/>
         <status>0</status>
      </pop>
 </business>
</username>
XML;

$xml = simplexml_load_string($xmlstring);

$xml ->business->pop->ips->addChild("i",123);
$xml ->business->pop->ips->addChild("i",456);
$xml ->business->pop->ips->addChild("i",789);

echo $xml->asXML();

非常感谢..
0
balabalala
balabalala
还有一个问题 如果我想删除 所有 i的节点怎么办呢
0
皮总
皮总
<?php

$xmlstring = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<username update="2012-07-17" copyright="awangba.com">
   <uid>123</uid>
   <progress>Lisexe</progress>
   <business>
      <pop name="广告">
         <bid>3</bid>
		 <ips>
		  <i>123</i>
		  <i>456</i>
		  <i>789</i>
		 </ips>
         <url/>
         <status>0</status>
      </pop>
 </business>
</username>
XML;

$xml = simplexml_load_string($xmlstring);

foreach ($xml->business->pop->ips as $i)
{
	$dom = dom_import_simplexml($i);
	$dom->parentNode->removeChild($dom);
}

echo $xml->asXML();


赚你一点分数真难

balabalala
balabalala
O(∩_∩)O~ 难道这个可以给分的么,我怎么不知道
返回顶部
顶部