首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除XML中的节点

删除XML中的节点
EN

Stack Overflow用户
提问于 2011-09-06 03:53:02
回答 1查看 224关注 0票数 0

我是操作xml的新手,希望能从大家那里得到一些帮助。

我的后端系统输出了一个具有以下节点结构的XML文件。

代码语言:javascript
复制
<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>

现在需要更改的部分是order Node。需要保存的数据是第一个和最后一个支付节点。任何其他落在其中的节点都可以被移除或删除。有没有办法用E4x做到这一点?

谢谢你的帮助。贝尔托

EN

回答 1

Stack Overflow用户

发布于 2011-09-06 05:39:34

不确定是否使用E4X,但使用RhinoEnvjsjQuery

启动Rhino:

代码语言:javascript
复制
java -jar js.jar -opt -1

现在您应该在Rhino提示符下。

加载一些库(我不建议从internet加载,但出于示例的目的),读取orders文件,解析为xml,删除付款,然后打印结果……

代码语言:javascript
复制
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“定义为:

代码语言:javascript
复制
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();
        }
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7312211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档