2010-05-14 55 views
0

我有這個在我的XML文件:合併條目XMLFILE(使用SimpleXML PHP)

<product name="iphone"> 
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg"> 
     <description><![CDATA[iphone]]></description> 
     <short_description><![CDATA[]]></short_description> 
     <deliverytime><![CDATA[]]></deliverytime> 
     <options> 
      <option group="Color" option="Black" /> 
     </options> 
    </variant> 
</product> 
<product name="iphone"> 
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg"> 
     <description><![CDATA[iphone]]></description> 
     <short_description><![CDATA[]]></short_description> 
     <deliverytime><![CDATA[]]></deliverytime> 
     <options> 
      <option group="Color" option="White" /> 
     </options> 
    </variant> 
</product> 

我想將它合併到這個(請注意,我合併選項標籤):

<product name="iphone"> 
    <variant name="iphone" product_number="12345" price="500" picture="iphone.jpg"> 
     <description><![CDATA[iphone]]></description> 
     <short_description><![CDATA[]]></short_description> 
     <deliverytime><![CDATA[]]></deliverytime> 
     <options> 
      <option group="Color" option="Black" /> 
      <option group="Color" option="White" /> 
     </options> 
    </variant> 
</product> 

最好我想在記憶中做所有事情,因爲之後我會進一步處理它。

+0

使用「foreach($ product-> variant-> options AS $ variant)」遍歷XML文件「並獲取」名稱「。然後再次遍歷XML文件並比較這些值。問題是有超過1000種產品。這意味着我必須循環至少1000次以上。沒有更好的方法嗎? – Cudos 2010-05-14 14:06:10

回答

0

這是不容易的,我正在尋找同樣的...一個問題是鑑定哪個iphone的意思等等..存在合併功能,但在我的情況下,他們並沒有很好地工作:http://www.php.net/manual/de/ref.simplexml.php#92272當你發現沒有解決辦法但我覺得是時候做我自己,並報告你:

更新1:功能xml_attributeaddxmlelement是從正常的PHP頁面(我複製到結束)。我寫的函數對於一種情況非常獨立,並不適用於所有合併情況。我爲我創作了一部小說。

XML:NEU(新增加)

<varrDaten> 
     <person> 
     <street id="adder">adder</street> 
     <loc>land AAA</loc> 
    </person> 
    <person> 
     <street id="exister">exister street</street> 
     <loc>land gg</loc> 
    </person> 
    <person> 
     <street id="updater">street is uptodate</street> 
     <loc>land is updated</loc> 
    </person> 
</varrDaten> 

XML(存在)

<varrDaten> 
    <person> 
     <street id="minuser">minuser</street> 
     <loc>land 0</loc> 
    </person> 
    <person> 
     <street id="exister">exister street</street> 
     <loc>land lon</loc> 
    </person> 
    <person> 
     <street id="updater">update need street</street> 
     <loc>land need update</loc> 
    </person> 
</varrDaten> 

function simplexml_merge2($xpOld,$xpNeu) 
      { 
      $selIDS=$xpNeu->xpath('//street[not(@id="0")]'); 
      foreach($selIDS as $ident) 
       { 
       //existiert in vorhandennen? 
       $xpSelV=$xpOld->xpath('//person/street[@id="'.xml_attribute($ident,'id').'"]'); 
       if(count($xpSelV)>0) 
        { 
        echo "YES EXISTS".(string)$xpSelV[0][0]."<>".(string)$ident[0]."
"; //test of value maybe.. if(!((string)$xpSelV[0][0]==(string)$ident[0])) { //HERE YOU CAN ADD CRITERIA WHICH SHOULD BE SYCNRONISED IF ALREADY EXISTS echo "VAL not the same"; $xp2Ef=$xpOld->xpath('//person[./street[@id="'.xml_attribute($ident,'id').'"]]'); $xp2Ef[0][0]=(string)$ident[0]; } } else { echo "NOOO:".$xpNeu[0]; $xpEF=$xpOld->xpath('//varrDaten'); echo '//person[./street[@id="'.xml_attribute($ident,'id').'"]]'; $xp2Ef=$xpNeu->xpath('//person[./street[@id="'.xml_attribute($ident,'id').'"]]'); AddXMLElement($xpEF[0],$xp2Ef[0]); } } }
添加。 func
function xml_attribute($object, $attribute) 
{ 
    if(isset($object[$attribute])) 
     return (string) $object[$attribute]; 
    else 
     return false; 
} 
function AddXMLElement(SimpleXMLElement $dest, SimpleXMLElement $source) 
    { 
     $new_dest = $dest->addChild($source->getName(), $source[0]);

foreach ($source->attributes() as $name => $value) 
    { 
     $new_dest->addAttribute($name, $value); 
    } 

    foreach ($source->children() as $child) 
    { 
     AddXMLElement($new_dest, $child); 
    } 
}