gpt4 book ai didi

java - 使用 Xerces 将 DOM 序列化为 FileOutputStream

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:04:06 24 4
gpt4 key购买 nike

我正在使用 this链接以使用 DOM 生成 XML 文件。它说“Xerces 解析器与 JDK 1.5 发行版捆绑在一起。因此您无需单独下载解析器。”

但是,当我在我的 Eclipse Helios 中编写以下行时,即使我的系统中有 Java 1.6,它也会出现编译时错误。

import org.apache.xml.serialize.XMLSerializer;

为什么会这样?

最佳答案

Xerces 确实与 JDK 捆绑在一起,但您应该将它与 javax.xml.parsers 下的 JAXP API 一起使用。检查下面程序的输出。

此外,要序列化 ​​XML Document,您应该使用 DOM Level 3 加载和保存(存在于 JDK 中)或没有样式表的 XSLT 转换(身份转换)。其余的取决于具体的实现。 Xerces XMLSerializer 已弃用:

Deprecated. This class was deprecated in Xerces 2.9.0. It is recommended that new applications use the DOM Level 3 LSSerializer or JAXP's Transformation API for XML (TrAX) for serializing XML. See the Xerces documentation for more information.

这是一个使用 DOM 级别 3 进行序列化的示例:

import org.w3c.dom.*;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.*;

public class DOMExample3 {

public static void main(String[] args) throws Exception {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
if (impl == null) {
System.out.println("No DOMImplementation found !");
System.exit(0);
}

System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName());

LSParser parser = impl.createLSParser(
DOMImplementationLS.MODE_SYNCHRONOUS,
"http://www.w3.org/TR/REC-xml");
// http://www.w3.org/2001/XMLSchema
System.out.printf("LSParser: %s\n", parser.getClass().getName());

if (args.length == 0) {
System.exit(0);
}

Document doc = parser.parseURI(args[0]);

LSSerializer serializer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setEncoding("UTF-8");
output.setByteStream(System.out);
serializer.write(doc, output);
System.out.println();
}
}

这是一个身份转换的例子:

import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class DOMExample2 {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();

System.out.println("Parsing XML document...");
Document doc;
doc = parser.parse(args[0]);

// Xerces Java 2

/* Deprecated. This class was deprecated in Xerces 2.9.0.
* It is recommended that new applications use the DOM Level 3
* LSSerializer or JAXP's Transformation API for XML (TrAX)
* for serializing XML and HTML.
* See the Xerces documentation for more information.
*/
/*
System.out.println("XERCES: Displaying XML document...");
OutputFormat of = new OutputFormat(doc, "ISO-8859-1", true);
PrintWriter pw = new PrintWriter(System.out);
BaseMarkupSerializer bms = new XMLSerializer(pw, of);
bms.serialize(doc);
*/
// JAXP

System.out.println("JAXP: Displaying XML document...");
TransformerFactory transFactory = TransformerFactory.newInstance();
System.out.println(transFactory.getClass().getName());
//transFactory.setAttribute("indent-number", 2);
Transformer idTransform = transFactory.newTransformer();
idTransform.setOutputProperty(OutputKeys.METHOD, "xml");
idTransform.setOutputProperty(OutputKeys.INDENT,"yes");
// Apache default indentation is 0
idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Source input = new DOMSource(doc);
Result output = new StreamResult(System.out);
idTransform.transform(input, output);
}
}

关于java - 使用 Xerces 将 DOM 序列化为 FileOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com