我有一个包含XML数据的字符串变量,我希望在该XML的另一个元素中添加一个子属性。我在脑海中的一个解决方案是将此字符串转换为XML,然后通过XML.appendChild()
方法将其转换为完成,但我不确定,因为我还没有尝试过。
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在<coupon-id> somevalue </coupon-id>
元素中插入<price-adjustment>
属性。输出将如下:
<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 :(得分:0)
他们举例说明了如何在类描述的顶部添加一些内容。
处理XML的最佳做法是处理xml文件。您必须首先使用XMLStreamReader来读取xml。在用XMLStreamWriter写一个新的后,添加你需要的信息。无法在xml文件中追加内容。
有关更多说明,请查看dw.io包的文档 - &gt; https://documentation.demandware.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/package_dw_io.html
谢谢