2017-10-11 96 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>' 

正如你可以看到上面的字符串,我只是想插入<coupon-id> somevalue </coupon-id>屬性<price-adjustment>元素中使用JavaScript。輸出將如下所示:

<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

感謝

相關問題