2012-02-16 10 views
4

まず、xml文書内の特定の子ノード値で親ノードを検索する必要があります。次に、特定の子ノードを親ノードから別のXMLドキュメントにコピーします。例えばPHPある文書から別の文書にxmlノードをコピー

DESTINATION FILE: ('destination.xml') 
<item> 
    <offerStartDate>2012-15-02</offerStartDate> 
    <offerEndDate>2012-19-02</offerEndDate> 
    <title>Item Title</title> 
    <rrp>14.99</rrp> 
    <offerPrice>9.99</offerPrice> 
</item> 

SOURCE FILE: ('source.xml') 
<items> 
    <item> 
     <title>Item A</title> 
     <description>This is the description for Item A</description> 
     <id>1003</id> 
     <price> 
      <rrp>10.00</rrp> 
      <offerPrice>4.99</offerPrice> 
     </price> 
     <offer> 
      <deal> 
       <isLive>0</isLive> 
      </deal> 
     </offer> 
    </item> 
    <item> 
     <title>Item B</title> 
     <description>This is the description for Item B</description> 
     <id>1003</id> 
     <price> 
      <rrp>14.99</rrp> 
      <offerPrice>9.99</offerPrice> 
     </price> 
     <offer> 
      <deal> 
       <isLive>1</isLive> 
      </deal> 
     </offer> 
    </item> 
    <item> 
     <title>Item C</title> 
     <description>This is the description for Item C</description> 
     <id>1003</id> 
     <price> 
      <rrp>9.99</rrp> 
      <offerPrice>5.99</offerPrice> 
     </price> 
     <offer> 
      <deal> 
       <isLive>0</isLive> 
      </deal> 
     </offer> 
    </item> 

私はそれが "1" に設定された子ノード<isLive>値のしている親ノード<item>を見つけたいです。親ノードの他の子ノードを宛先xmlにコピーします。

親ノード<item>の子ノード<isLive>が1に設定されている場合は、<title>,<rrp>および<offerPrice>のノードとその値をコピーし、上記のように子ノードとして宛先ファイルに追加します。

正しく使用していない場合は、技術用語を教えてください。

多くのお手伝いをいただきありがとうございます。 SimpleXMLは(demo)で

+0

私はあなたのdestination.xmlがあまりにも、項目のルートノードを持っていると仮定? – Gordon

+0

はい、それは ''です。今すぐそれに取り組んでください – echez

答えて

6

:DOM(demo)で

$dItems = simplexml_load_file('destination.xml'); 
$sItems = simplexml_load_file('source.xml'); 
foreach ($sItems->xpath('/items/item[offer/deal/isLive=1]') as $item) { 
    $newItem = $dItems->addChild('item'); 
    $newItem->addChild('title', $item->title); 
    $newItem->addChild('rrp', $item->price->rrp); 
    $newItem->addChild('offerprice', $item->price->offerPrice); 
} 
echo $dItems->saveXML(); 

$destination = new DOMDocument; 
$destination->preserveWhiteSpace = false; 
$destination->load('destination.xml'); 
$source = new DOMDocument; 
$source->load('source.xml'); 
$xp = new DOMXPath($source); 
foreach ($xp->query('/items/item[offer/deal/isLive=1]') as $item) 
{ 
    $newItem = $destination->documentElement->appendChild(
     $destination->createElement('item') 
    ); 
    foreach (array('title', 'rrp', 'offerPrice') as $elementName) { 
     $newItem->appendChild(
      $destination->importNode(
       $item->getElementsByTagName($elementName)->item(0), 
       true 
      ) 
     ); 
    } 
} 
$destination->formatOutput = true; 
echo $destination->saveXml(); 
+0

彼らは両方ともphpファイルに新しいデータを表示しますが、どちらも宛先xmlに保存しません。任意のアイデアpls? – echez

+0

@echez私はPHPのマニュアルのそれぞれのドキュメントから把握するためにあなたに最後のビットを残しています;) – Gordon

+0

フェア十分です。どうもありがとう。あなたの答えがあなたの問題を解決すれば、私は開発の投稿を – echez

関連する問題