I'm trying to remove SOAP
and ns2
nodes from this XML :
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns2:createCustomer>
<Customer>
<CustomerId/>
<names>
<firstName>fName</firstName>
<lastName>lName</lastName>
<middleName>nName</middleName>
<nickName/>
</names>
<addressList>
<address>
<streetInfo>
<houseNumber>22</houseNumber>
<baseName>Street base name</baseName>
<district>kewl district</district>
</streetInfo>
<zipcode>22231</zipcode>
<state>xxx</state>
<country>xxxz</country>
<primary>true</primary>
</address>
</addressList>
<SSN>561381</SSN>
<phone>
<homePhone>123123123</homePhone>
<officePhone/>
<homePhone>21319414</homePhone>
</phone>
<preferred>true</preferred>
</Customer>
</ns2:createCustomer>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Is this possible before this :
Document doc = parser.parse(xmlFile);
NodeList startlist = doc.getChildNodes();
I tried to read this as String then writing it back to the XML file like this :
private void preParsing(String fileName,String ...tags) {
try {
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
for (String string : tags) {
if(!line.contains(string)){
sb.append(line);
sb.append("\n");
}
}
}
System.out.println(sb.toString());
br.close();
} catch (IOException e) {
System.err.println("Error occured: " + e.getMessage());
}
}
It worked if I ignore only one tag like :
preParsing("src/main/resources/test.xml", "SOAP");
But it didn't work when I pass more than one tag argument to ignore/remove from file. I'm sure there is more elegant way of doing this I just can't think of any.