删除 XML 中的节点

发布于 12-03 02:15 字数 2938 浏览 3 评论 0原文

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

我的后端系统使用以下节点结构生成一个 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 有办法做到这一点吗?

感谢您的帮助。 贝尔托

I'm a newbie to manipulating xml and hope i can get some help from you all.

My back end system spits out an XML file with the following node structer.

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

Now The part that need to change is the order Node.
The data that needs to be kept are the first and last payment nodes.
Any other node that falls in the middle of that can be removed or deleted.
Is there a way with E4x to do this?

Thanks for all your help.
Berto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人2024-12-10 02:15:42

不确定 E4X,但使用 RhinoEnvjsjQuery

启动 Rhino:

java -jar js.jar -opt -1

现在您应该位于犀牛提示。

加载一些库(我不建议从互联网加载,但出于示例目的),读取订单文件,解析为 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();
        }
    });
}

Not sure about E4X, but using Rhino, Envjs and jQuery:

Start Rhino:

java -jar js.jar -opt -1

Now you should be at the Rhino prompt.

Load some libraries (I don't recommend loading from the internet, but for the purposes of an example), read the orders file, parse into xml, remove the payments, then print out the result...

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)

Where "removeFirstAndLastPayments" is defined as:

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();
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文