我是操作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>现在需要更改的部分是order Node。需要保存的数据是第一个和最后一个支付节点。任何其他落在其中的节点都可以被移除或删除。有没有办法用E4x做到这一点?
谢谢你的帮助。贝尔托
发布于 2011-09-06 05:39:34
不确定是否使用E4X,但使用Rhino、Envjs和jQuery
启动Rhino:
java -jar js.jar -opt -1现在您应该在Rhino提示符下。
加载一些库(我不建议从internet加载,但出于示例的目的),读取orders文件,解析为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();
}
});
}https://stackoverflow.com/questions/7312211
复制相似问题