我必须根据这些节点的子元素中的值从XML文件中删除一个节点。 实际文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<_-AMS_-OBRXINVOIC02>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>132</MANDT>
<DOCNUM>001</DOCNUM>
<DOCREL>620</DOCREL>
<STATUS>30</STATUS>
<DIRECT>1</DIRECT>
<OUTMOD>4</OUTMOD>
<IDOCTYP>INVOIC02</IDOCTYP>
<CIMTYP>/AMS/OBRXINVOIC02</CIMTYP>
<MESTYP>INVOIC</MESTYP>
<SNDPOR>SAPABI</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>ABIPRR3132</SNDPRN>
<RCVPOR>XIS</RCVPOR>
<RCVPRT>LS</RCVPRT>
<RCVPFC>LS</RCVPFC>
<RCVPRN>BREEDI0001</RCVPRN>
<CREDAT>20110406</CREDAT>
<CRETIM>071233</CRETIM>
<SERIAL>20110406071232</SERIAL>
</EDI_DC40>
<E1EDK01 SEGMENT="1">
<CURCY>BRL</CURCY>
<HWAER>BRL</HWAER>
<WKURS>1.00000</WKURS>
<ZTERM>Z171</ZTERM>
<BSART>INVO</BSART>
<BELNR>0969286361</BELNR>
<NTGEW>2949.090</NTGEW>
<BRGEW>3329.780</BRGEW>
<GEWEI>KGM</GEWEI>
<FKART_RL>ZLR</FKART_RL>
<RECIPNT_NO>0001061877</RECIPNT_NO>
<FKTYP>L</FKTYP>
<_-AMS_-OBR_E1EDK01 SEGMENT="1">
<ANZPK>00449</ANZPK>
<VOLTOT>7.688</VOLTOT>
<SERIES>001</SERIES>
<NFNUM>000000</NFNUM>
<NFTYPE>V1</NFTYPE>
<MODEL>55</MODEL>
<PSTDAT>20110406</PSTDAT>
<CREDAT>20110406</CREDAT>
<CRETIM>034335</CRETIM>
<NFFRE>0.00</NFFRE>
<NFINS>0.00</NFINS>
<NFOTH>0.00</NFOTH>
<NFDIS>2200.90-</NFDIS>
<NFNET>28757.95</NFNET>
<NFTOT>28186.49</NFTOT>
<ZNFNET>28186.49</ZNFNET>
<PROCESS>X</PROCESS>
<NFENUM>000423391</NFENUM>
</_-AMS_-OBR_E1EDK01>
</E1EDK01>
<E1EDKA1 SEGMENT="1">
<PARVW>RS</PARVW>
<LIFNR>0001061877</LIFNR>
<LAND1>BR</LAND1>
<SPRAS>P</SPRAS>
<BNAME>BRSANTOSRO5</BNAME>
<PAORG>BRN1</PAORG>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>1B</PARVW>
<PARTN>0000899500</PARTN>
<NAME1>L6 BRN1 KA CAR FL Matriz Dia</NAME1>
<ORT01>Sao Paulo</ORT01>
<LAND1>BR</LAND1>
<SPRAS>P</SPRAS>
<REGIO>SP</REGIO>
<SPRAS_ISO>PT</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>1C</PARVW>
<PARTN>0000899499</PARTN>
<NAME1>L5 BRN1 KA Car FL Matriz Dia</NAME1>
<ORT01>Sao Paulo</ORT01>
<LAND1>BR</LAND1>
<SPRAS>P</SPRAS>
<REGIO>SP</REGIO>
<SPRAS_ISO>PT</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>1D</PARVW>
<PARTN>0002766603</PARTN>
<NAME1>L4 BRN1 KA Car Dia</NAME1>
<ORT01>Rio de Janeiro</ORT01>
<PSTLZ>22775-112</PSTLZ>
<LAND1>BR</LAND1>
<SPRAS>P</SPRAS>
<REGIO>RJ</REGIO>
<SPRAS_ISO>PT</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>SS</PARVW>
<PARTN>0000899472</PARTN>
<NAME1>L3 BRN1 KA Carrefour</NAME1>
<ORT01>Sao Paulo</ORT01>
<LAND1>BR</LAND1>
<SPRAS>P</SPRAS>
<REGIO>SP</REGIO>
<SPRAS_ISO>PT</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>SS</PARVW>
<PARTN>0000021177</PARTN>
<NAME1>AMS View</NAME1>
<ORT01>Vevey</ORT01>
<LAND1>CH</LAND1>
<SPRAS>E</SPRAS>
<ANRED>Sociedade</ANRED>
<REGIO>VD</REGIO>
<SPRAS_ISO>EN</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>1G</PARVW>
<PARTN>0000021174</PARTN>
<NAME1>Global View</NAME1>
<ORT01>Vevey</ORT01>
<LAND1>CH</LAND1>
<SPRAS>E</SPRAS>
<ANRED>Sociedade</ANRED>
<REGIO>VD</REGIO>
<SPRAS_ISO>EN</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>AG</PARVW>
<PARTN>0001061877</PARTN>
<NAME1>Dia Brasil Sociedade Ltda</NAME1>
<STRAS>R Agricola La Paz Tristante SN</STRAS>
<ORT01>Osasco</ORT01>
<PSTLZ>06276-035</PSTLZ>
<LAND1>BR</LAND1>
<TELF1>11 38868131</TELF1>
<SPRAS>P</SPRAS>
<ORT02>Pq Industrial Anhanguera</ORT02>
<REGIO>SP</REGIO>
<ILNNR>7899288907049</ILNNR>
<SPRAS_ISO>PT</SPRAS_ISO>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>SS</PARVW>
<PARTN>0001061877</PARTN>
<NAME1>Dia Brasil Sociedade Ltda</NAME1>
<STRAS>R Agricola La Paz Tristante SN</STRAS>
<ORT01>Osasco</ORT01>
<PSTLZ>06276-035</PSTLZ>
<LAND1>BR</LAND1>
<TELF1>11 38868131</TELF1>
<SPRAS>P</SPRAS>
<ORT02>Pq Industrial Anhanguera</ORT02>
<REGIO>SP</REGIO>
<ILNNR>7899288907049</ILNNR>
<SPRAS_ISO>PT</SPRAS_ISO>
<_-AMS_-OBR_E1EDKA1 SEGMENT="1">
<STCD1>03476811021159</STCD1>
<STCD3>492.488.462.119</STCD3>
</_-AMS_-OBR_E1EDKA1>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>RG</PARVW>
<PARTN>0000658366</PARTN>
<NAME1>Carrefour Com e Industria Ltda</NAME1>
<STRAS>Av das Americas 5150</STRAS>
<ORT01>Rio de Janeiro</ORT01>
<PSTLZ>22640-102</PSTLZ>
<LAND1>BR</LAND1>
<TELF1>21 4313011</TELF1>
<TELFX>21 3252624</TELFX>
<SPRAS>P</SPRAS>
<ORT02>Barra da Tijuca</ORT02>
<REGIO>RJ</REGIO>
<ILNNR>7891103001024</ILNNR>
<SPRAS_ISO>PT</SPRAS_ISO>
<_-AMS_-OBR_E1EDKA1 SEGMENT="1">
<STCD1>45543915000262</STCD1>
<STCD3>81536600</STCD3>
</_-AMS_-OBR_E1EDKA1>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>TF</PARVW>
<PARTN>0100301696</PARTN>
<NAME1>Transportadora Nosei Ltda</NAME1>
<STRAS>Rua Carlos Weber 512</STRAS>
<ORT01>Sao Paulo</ORT01>
<PSTLZ>05303-000</PSTLZ>
<LAND1>BR</LAND1>
<TELF1>11 38329460</TELF1>
<TELFX>11 38329460</TELFX>
<SPRAS>P</SPRAS>
<ORT02>Vila Leopoldina</ORT02>
<REGIO>SP</REGIO>
<SPRAS_ISO>PT</SPRAS_ISO>
<_-AMS_-OBR_E1EDKA1 SEGMENT="1">
<STCD1>54043435000142</STCD1>
<STCD3>111173871116</STCD3>
</_-AMS_-OBR_E1EDKA1>
</E1EDKA1>
<E1EDKA1 SEGMENT="1">
<PARVW>BK</PARVW>
<LIFNR>BR10</LIFNR>
<NAME1>Nestle Brasil Ltda</NAME1>
<STRAS>Av Dr Chucri Zaidan 246</STRAS>
<ORT01>Sao Paulo</ORT01>
<COUNC>SP</COUNC>
<PSTLZ>04583-110</PSTLZ>
<LAND1>BR</LAND1>
<TELF1>11 55084400</TELF1>
<TELFX>11 55085475</TELFX>
<ORT02>Vila Cordeiro</ORT02>
<REGIO>SP</REGIO>
<_-AMS_-OBR_E1EDKA1 SEGMENT="1">
<ZCCODE>7891000001004</ZCCODE>
</_-AMS_-OBR_E1EDKA1>
</E1EDKA1>
<E1EDK02 SEGMENT="1">
<QUALF>001</QUALF>
<BELNR>813763</BELNR>
<DATUM>20110404</DATUM>
</E1EDK02>
<E1EDK03 SEGMENT="1">
<IDDAT>024</IDDAT>
<DATUM>20110406</DATUM>
</E1EDK03>
<E1EDK17 SEGMENT="1">
<QUALF>002</QUALF>
<LKOND>CIF</LKOND>
<LKTEXT>Custo, Seguro e Frete</LKTEXT>
</E1EDK17>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>3565689</BRNUM>
<BNAME>BANCO ABN AMRO REAL S A</BNAME>
<BALOC>S#o Paulo</BALOC>
<ACNUM>5704752</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>00191893</BRNUM>
<BNAME>Banco Do Brasil S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>331415</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>00193070</BRNUM>
<BNAME>BANCO DO BRASIL S A</BNAME>
<BALOC>São Paulo</BALOC>
<ACNUM>331415</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>47901</BRNUM>
<BNAME>BANKBOSTON BANCO MULTIPLO SA</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>89821</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>23722372</BRNUM>
<BNAME>BANCO BRADESCO S A</BNAME>
<BALOC>São Paulo</BALOC>
<ACNUM>4617</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>1040988</BRNUM>
<BNAME>Caixa Economica Federal</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>30001500</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>74551</BRNUM>
<BNAME>Banco Citibank S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>1002058</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>3999454</BRNUM>
<BNAME>HSBC Bank Brasil S A-Banco Multiplo</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>4425</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>3417912</BRNUM>
<BNAME>Banco Itau S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>1445</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>34172938</BRNUM>
<BNAME>Banco Itau S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>82</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>1511935</BRNUM>
<BNAME>Banco Nossa Caixa S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>4100342</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>03372271</BRNUM>
<BNAME>BANCO DO ESTADO DE SAO PAULO SA BANESPA</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>13001181-7</ACNUM>
</E1EDK28>
<E1EDK28 SEGMENT="1">
<BCOUN>BR</BCOUN>
<BRNUM>4090352</BRNUM>
<BNAME>Unibanco Uniao De Bancos Brasileiros S A</BNAME>
<BALOC>Sao Paulo</BALOC>
<ACNUM>102049</ACNUM>
</E1EDK28>
<E1EDKT1 SEGMENT="1">
<TDID>Z018</TDID>
<TSSPRAS>P</TSSPRAS>
<TSSPRAS_ISO>PT</TSSPRAS_ISO>
<E1EDKT2 SEGMENT="1">
<TDLINE>"O dest.deverá,nas operações com merc ou prest de serv receb c imposto</TDLINE>
<TDFORMAT>*</TDFORMAT>
</E1EDKT2>
<E1EDKT2 SEGMENT="1">
<TDLINE>retido,escrit o doc fiscal nos termos art 278 do RICMS".</TDLINE>
</E1EDKT2>
</E1EDKT1>
<E1EDKT1 SEGMENT="1">
<TDID>Z019</TDID>
<TSSPRAS>P</TSSPRAS>
<TSSPRAS_ISO>PT</TSSPRAS_ISO>
<E1EDKT2 SEGMENT="1">
<TDLINE>VENDAS COM CONDIÇÃO CIF</TDLINE>
<TDFORMAT>*</TDFORMAT>
</E1EDKT2>
</E1EDKT1>
<E1EDK14 SEGMENT="1">
<QUALF>008</QUALF>
<ORGID>BRN1</ORGID>
</E1EDK14>
<E1EDK14 SEGMENT="1">
<QUALF>007</QUALF>
<ORGID>06</ORGID>
</E1EDK14>
<E1EDK14 SEGMENT="1">
<QUALF>006</QUALF>
<ORGID>00</ORGID>
</E1EDK14>
<E1EDK14 SEGMENT="1">
<QUALF>015</QUALF>
<ORGID>F2</ORGID>
</E1EDK14>
<E1EDK14 SEGMENT="1">
<QUALF>003</QUALF>
<ORGID>BR10</ORGID>
</E1EDK14>
<E1EDS01 SEGMENT="1">
<SUMID>011</SUMID>
<SUMME>28186.49</SUMME>
<WAERQ>BRL</WAERQ>
</E1EDS01>
<E1EDS01 SEGMENT="1">
<SUMID>010</SUMID>
<SUMME>24332.15</SUMME>
<WAERQ>BRL</WAERQ>
</E1EDS01>
我的要求是,如果其子元素“PARVW”带有值“SS”,我想从文件中删除整个段“E1EDKA1”。 在我的情况下,我想删除PARVW即将作为SS的“E1EDKA1”的3段
我试着从我这边来:我的代码如下:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fis);
NodeList nList = doc.getElementsByTagName("E1EDKA1");
System.out.println(nList.getLength());
if (nList != null && nList.getLength() > 0) {
for (int i = 0; i < nList.getLength();i++) {
Node node = nList.item(i);
// System.out.println(node);
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("PARVW");
String title = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println(title);
if (title.equals("SS")) {
System.out.println("Test");
doc.getFirstChild().removeChild(node);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:/Users/ssourav/Desktop/Idoc12_out123.xml"));
transformer.transform(source, result);
请帮助我获取此输出并建议我代码中的问题在哪里。 实际上程序在遇到第一个“SS”值时就会出现,并且没有任何内容写入输出文件。
答案 0 :(得分:1)
XPath path = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) path.evaluate("*/E1EDKA1[PARVW/text()='SS']", doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
nl.item(i).getParentNode().removeChild(nl.item(i));
}
答案 1 :(得分:0)
你可以这样做,
void removeElement(Document docObj) {
NodeList records= doc.getElementsByTagName("EDI_DC40");
Element record= null;
//iterate over all records
for(int i=0; i<records.getLength();i++){
record= (Element) records.item(i);
Node statusNode = emp.getElementsByTagName("STATUS").item(0);
record.removeChild(statusNode);
}
}
答案 2 :(得分:0)
如果没有写任何内容,我认为你会得到例外。你没有追踪它吗?
尝试以这种方式移除节点:e.getParentNode().removeChild(e);