java - VTD-XML : XMLModifier. output() 抛出 IndexOutOfBoundsException

标签 java xml xml-parsing vtd-xml xml-encoding

我一直在尝试使用 VTD-XML 修改 xml 文件。该 xml 已从 java (JAX-WS) Web 服务以字符串形式接收。来自服务器的 http 响应 header 具有内容类型: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 作为编码)一切正常。

如有任何帮助,我们将不胜感激。

最佳答案

我将所有内容加载到 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)

关于java - VTD-XML : XMLModifier. output() 抛出 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148125/

相关文章:

java - Gradle同步失败: Cause: unable to find valid certification path to requested target on MacOS

java - Play 框架从 2.1 更新到 2.2 : SimpleResult not working - compile error

android - 不允许 NativeScript Angular 明文 HTTP 流量到本地主机

java - 如何解析来自网站的自定义 XML 样式错误代码响应

python - 将修改后的 Beautiful Soup 树写入文件,同时保持原始 XML 格式

java - 如何使用 SWT 将自定义图标与系统托盘气球通知/工具提示一起使用?

java - 如何从java桌面应用程序代码打开新的浏览器窗口?

html - 在 R 中解析 HTML 文件

php - 使用 PHP 解析 XML

xml - golang xml解析动态标签名