doupu1957 2013-12-24 14:26
浏览 40
已采纳

Php和XML为新手

I have this simple xml file :

<iPhone> 
  <anEvent>
    <data1>some Data</data1>
    <data2>Some data here </data2>
  </anEvent>

  <anEvent>
    <data1>some Data</data1>
    <data2>Some data here </data2>
  </anEvent>

</iPhone>

I need to get remove it from this file and write it to another file.

To get the <anEvent> i want, i do (i just want to transfer only one anEvent) :

$dom = new DomDocument(); 
$dom->load('myFile.xml');
$myData = $dom->getElementsByTagName('anEvent')->item(0); 

I get my at index 0 it's cool. In order to remove it I simply use :

$customRequete->parentNode->removeChild($myData);   
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->saveXML();
$dom->save("myFile.xml");

It works, that's cool. The problem is when I want to write the <anEvent> to another file. I know how to write to XML but not using DOM DOCUMENT :

$myNewFile = simplexml_load_file("myNewFile.xml");
$event = $myNewFile->addChild('anEvent'); 
$data1 = $event->addChild('data1','someData'); 
$data2 = $event->addChild('data2','some data here');

I was wondering if I could use $myData from my $dom in order to write something like :

$myNewFile -> addChild($myData); 
$myNewFile->save();

Or do I need to loop into $myData to get the node values?

  • 写回答

3条回答 默认 最新

  • download201401 2013-12-24 14:41
    关注

    If you want to move nodes from a document to another, you have to import them, using the importNode method on the new created DOMDocument. The latter parameter allows you to recursively import every node child.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b