PHP生成时,子元素的创建

zhangxinzx11 发布于 2011/10/17 10:13
阅读 183
收藏 0
PHP

<?

$dom = new DOMDocument("1.0","utf-8");
header("Content-Type: text/plain");
$root = $dom->createElement("ProgramExportRequest");
$dom->appendChild($root);
 
$SourceModuleID = $dom->createElement("SourceModuleID");
$root->appendChild($SourceModuleID);
$text = $dom->createTextNode("60000000-0000-0000-0000-000000000007");
$SourceModuleID->appendChild($text);

$AssociationID = $dom->createElement("AssociationID");
$root->appendChild($AssociationID);
$text = $dom->createTextNode("32344");
$AssociationID->appendChild($text);

$Cmd = $dom->createElement("Cmd");
$root->appendChild($Cmd);
$text = $dom->createTextNode("GetProgram");
$Cmd->appendChild($text);

$UserToken = $dom->createElement("UserToken");
$root->appendChild($UserToken);
$text = $dom->createTextNode("8000000f0-0000-0000-0000-0000000000007");
$UserToken->appendChild($text);

$LastSyncTime = $dom->createElement("LastSyncTime");
$root->appendChild($LastSyncTime);
$text = $dom->createTextNode($datetime);
$LastSyncTime->appendChild($text);
echo $dom->saveXML();
?>

上面PHP代码生成了:

  <?xml version="1.0" encoding="utf-8" ?>
- < ProgramExportRequest >
       < SourceModuleID > 60000000-0000-0000-0000-000000000007 </ SourceModuleID >
       < AssociationID > 32344 </ AssociationID >
     < Cmd > GetProgram </ Cmd >
        < UserToken > 8000000f0-0000-0000-0000-0000000000007 </ UserToken >
       < LastSyncTime > 2011-10-17T10:11:51 </ LastSyncTime >
</ ProgramExportRequest >
 
我现在需要的是
<?xml version="1.0" encoding="utf-8" ?>
<ProgramExportRequest>
  <CmdRequest>
    <SourceModuleID>60000000-0000-0000-0000-000000000007</SourceModuleID>
    <AssociationID>32344</AssociationID>
    <Cmd>GetProgram</Cmd>
    <UserToken>8000000f0-0000-0000-0000-0000000000007</UserToken>
    <LastSyncTime></LastSyncTime>
  </CmdRequest>
</ProgramExportRequest>
就是在<ProgramExportRequest>根元素下还有个<CmdRequest>子元素。那PHP代码里应该怎么添加子元素呢?
加载中
0
开源中国董事会主席
开源中国董事会主席
用foreach或for 循环createElement和appendChild
0
宋一哈
宋一哈
楼上正解
0
xyz555
xyz555
$SourceModuleID = $dom->createElement("SourceModuleID");         // 生成 SourceModuleID 节点

$root->appendChild($SourceModuleID);         // 在root节点下添加SourceModuleID节点

现在明白怎么写了?

0
傅小黑
傅小黑
如果php > 5.1 了。。还是建议xmlWriter。。。更快
返回顶部
顶部