-1

I have a string variable which contains XML data, I justed wanted to add a child attribute inside another element of that XML. A solution I have in my mind is to convert this string to XML then by XML.appendChild() method it can be done, but I am not sure as I haven't tried yet.

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>'

As you can see the above string, i just wanted to insert <coupon-id> somevalue </coupon-id> attribute inside <price-adjustment> element using JAVASCRIPT. The output will be like:

<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>

Help with be thankful.

1 Answers1

0

did you check XML class documentation -> 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

They have example how to add something in the top of the class description.

Also best practice to deal with XML is deal with xml files. You would have to use XMLStreamReader first to read the xml. And after write a new one with XMLStreamWriter adding information that you need. There is no ability to append something in xml file.

For more description please check documentation of dw.io package -> https://documentation.demandware.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/package_dw_io.html

Thanks

Oleg Sapishchuk
  • 667
  • 6
  • 18