Java Transformer 输出 < 和 > 而不是 <>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17280965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Transformer outputs < and > instead of <>
提问by RegedUser00x
I am editing an XML file in Java with a Transformer by adding more nodes. The old XML code is unchanged but the new XML nodes have <
and >
instead of <> and are on the same line. How do I get <> instead of <
and >
and how do I get line breaks after the new nodes. I already read several similar threads but wasn't able to get the right formatting. Here is the relevant portion of the code:
我正在通过添加更多节点使用 Transformer 在 Java 中编辑 XML 文件。旧的 XML 代码没有改变,但新的 XML 节点有<
and>
而不是 <> 并且在同一行。我如何获得<>而不是<
和>
以及如何在新的节点后,我会得到换行符。我已经阅读了几个类似的主题,但无法获得正确的格式。这是代码的相关部分:
// Read the XML file
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc=db.parse(xmlFile.getAbsoluteFile());
Element root = doc.getDocumentElement();
// create a new node
Element newNode = doc.createElement("Item");
// add it to the root node
root.appendChild(newNode);
// create a new attribute
Attr attribute = doc.createAttribute("Name");
// assign the attribute a value
attribute.setValue("Test...");
// add the attribute to the new node
newNode.setAttributeNode(attribute);
// transform the XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
StreamResult result = new StreamResult(new FileWriter(xmlFile.getAbsoluteFile()));
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
Thanks
谢谢
采纳答案by Woot4Moo
based on a question posted here:
基于此处发布的问题:
public void writeToOutputStream(Document fDoc, OutputStream out) throws Exception {
fDoc.setXmlStandalone(true);
DOMSource docSource = new DOMSource(fDoc);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.transform(docSource, new StreamResult(out));
}
produces:
产生:
<?xml version="1.0" encoding="UTF-8"?>
The differences I see:
我看到的差异:
fDoc.setXmlStandalone(true);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
回答by Ashish
To replace the > and other tags you can use org.apache.commons.lang3:
要替换 > 和其他标签,您可以使用 org.apache.commons.lang3:
StringEscapeUtils.unescapeXml(resp.toString());
After that you can use the following property of transformer for having line breaks in your xml:
之后,您可以使用转换器的以下属性在您的 xml 中换行:
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
回答by madhav-turangi
Try passing InputStream
instead of Writer
to StreamResult
.
尝试传递InputStream
而不是Writer
to StreamResult
。
StreamResult result = new StreamResult(new FileInputStream(xmlFile.getAbsoluteFile()));
The Transformer documentationalso suggests that.
Transformer文档也表明了这一点。