2011-09-05 143 views
0

我是一个操作xml的新手,希望我能从你们那里得到一些帮助。删除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有没有办法做到这一点?

感谢您的帮助。 叶贝

+0

什么是 “reming”?你的意思是重命名?删除? – Oded

+0

为什么你需要使用E4X?您提到XML文件是在后端生成的,那么您是否无法使用后端语言/技术来操作XML文件? – clarkb86

+0

如果有2个付款节点会怎么样?你首先和最后删除?为了避免这两个问题,你可以删除它们吗? –

回答

0

不确定E4X,但使用RhinoEnvjsjQuery

开始犀牛:

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(); 
     } 
    }); 
} 
+0

Paul, 感谢您回复我。 我会试一试。 并让你知道。 干杯 Berto – Berto