删除XML中的节点

时间:2011-09-05 19:53:02

标签: xml e4x

我是操纵xml的新手,希望我能从你们这里得到一些帮助。

我的后端系统使用以下节点structer吐出XML文件。

<orders xmlns="http://www.foo.com/xml/impex/order/">
<order order-no="0000000000000303">
<order-date>2011-09-02T18:55:00.000Z</order-date>
<created-by>foo</created-by>
<original-order-no>0000000000000303</original-order-no>
<currency>USD</currency>
<customer-locale>default</customer-locale>
<affiliate-partner-name/>
<affiliate-partner-id/>
<invoice-no>00001422</invoice-no>
<customer>...</customer>
<customer-order-reference/>
<status>...</status>
<replace-code/>
<replace-description/>
<replacement-order-no/>
<replaced-order-no/>
<current-order-no>0000000000000303</current-order-no>
<cancel-code/>
<cancel-description/>
<product-lineitems>...</product-lineitems>
<giftcertificate-lineitems/>
<shipping-lineitems>...</shipping-lineitems>
<shipments>...</shipments>
<totals>...</totals>
<payments>
<payment>
<gift-certificate>
<custom-attributes>
<custom-attribute attribute-id="giftCard01Number">01000169466975</custom-attribute>
<custom-attribute attribute-id="giftCard01Value">10.00</custom-attribute>
<custom-attribute attribute-id="giftCard02Number">01100995910</custom-attribute>
<custom-attribute attribute-id="giftCard02Value">20.00</custom-attribute>
<custom-attribute attribute-id="giftCertificateType">card</custom-attribute>
</custom-attributes>
</gift-certificate>
<amount>10.00</amount>
<processor-id>BARNEYS_GIFT_CARD</processor-id>
<transaction-id>0000000000000303</transaction-id>
</payment>
<payment>
<gift-certificate>
<custom-attributes>
<custom-attribute attribute-id="giftCard02Number">01100995910</custom-attribute>
<custom-attribute attribute-id="giftCard02Value">20.00</custom-attribute>
<custom-attribute attribute-id="giftCertificateType">card</custom-attribute>
</custom-attributes>
</gift-certificate>
<processor-id>BARNEYS_GIFT_CARD</processor-id>
</payment>
<payment>
<credit-card>...</credit-card>
<amount>35.33</amount>
<processor-id>VCOMMERCE_CREDIT</processor-id>
<transaction-id>0000000000000303</transaction-id>
<custom-attributes>...</custom-attributes>
</payment>
</payments>
<remoteHost/>
<external-order-no/>
<external-order-status/>
<external-order-text/>
<custom-attributes>...</custom-attributes>
</order>
</orders>

现在需要更改的部分是订单节点。 需要保留的数据是第一个和最后一个支付节点。 可以删除或删除位于其中间的任何其他节点。 E4x有办法做到这一点吗?

感谢您的帮助。 叶贝

1 个答案:

答案 0 :(得分:0)

不确定E4X,但使用RhinoEnvjsjQuery

启动Rhino:

java -jar js.jar -opt -1

现在你应该进入Rhino提示。

加载一些库(我不建议从互联网上加载,但出于举例的目的),阅读订单文件,解析为xml,删除付款,然后打印出结果......

load("http://www.envjs.com/dist/env.rhino.1.2.js")
load("https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")
load("../rhino-scripts/removeFirstAndLastPayments.js")
xmlstr = readFile("../rhino-scripts/orders.xml")
xml = $.parseXML(xmlstr)
removeFirstAndLastPayments(xml)
new XMLSerializer().serializeToString(xml)

其中“removeFirstAndLastPayments”定义为:

function removeFirstAndLastPayments(root) {
    $(root).find("orders order").each(function (orderIdx, order) {
        var payments = $(order).find("payment");
        if (payments.length > 2) {
            // only remove first and last if there are more than 2 payments
            payments.first().remove();
            payments.last().remove();
        }
    });
}