2017-10-11 13 views
-1

XMLデータを含む文字列変数があります。そのXMLの別の要素内に子属性を追加したがりました。私が考えている解決策は、この文字列をXMLに変換してからXML.appendChild()メソッドで解決することですが、まだ試していないのでわかりません。別のノード内にノードを挿入するXML

var pli = ''; 
pli = '<product-lineitem> 
      <net-price>70.00</net-price> 
      <tax>4.66</tax> 
      <gross-price>74.66</gross-price> 
      <base-price>70.00</base-price> 
      <lineitem-text>THE GOLD GODS Micro Jesus Piece Gunmetal Necklace</lineitem-text> 
      <tax-basis>52.50</tax-basis> 
      <position>1</position> 
      <product-id>26809214001</product-id> 
      <product-name>THE GOLD GODS Micro Jesus Piece Gunmetal Necklace</product-name> 
      <quantity unit="">1.0</quantity> 
      <tax-rate>0.08875</tax-rate> 
      <shipment-id>00017006</shipment-id> 
      <gift>false</gift> 
      <custom-attributes> 
       <custom-attribute attribute-id="defaultItemShipment">add-to-cart</custom-attribute> 
      </custom-attributes> 
      <price-adjustments> 
       <price-adjustment> 
        <net-price>-17.50</net-price> 
        <tax>0.00</tax> 
        <gross-price>-17.50</gross-price> 
        <base-price>-17.50</base-price> 
        <lineitem-text>25% off Dresses</lineitem-text> 
        <tax-basis>0.00</tax-basis> 
        <promotion-id>25-off-dresses-test</promotion-id> 
        <campaign-id>25-off-dresses-test</campaign-id> 
       </price-adjustment> 
      </price-adjustments> 
     </product-lineitem>' 

あなたは上記の文字列を見ることができるように、私はちょうどJavaScriptを使用して<price-adjustment>要素内<coupon-id> somevalue </coupon-id>属性を挿入したかったです。出力は次のようになります。

<product-lineitem> 
     <net-price>70.00</net-price> 
     <tax>4.66</tax> 
     <gross-price>74.66</gross-price> 
     <base-price>70.00</base-price> 
     <lineitem-text>THE GOLD GODS Micro Jesus Piece Gunmetal Necklace</lineitem-text> 
     <tax-basis>52.50</tax-basis> 
     <position>1</position> 
     <product-id>26809214001</product-id> 
     <product-name>THE GOLD GODS Micro Jesus Piece Gunmetal Necklace</product-name> 
     <quantity unit="">1.0</quantity> 
     <tax-rate>0.08875</tax-rate> 
     <shipment-id>00017006</shipment-id> 
     <gift>false</gift> 
     <custom-attributes> 
      <custom-attribute attribute-id="defaultItemShipment">add-to-cart</custom-attribute> 
     </custom-attributes> 
     <price-adjustments> 
      <price-adjustment> 
       <net-price>-17.50</net-price> 
       <tax>0.00</tax> 
       <gross-price>-17.50</gross-price> 
       <base-price>-17.50</base-price> 
       <lineitem-text>25% off Dresses</lineitem-text> 
       <tax-basis>0.00</tax-basis> 
       <promotion-id>25-off-dresses-test</promotion-id> 
       <campaign-id>25-off-dresses-test</campaign-id> 
       <coupon-id> somevalue </coupon-id> 
      </price-adjustment> 
     </price-adjustments> 
    </product-lineitem> 

ご協力いただきありがとうございます。

答えて

0

あなたはXMLクラスのドキュメントをチェックしなかった - >https://documentation.demandware.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/class_TopLevel_XML.html?resultof=%22%61%70%70%65%6e%64%43%68%69%6c%64%22%20%22%61%70%70%65%6e%64%63%68%69%6c%64%22%20

彼らは、クラス記述の上で何かを追加する方法の例を持っています。

また、XMLを処理するためのベストプラクティスはxmlファイルを処理します。最初にXMLStreamReaderを使用してxmlを読み取る必要があります。そして、必要な情報を追加するXMLStreamWriterで新しいものを書き込んでください。 xmlファイルに何かを追加する機能はありません。

より説明について

dw.ioパッケージのマニュアルを確認してください - >https://documentation.demandware.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/package_dw_io.html

を感謝

関連する問題