使用 Java 以不同的字符编码编写 XML

发布于 2024-09-03 18:38:03 字数 1032 浏览 2 评论 0原文

我正在尝试编写一个可以再次读入我的程序的 XML 库文件。

文件编写器代码如下:

XMLBuilder builder = new XMLBuilder();
Document doc = builder.build(bookList);
DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer ser = implLS.createLSSerializer();
String out = ser.writeToString(doc);

//System.out.println(out);

try{
    FileWriter fstream = new FileWriter(location);
    BufferedWriter outwrite = new BufferedWriter(fstream);
    outwrite.write(out);
    outwrite.close();
}catch (Exception e){
}

上面的代码确实编写了一个xml文档。

但是,在 XML 标头中,文件以 UTF-16 编码是一个属性。

当我读入文件时,出现错误:

“prolog 中不允许内容”

当编码属性手动更改为 UTF-8 时,不会发生此错误。

我试图让上面的代码编写一个以 UTF-8 编码的 XML 文档,或者成功解析一个 UTF-16 文件。

解析的代码是

DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
DocumentBuilder loader = factory.newDocumentBuilder();
Document document = loader.parse(filename);

最后一行返回错误。

I am attempting to write an XML library file that can be read again into my program.

The file writer code is as follows:

XMLBuilder builder = new XMLBuilder();
Document doc = builder.build(bookList);
DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer ser = implLS.createLSSerializer();
String out = ser.writeToString(doc);

//System.out.println(out);

try{
    FileWriter fstream = new FileWriter(location);
    BufferedWriter outwrite = new BufferedWriter(fstream);
    outwrite.write(out);
    outwrite.close();
}catch (Exception e){
}

The above code does write an xml document.

However, in the XML header, it is an attribute that the file is encoded in UTF-16.

when i read in the file, i get the error:

"content not allowed in prolog"

this error does not occur when the encoding attribute is manually changed to UTF-8.

I am trying to get the above code to write an XML document encoded in UTF-8, or successfully parse a UTF-16 file.

the code for parsing in is

DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
DocumentBuilder loader = factory.newDocumentBuilder();
Document document = loader.parse(filename);

the last line returns the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聊慰 2024-09-10 18:38:03

LSSerializer writeToString 方法不允许序列化器选择编码。

通过 LSOutput 实例的 setEncoding 方法,LSSerializer 的 write 方法可用于更改编码。 LSOutput CharacterStream 可以设置为 BufferedWriter 的实例,这样来自 LSSerializer 的 write 调用就会写入文件。

the LSSerializer writeToString method does not allow the Serializer to pick a encoding.

with the setEncoding method of an instance of LSOutput, LSSerializer's write method can be used to change encoding. the LSOutput CharacterStream can be set to an instance of the BufferedWriter, such that calls from LSSerializer to write will write to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文