我有一个处理 XML 文件以读取一些值的 Java 代码。我收到一个错误:无效的 UTF8 编码,我试图将文件内容复制到 NotePad++ 上的另一个文件中,该过程运行良好,但如果我只将文件另存为其他名称,则会给出相同的错误。抱歉,我不能把我的 XML 文件放在这里,因为它太大了,我只会放 header 和 trailer。感谢您提供任何帮助来解决此错误。我处理 xml 文件的 java 代码:
XPathFactory f=XPathFactory.newInstance();
XPath x=f.newXPath();
InputSource source=new InputSource(new FileInputStream("C:\\Users\\cc\\eclipse-workspace\\data\\file.xml") );
InputSource source2=new InputSource(new FileInputStream("C:\\Users\\cc\\eclipse-workspace\\data\\file.xml") );
XPathExpression trlr=x.compile("pers/trailer/text()");
XPathExpression hdr=x.compile("pers/header/CD/text()");
String s=trlr.evaluate(source);
String s2=hdr.evaluate(source2);
System.out.println("header :"+s+" trailer"+s2);
pers 是 xml 文件中的根标记:
XML 文件如下所示:
<?xml version = '1.0' encoding = 'UTF-8'?>
<pers>
<header>555</header>
.
.
.
.
<trailer>666</trailer>
</pers>
智慧大石
守着一只汪
相关分类