-1

我有一个包含 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>

帮助感恩。

4

1 回答 1

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

谢谢

于 2017-10-13T03:00:03.640 回答