2012-06-13 82 views
3

這聽起來很直截了當,但我仍然想在論壇中發佈這個問題。我有一個xml文件,它需要在main元素之後追加數據,並保存xml文件而不覆蓋現有的xml文件,而是將數據追加到已存在的數據並更新xml文件。更新/追加數據到XML文件使用php

例如我的XML數據類似於這樣的東西:

<maincontent> 
    <headercontent> 
     <product num="2102"> 
      <name>MSG</name> 
      <category>Wellness</category> 
      <available content="YES"></available> 
     </product> 
     <product num="2101"> 
      <name>YSD</name> 
      <category>Music</category> 
      <available content="NO"></available> 
     </product> 
     <product num="2100"> 
      <name>RCS</name> 
      <category>Media</category> 
      <available content="YES"></available> 
     </product> 
    </headercontent> 
</maincontent> 

我想在頂部加入其他產品的所有信息,並追加新添加的數據,使得新添加的數據應該跟從headercontent。要添加

數據:

 <product num="2103"> 
      <name>AGB</name> 
      <category>Movies</category> 
      <available content="YES"></available> 
     </product> 

更新的XML文件應該這樣看,如下圖所示:

<maincontent> 
    <headercontent> 
     <product num="2103"> 
       <name>AGB</name> 
       <category>Movies</category> 
       <available content="YES"></available> 
    </product> 
     <product num="2102"> 
      <name>MSG</name> 
      <category>Wellness</category> 
      <available content="YES"></available> 
     </product> 
     <product num="2101"> 
      <name>YSD</name> 
      <category>Music</category> 
      <available content="NO"></available> 
     </product> 
     <product num="2100"> 
      <name>RCS</name> 
      <category>Media</category> 
      <available content="YES"></available> 
     </product> 
    </headercontent> 
</maincontent> 

任何有用的建議或一段示例代碼將是非常有益的。

編輯:

對不起你們我還沒有發佈任何PHP代碼,我的錯。這是我一直在努力代碼:

感謝

<?php 

$xmldoc = new DomDocument();  
    $xmldoc->formatOutput = true; 

    $productNum = "2103"; 
    $name = "AGB"; 
    $category = "Movies"; 
    $content = "YES"; 

    if($xml = file_get_contents('main.xml')){ 
     $xmldoc->loadXML($xml); 

     $root = $xmldoc->firstChild;   

     $newElement = $xmldoc->createElement('product'); 
     $root->appendChild($newElement); 
     $numAttribute = $xmldoc->createAttribute("num"); 
     $numAttribute->value = $productNum; 
     $newElement->appendChild($numAttribute); 

     $nameElement = $xmldoc->createElement('name'); 
     $root->appendChild($nameElement); 
     $nameText = $xmldoc->createTextNode($name); 
     $nameElement->appendChild($nameText); 

     $categoryElement = $xmldoc->createElement('category'); 
     $root->appendChild($categoryElement); 
     $categoryText = $xmldoc->createTextNode($category); 
     $categoryElement->appendChild($categoryText); 

     $availableElement = $xmldoc->createElement('available'); 
     $root->appendChild($availableElement); 
     $availableAttribute = $xmldoc->createAttribute("content"); 
     $availableAttribute->value = $content; 
     $availableElement->appendChild($availableAttribute); 


     $xmldoc->save('main.xml'); 
    } 
?> 

我的XML文件被更新,但該數據被添加到則firstChild和太在底部,而不是我想以後添加數據並在開始如上所示。 這裏是我的輸出:

<maincontent> 
    <headercontent> 
     <product num="2102"> 
      <name>MSG</name> 
      <category>Wellness</category> 
      <available content="YES"/> 
     </product> 
     <product num="2101"> 
      <name>YSD</name> 
      <category>Music</category> 
      <available content="NO"/> 
     </product> 
     <product num="2100"> 
      <name>RCS</name> 
      <category>Media</category> 
      <available content="YES"/> 
     </product> 
    </headercontent> 
<product num="2103"/><name>AGB</name><category>Movies</category><available content="YES"/></maincontent> 

有什麼建議嗎?

+0

你到目前爲止嘗試過什麼?如果你沒有在你身邊展示一些努力,這裏沒有人會幫助你。 – Nikola

+0

可能重複'http:// stackoverflow.com/questions/194574 /插入數據在xml文件與php-dom' – Sanjay

+0

我已更新帖子,任何答覆! – 125369

回答

6

這將工作。

<?php 
$xmldoc = new DomDocument('1.0'); 
$xmldoc->preserveWhiteSpace = false; 
$xmldoc->formatOutput = true; 

$productNum = "2103"; 
$name = "AGB"; 
$category = "Movies"; 
$content = "YES"; 

if($xml = file_get_contents('main.xml')) { 
    $xmldoc->loadXML($xml, LIBXML_NOBLANKS); 

    // find the headercontent tag 
    $root = $xmldoc->getElementsByTagName('headercontent')->item(0); 

    // create the <product> tag 
    $product = $xmldoc->createElement('product'); 
    $numAttribute = $xmldoc->createAttribute("num"); 
    $numAttribute->value = $productNum; 
    $product->appendChild($numAttribute); 

    // add the product tag before the first element in the <headercontent> tag 
    $root->insertBefore($product, $root->firstChild); 

    // create other elements and add it to the <product> tag. 
    $nameElement = $xmldoc->createElement('name'); 
    $product->appendChild($nameElement); 
    $nameText = $xmldoc->createTextNode($name); 
    $nameElement->appendChild($nameText); 

    $categoryElement = $xmldoc->createElement('category'); 
    $product->appendChild($categoryElement); 
    $categoryText = $xmldoc->createTextNode($category); 
    $categoryElement->appendChild($categoryText); 

    $availableElement = $xmldoc->createElement('available'); 
    $product->appendChild($availableElement); 
    $availableAttribute = $xmldoc->createAttribute("content"); 
    $availableAttribute->value = $content; 
    $availableElement->appendChild($availableAttribute); 

    $xmldoc->save('main.xml'); 
} 
?> 

希望這會有所幫助。

+0

嗨Pushpesh,非常感謝你的一段代碼,它已經像魅力一樣工作了! – 125369

+0

樂意幫忙。你已經在努力工作了,我所要做的只是在兩處修改它;) –