2011-09-05 1 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에서이를 수행 할 수있는 방법이 있습니까?

도움 주셔서 감사합니다. Berto에

E4X 확실하지만, Rhino, EnvjsjQuery 사용하지
+0

"reming"은 무엇입니까? 이름 바꾸기를 의미합니까? 풀이? – Oded

+0

왜 E4X를 사용해야합니까? XML 파일이 백엔드에서 생성되었다고 언급 했으므로 백엔드 언어/기술을 사용하여 XML 파일을 조작 할 수 없습니까? – clarkb86

+0

결제 노드가 2 개인 경우 어떻게해야합니까? 처음부터 끝까지 제거합니까? 순서대로 당신은 둘 다 제거합니까? –

답변

0

:

시작 코뿔소를 :

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