我正在开发一个系统,该系统应该能够读取任何(或至少是任何格式良好的)XML文件,操作一些节点并将它们写回到同一个文件中。我希望我的代码尽可能通用,我不想要
目标是使源文件完全不变,除了通过XPath检索的已更改的节点。我想逃避标准的javax.xml。
到目前为止我的进展:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setAttribute("http://xml.org/sax/features/namespaces", true);
factory.setAttribute("http://xml.org/sax/features/validation", false);
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setIgnoringComments(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(inStream));
这会成功地将XML源加载到org.w3c.dom.Document中,忽略DTD验证。我可以替换,然后使用
Source source = new DOMSource(document);
Result result = new StreamResult(getOutputStream(getPath()));
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
把它写回来。这几乎是完美的。但无论我做什么,Doctype标签都不见了。在调试时,我看到解析后在Document对象中有一个DeferredDoctypeImpl [log4j:configuration:null]对象,但它在某种程度上是错误的,空的或被忽略的。我测试的文件就是这样开始的(但是对于其他文件类型它是一样的):
<?xml version =“1.0”encoding =“UTF-8”?>
<!DOCTYPE log4j:配置系统“log4j.dtd”>
< log4j:configuration xmlns:log4j =“http://jakarta.apache.org/log4j/”debug =“false”>
[...]
我认为有很多(简单?)方法涉及黑客攻击或将额外的JAR引入项目中。但我更愿意使用我已经使用过的工具。
答案 0 :(得分:2)
抱歉,现在使用XMLSerializer代替Transformer ......
答案 1 :(得分:0)
以下是使用JDK中的LSSerializer实现的方法:
private void writeDocument(Document doc, String filename)
throws IOException {
Writer writer = null;
try {
/*
* Could extract "ls" to an instance attribute, so it can be reused.
*/
DOMImplementationLS ls = (DOMImplementationLS)
DOMImplementationRegistry.newInstance().
getDOMImplementation("LS");
writer = new OutputStreamWriter(new FileOutputStream(filename));
LSOutput lsout = ls.createLSOutput();
lsout.setCharacterStream(writer);
/*
* If "doc" has been constructed by parsing an XML document, we
* should keep its encoding when serializing it; if it has been
* constructed in memory, its encoding has to be decided by the
* client code.
*/
lsout.setEncoding(doc.getXmlEncoding());
LSSerializer serializer = ls.createLSSerializer();
serializer.write(doc, lsout);
} catch (Exception e) {
throw new IOException(e);
} finally {
if (writer != null) writer.close();
}
}
需要进口:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
我知道这是一个已经回答的旧问题,但我认为技术细节可能有助于某人。
答案 2 :(得分:0)
我尝试使用LSSerializer库,并且在保留Doctype方面无法使用它。这是斯蒂芬可能使用的解决方案 注意:这是在scala中但使用了一个java库,所以只需转换代码
import com.sun.org.apache.xml.internal.serialize.{OutputFormat, XMLSerializer}
def transformXML(root: Element, file: String): Unit = {
val doc = root.getOwnerDocument
val format = new OutputFormat(doc)
format.setIndenting(true)
val writer = new OutputStreamWriter(new FileOutputStream(new File(file)))
val serializer = new XMLSerializer(writer, format)
serializer.serialize(doc)
}