2011-09-05 200 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