向现有XML添加节点


Adding Nodes to Existing XML

我遇到的问题是每次写入XML时都会生成根XML。主要的问题是设置子节点和定义根节点。从Łza的帮助我现在明白了根XML节点被忽略了。然后你设置并创建一个Child然后添加你的内容,正确格式的例子是。

$xml = simplexml_load_file('FILENAME.xml');  // Load XML File Need to add IF Statment to create if does not exist
$result = $xml->addchild('Result'); // Ignore Root NODE and Add Child Results
$result->addChild('Time', gmdate('D-M-Y -H:i:s')); // Rest of the below adds Child to Result and outputs results
$result->addChild('Channel', $Site);
$result->addChild('Type', '**');
$result->addChild('Process', $Status);
$result->addChild('SKU', $code->SKU);
$result->addChild('item', $item);
$result->addChild('Status', '$Feedback');
$result->addChild('ErrorID', '$Error');
$result->addChild('Message', '$Message');
$xml->asXml('FILENAME.xml');  //Write to file would be
// All of the above Code is using variables from another part of the script

输出将是

<Root>
    <Result>
        <Time>Fri-May-2013 -09:15:22</Time>
        <Channel>20</Channel>
        <Type>**</Type>
        <Process>Update</Process>
        <SKU>98746524765</SKU>
        <Item/>
        <Status>Problem</Status>
        <ErrorID>999-Error</ErrorID>
        <Message>Unknown file format support</Message>
    </Result>
    <Result>
        <Time>Fri-May-2013 -09:15:22</Time>
        <Channel>20</Channel>
        <Type>**</Type>
        <Process>Update</Process>
        <SKU>5412254785</SKU>
        <Item/>
        <Status>Problem</Status>
        <ErrorID>123-Error</ErrorID>
        <Message>Invalid Item</Message>
    </Result>
</Root>

谢谢

尝试使用SimpleXMLElement库代替硬编码的xml创建。这在开始使用时可能更复杂,但更安全(我的意思是在硬编码xml时避免xml结构中可能出现的错误),并且在刚开始使用时易于使用。并且易于添加/删除节点,子节点。

这是你的代码的一个例子:

$xml = new SimpleXMLElement('<xml/>');
$data = $xml->addChild('data');
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);
// ... and the same way create all your xml nodes.
// if you want add next <result> node witch all elements repeat the code, (or put it in loop if you want more <result> elements):
$result = $data->addChild('Result');
$result->addChild('Time', gmdate('D-M-Y -H:i:s'));
$result->addChild('Channel', $SiteID);
// and after create all nodes save the file:
$xml->asXml('DHError.xml'); 
上面的代码将创建xml:
 <xml>
  <data>
   <Result>
     <Time>Fri-May-2013 -12:14:39</Time> 
     <Channel>data</Channel> 
   </Result>
   <Result>
     <Time>Fri-May-2013 -12:14:39</Time> 
     <Channel>data</Channel> 
   </Result>
  </data>
 </xml>

这它。然后,如果您需要加载和处理xml,这将是很容易的:

加载文件只需使用:

$xml2 = simplexml_load_file('DHError.xml');
// to add new node <Result>:
$resultNext = $xml2->data->addchild('Result');
$resultNext->addChild('Time', gmdate('D-M-Y -H:i:s'));
$resultNext->addChild('Channel', $SiteID);
//and save file
$xml2->asXml('DHError.xml');

创建一个xml:

<?xml version="1.0" ?> 
 <xml>
  <data>
   <Result>
     <Time>Fri-May-2013 -12:27:24</Time> 
     <Channel>data</Channel> 
   </Result>
   <Result>
     <Time>Fri-May-2013 -12:27:24</Time> 
     <Channel>data</Channel> 
  </Result>
  <Result>
    <Time>Fri-May-2013 -12:27:24</Time> 
    <Channel>data</Channel> 
  </Result>
 </data>
</xml>