处理 xml 文件 (Java) [英] Processing xml file (Java)

查看:38
本文介绍了处理 xml 文件 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须读取和 xml 文件,进行一些更改,然后将其复制到另一个位置.我还必须保留德语特殊字符,并保持空标签不变(防止它们成为自闭合标签).为了防止自关闭标签,我使用了 Xerces 库,如链接所示:防止空的xml元素被转换成自闭元素

I have to read and xml file, do some changes, and copy it to another location. I also have to keep the german special characters, and keep the empty tags as they are (prevent them to become self-closing tags). For preventing the self closing tags, I used Xerces Library, as in the link: preventing empty xml elements are converted to self closing elements

在我的应用程序中,如果我在 xml 中的更改被忽略,则代码如下所示:

In my application, if my changes in xml are ignored, the code looks like:

    public static void main(String args[]) throws Exception {
    InputStream inputStream= new FileInputStream(new File("D:\\qwe.xml"));
    Reader reader = new InputStreamReader(inputStream,"ISO-8859-1");
    InputSource is = new InputSource(reader);
    is.setEncoding("ISO-8859-1");

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.setXmlStandalone(true);

    File file = new File ("D:\\qwerty.xml");
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(new FileOutputStream(file));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1") ;
    transformer.transform(new DOMSource(doc), new StAXResult(writer));

}

源文件的第一行是

<?xml version="1.0" encoding="UTF-8"?>

问题出在目标文件 qwerty.xml 中,其中删除了 encoding="UTF-8".在源文件中,虽然编码是UTF-8,但由于德语字符,我不得不将其设置为ISO-8859-1".我想将第一行保留为原始行,保留空标签(不是自关闭标签),并保留德语字符.我的代码只成功完成了第二和第三件事.

The problem is in the destination file, qwerty.xml, where encoding="UTF-8" is removed. In the source file, although the encoding is UTF-8, I had to set it as "ISO-8859-1" because of german characters. I want to keep the first row as the original, keep the empty tags as they are (not self-closing tags), and keep the german characters. My code succeeds to do only the second and third thing.

推荐答案

调用

Transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");

除非转换器产生序列化输出,否则无效.

has no effect unless the transformer is producing serialized output.

在您的情况下,转换器不会产生序列化输出,因为您将输出发送到 StAXResult.我不确定您为什么要使用 XmlStreamWriter 来生成输出,但是如果您想这样做,则由 XmlStreamWriter 决定编码,而不是 Transformer.

In your case the transformer is not producing serialized output because you are sending the output to a StAXResult. I'm not sure why you are using the XmlStreamWriter to produce output, but if you want to do it that way, it's the XmlStreamWriter that decides on the encoding, not the Transformer.

我原以为将 Transformer 输出发送到 StreamResult 会更简单.

I would have thought it was simpler to send the Transformer output to a StreamResult.

这篇关于处理 xml 文件 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆