2017-12-11 9 views
1

私はこれに困惑だし、面白い部分は、私は正常にこのコード何度も使用してきましたさ... SimpleXMLはできませんXMLからの既存のノードに文字列を結合します。たぶん私は」は、PHPを経由して、XMLにノードを追加し、(newsku)</p> <p>私の目的は、新しいノードを作成することです

...何が起こることは、私は新しいノードのない元のXMLを取得することである

<?php 
    header('Content-Type: text/html; charset=UTF-8'); 
    error_reporting(E_ALL); 

    // Import test xml 

    $products = simplexml_load_file("http://test.com/xml/customer.xml"); 

    foreach($products->xpath("product") as $p) { 
     $p->addChild("newsku", "NEW" . $p->sku); 
    } 

    $products->asXML('test.xml'); 
    echo 'test XML files are updated'; 
?> 

:ここ

は、XMLです:

<products> 
    <product> 
     <id>3</id> 
     <name><![CDATA[ΜΙΚΡΟΦΩΝΟ SAMSON G-TRACK]]></name> 
     <manufacturer><![CDATA[SAMSON]]></manufacturer> 
     <sku><![CDATA[550.SAM.060]]></sku> 
     <description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered), built-in audio interface and mixer, 
allows simultaneous input of vocals and guitar, bass, or keyboard while also 
providing monitoring through an on-board headphone output. Specifications: mic 
and instrument/line gain control with clip LED, stereo input jacks for (3.5mm 
stereo-jack) instrument or line level signal, stereo headphone jack for zero 
latency monitoring with level control, 3-position headphone switch for stereo, 
mono and computer monitoring. USB bus-powered. Includes desktop microphone 
stand, audio I/O cables, USB cables and Cakewalk Sonar LE software. Optional 
shockmount available. 
]]></description_greek> 
     <short_description_greek><![CDATA[Samson G-Track - large diaphragm USB studio 
condenser microphone (USB bus-powered)]]></short_description_greek> 
     <price>155.00</price> 
     <msrp>185.00</msrp> 
     <instock>no</instock> 
     <images total="2"> 
      <image_1>http://test.com/media/catalog/product/5/5/550.sam.060-mi- 
01.jpg</image_1> 
      <image_2>http://test.com/media/catalog/product/5/5/550.sam.060-mi- 
02.jpg</image_2> 
     </images> 
    </product> 
</products> 

そして、ここに私のコードです私はこれを他の多くのXMLファイルでも問題なく使用して以来、本当にばかげたことをしています...

アイデア?

ありがとうございます!

+0

を - https://3v4l.org/5JnEv –

+0

こんにちはローレンスは、なぜこのため、他のXMLファイルとではないため、この作業を行います1? –

+0

'http:// test.com/xml/customer.xml'からのフェッチと' test.xml':/への保存が繰り返されるだけです。 –

答えて

0

コードは正常に動作しますが、ではありません。は、ローカルソースコードtest.xmlのみを更新します。これは、PHPコードの各呼び出しで同じ値で上書きされます。

したがって、次のコードはリモートソースからフェッチし、新しい要素newskuを追加します。 ブラウザに出力します。

$products = simplexml_load_file('http://test.com/xml/customer.xml'); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 
// just outputs the xml - does not save 
exit($products->asXML()); 

あなたはその後、何度も書く最初のファイルを保存するために、XMLをキャッシュする必要がある場合。これは、それぞれのリフレッシュ時に新しい項目を追加します(あなたが望むものではありません)。

// the xml file name 
$xml_file = 'test.xml'; 

// check if the file exists or download and save it 
if (!file_exists($xml_file)) { 
    file_put_contents($xml_file, file_get_contents('http://test.com/xml/customer.xml')); 
} 

// load xml file for changes 
$products = simplexml_load_file($xml_file); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 

// save the xml to file 
$products->asXML($xml_file); 

// read and display the xml file 
readfile($xml_file); 

/xml/customer.xmlいる場合は、サーバー上で実際にローカルである、と私はリモートと困惑しています。上記のコードをfgc/fpcのものを使用せずに使用して、好きなようにすることができます(ファイルの呼び出しごとに更新されます)。

// load xml file for changes 
$products = simplexml_load_file('/path/to/xml/customer.xml'); 

foreach($products->xpath("*/product") as $p) { 
    $p->addChild("newsku", "NEW".$p->sku); 
} 

header('Content-Type: text/xml; charset=UTF-8'); 

// save the xml to file 
$products->asXML($xml_file); 
readfile($xml_file); 

// or just outputs the xml - no saving 
// exit($products->asXML()); 

希望します。

オンラインそれを参照してください - 期待どおりに動作しますが、これは、リモートサイトのバージョンを更新する習慣https://3v4l.org/U8EbE

+0

こんにちはローレンスとあなたの時間に感謝!もちろん、ローカルで編集されたリモートxmlのバージョンを保存することに興味があります。それでも新しいアイテムは追加されません。実際のリモートファイルURLをテストして、あなたのために動作するかどうかを確認します:https://www.rihardos.gr/index.php?option=com_vmxmlexporter&task=feed.export&id=7&type=xml&key=rihardos –

+0

''ラッパーの代わりに '$ products-> xpath(" */product ")'を使用してください。 –

+0

一度その作業をupvoteに忘れる; p –

関連する問題