I have been to trying to modify a xml file using VTD-XML.The xml has been received from a java (JAX-WS) web service as a String. The http response header from the server has content type : text/xml and charset = utf-8.
Here is the code :
private static byte[] getDataFromFile(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] byteArray = new byte[(int) file.length()];
fileInputStream.read(byteArray);
String fileData = new String(byteArray);
byteArray = fileData.getBytes("UTF-16");
return byteArray;
}
private static void cutOffXmlByXpath(String xpathQuery, String inputFilePath, String outputFilePath) throws Exception {
byte[] byteArray = getDataFromFile(inputFilePath);
VTDGen vg = new VTDGen();
vg.setDoc(byteArray);
vg.parse(false);
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath(xpathQuery);
XMLModifier xm = new XMLModifier(vn);
while((ap.evalXPath())!=-1) {
xm.remove(vn.getElementFragment());
}
xm.output(outputFilePath);
}
public static void main(String[] args) {
try {
cutOffXmlByXpath("//Part[@identifier != 'ID Page. Interview and Profile Form' and @identifier != 'Reports']", FILE_PATH, OUTPUT_FILE_PATH);
} catch (Exception e) {
e.printStackTrace();
}
}
The declaration above the xml is such :
<?xml version="1.0" encoding="utf-16"?>
Which is why I am reading the bytes from the file in UTF-16 in the getDataFromFile() method. Otherwise, the code throws an exception stating that that it cannot switch to encoding UTF-16.
Now the code above throws the following exception :
java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:345)
at com.ximpleware.XMLModifier.output(XMLModifier.java:2068)
at com.ximpleware.XMLModifier.output(XMLModifier.java:2193)
at Main.cutOffXmlByXpath(Main.java:111)
at Main.main(Main.java:161)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
If I change the encoding of the file to UTF-8 and modify the getDataFromFile() method accordingly (that is, we read bytes from the file without specifying any encoding or UTF-8 as encoding) everything works fine.
Any help would be appreciated.