1

我一直在尝试使用 VTD-XML 修改 xml 文件。xml 已作为字符串从 java (JAX-WS) Web 服务接收。来自服务器的 http 响应标头具有内容类型:text/xmlcharset = utf-8

这是代码:

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();
    }
}

xml 上面的声明是这样的:

<?xml version="1.0" encoding="utf-16"?>

这就是为什么我在getDataFromFile()方法中以UTF-16格式从文件中读取字节的原因。否则,代码将引发异常,指出它无法切换到编码 UTF-16。

现在上面的代码抛出以下异常:

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)

如果我将文件的编码更改为UTF-8并相应地修改getDataFromFile()方法(也就是说,我们从文件中读取字节而不指定任何编码UTF-8 作为编码)一切正常。

任何帮助,将不胜感激。

4

1 回答 1

0

我将所有内容都加载到了eclipse中。我得到的第一个字节是-17,它是二进制的0xef,这是一个无效的BOM起始字节。仅供参考,BOM 应该是 0xff 0xfe 或 0xfe 0xff ......所以它在解析例程中失败了......

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()];
        //byteArray[0]=(byte)0xff;
        //byteArray[1]=(byte)0xfe;
        //byteArray[2]=0x00;

        fileInputStream.read(byteArray);

        System.out.println(" first byte "+byteArray[0]);
        System.out.println(" second byte "+byteArray[1]);
        System.out.println(" third byte "+byteArray[2]);
        System.out.println(" length "+file.length());
        return byteArray;
    }

异常日志如下所示:

 first byte -17
 second byte -69
 third byte -65
 length 192
com.ximpleware.ParseException: XML decl error: Can't switch encoding to UTF-16
Line Number: 1 Offset: 39
    at com.ximpleware.VTDGen.matchUTFEncoding(VTDGen.java:2241)
    at com.ximpleware.VTDGen.process_dec_attr(VTDGen.java:3385)
    at com.ximpleware.VTDGen.parse(VTDGen.java:2632)
    at DOMTest.removeNode.cutOffXmlByXpath(removeNode.java:28)
    at DOMTest.removeNode.main(removeNode.java:46)
于 2016-03-24T05:29:07.710 回答