Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java VTD-XML:XMLModifier.output()引发IndexOutOfBoundsException_Java_Xml_Xml Parsing_Vtd Xml_Xml Encoding - Fatal编程技术网

Java VTD-XML:XMLModifier.output()引发IndexOutOfBoundsException

Java VTD-XML:XMLModifier.output()引发IndexOutOfBoundsException,java,xml,xml-parsing,vtd-xml,xml-encoding,Java,Xml,Xml Parsing,Vtd Xml,Xml Encoding,我一直在尝试使用VTD-xml修改xml文件。xml是作为字符串从java(JAX-WS)web服务接收的。来自服务器的http响应头具有内容类型:text/xml和charset=utf-8 代码如下: private static byte[] getDataFromFile(String filePath) throws IOException { File file = new File(filePath); FileInputStream fileInputStream

我一直在尝试使用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"?>
如果我将文件的编码更改为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)

我把所有东西都加载到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)


您能发布一个带有该编码头的XML文件的简化版本吗?谢谢您的及时回复。我已经上传了一个文件,使用它我能够重现异常。该文件是utf-16编码的,我相应地修改了getDataFromFile()方法(即,仅读取原始字节而不指定任何编码)以传递到vtd-xml。您可以通过电子邮件将此xml文件或更好的方式将整个测试用例发送给我吗jzhang@ximpleware.com?@vtd xml作者我已将文件发送给您。请让我知道你什么时候看了这个问题。我会尽快回复你…你能发布一个带有编码头的XML文件的简化版本吗?谢谢你的及时回复。我已经上传了一个文件,使用它我能够重现异常。该文件是utf-16编码的,我相应地修改了getDataFromFile()方法(即,仅读取原始字节而不指定任何编码)以传递到vtd-xml。您可以通过电子邮件将此xml文件或更好的方式将整个测试用例发送给我吗jzhang@ximpleware.com?@vtd xml作者我已将文件发送给您。请让我知道,当你看了这个问题。我会尽快给你回…是的,你得到了完全相同的错误。我用的是2.11。我也在使用同一个文件。谢谢。对不起,我弄错了。请像这样修改getDataFromFile()方法以重现异常<代码>私有静态字节[]getDataFromFile(String filePath)抛出IOException{File File=new File(filePath);FileInputStream FileInputStream=new FileInputStream(File);byte[]byteArray=new byte[(int)File.length()];FileInputStream.read(byteArray);return byteArray;}如果打印字节数组的第0个索引,则第一个字节将为-2。有线索吗?你的档案有多大?192字节长?我打印了前三个字节和文件的长度。。。你能核实一下你发给我的文件吗?还是重新发送文件?我又给你发送了文件。该文件应为380字节长。我得到的前三个字节是-2、-1和0。告诉我你发现了什么。谢谢。是的,你得到了完全相同的错误。我用的是2.11。我也在使用同一个文件。谢谢。对不起,我弄错了。请像这样修改getDataFromFile()方法以重现异常<代码>私有静态字节[]getDataFromFile(String filePath)抛出IOException{File File=new File(filePath);FileInputStream FileInputStream=new FileInputStream(File);byte[]byteArray=new byte[(int)File.length()];FileInputStream.read(byteArray);return byteArray;}如果打印字节数组的第0个索引,则第一个字节将为-2。有线索吗?你的档案有多大?192字节长?我打印了前三个字节和文件的长度。。。你能核实一下你发给我的文件吗?还是重新发送文件?我又给你发送了文件。该文件应为380字节长。我得到的前三个字节是-2、-1和0。告诉我你发现了什么。谢谢