2012-04-27 12 views
0

私はsimplexmlを使用して、phpでXML形式のAPI URLをロードしています。 ProductDescription要素には、保証セクションに追加したいhtmlが含まれています。 私はLAST </ul>タグの前にProductDescription要素で<li>Valid in US</li>を追加したい:PHPでSimpleXmlを使用して要素にデータを追加して保存する

<xmldata> 
     <Products> 
     <ProductCode>ACODE</ProductCode> 
     <ProductID>1234</ProductID> 
     <ProductName>PRODUCTTITLE</ProductName> 
     <ProductDescription> 
      <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong> 
      <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr /><strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /><strong>Warranty Information for a certain product</strong> 
      <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div> 
     </ProductDescription> 
     <ProductManufacturer>MFG</ProductManufacturer> 
     </Products> 
    </xmldata> 

が今私にできることすべてはProductDescriptionの外にむき出しのコードを取得している、私はそれを追加する方法を知っておく必要がありますlistタグを付けて投稿または表示する前に、最後のulタグの末尾に移動します。私はそれが苦痛かもしれないとして、(誰かが道を知っている場合を除き)正規表現を使用するように持っていけない期待しています

 foreach($xml as $NEW_XML) 
     {   
      $code = $NEW_XML->ProductCode; 
      $name = $NEW_XML->ProductName; 
      $desc = $NEW_XML->ProductDescription; 
      $mfg = $NEW_XML->ProductManufacturer; 
echo "<textarea rows='13' cols='64' name='DESC' />" . $desc. "</textarea>"; } 

:ここに私の部分のPHPです。私の最初の考えは<textarea>に入れてxmlに戻すだけですが、.xmlとして直接保存する方法があれば、より効果的です。

答えて

1

ul秒の数がわかっている場合、あなたはこのような情報を追加することができますが:

$NEW_XML->ProductDescription->ul[2]->addChild('li', 'Valid in US'); 

これは$NEW_XMLProducts -elementで指していることを前提としています。

$desc = $NEW_XML->ProductDescription; 
$count = count($desc->ul); 
$desc->ul[$count - 1]->addChild('li', 'Valid in US'); 

を次にあなたが$xml->asXML()を単に出力または保存することができます:に常には、あなたが最初にそれらをカウントする必要があり、最後のulを取得します。

更新:ここ

が私の最後のコメントから、私の仮定に基づいて更新され、私が使用した完全なコードです、多分これは役立ちます:

<?php 
$xmldata = <<<ENDXML 
<xmldata> 
     <Products> 
     <Product> 
      <ProductCode>ACODE</ProductCode> 
      <ProductID>1234</ProductID> 
      <ProductName>PRODUCTTITLE</ProductName> 
      <ProductDescription> 
       <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong> 
       <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr /> <strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /><strong>Warranty Information for a certain product</strong> 
       <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div> 
      </ProductDescription> 
      <ProductManufacturer>MFG</ProductManufacturer> 
     </Product> 
     <Product> 
      <ProductCode>ANOTHERCODE</ProductCode> 
      <ProductID>98765</ProductID> 
      <ProductName>PRODUCTTITLE 2</ProductName> 
      <ProductDescription> 
       <h1>Stuff</h1><p>description</p><hr /><strong>Features &amp; Benefits</strong> 
       <ul><li>feat</li><li>feat</li><li>feat</li></ul><hr /> <strong>Specifications</strong><ul><li>spec</li><li>spec</li><li>spec</li></ul> <hr /> <strong>Warranty Information for a certain product</strong> 
       <ul><li>3 Years Parts</li><li>3 Years Labor</li></ul><div> <a href="/ahref" target="_blank">See more products from MFG </a></div> 
      </ProductDescription> 
      <ProductManufacturer>MFG</ProductManufacturer> 
     </Product> 
    </Products> 
</xmldata> 
ENDXML; 

$xml = simplexml_load_string($xmldata); 

foreach ($xml->Products->Product as $product) { 
    $description = $product->ProductDescription; 
    $count = count($description->ul); 
    $description->ul[$count-1]->addChild('li', 'Valid in US'); 
} 

echo $xml->asXML(); 
+0

そのが働いていません。 '$ xml = simplexml_load_file($ feedURL)'を使っているからですか? – ToddN

+0

奇妙な、私はそれをテストしました。ループ出力に 'echo $ NEW_XML-> asXML();'とは何か? – cschorn

+0

それは 'エコー'絶対に何も、その空白。私は 'ProductDescription'のすべてが' Child'要素ではなくテキストのように読み込まれるため、そのことを信じています。 – ToddN

関連する問題