English 中文(简体)
• 如何在Xml档案中保存涂层和变换的OM文件?
原标题:How to save parsed and changed DOM document in xml file?
  • 时间:2010-12-30 10:12:39
  •  标签:
  • java
  • xml
  • dom

我有Xml-file。 我需要读一下,作一些改动,并向一些新目的地写新的改动版本。

我可以读、写和分发这一文件(DocumentBuilderFactory ,DocumentBuilder,,等等)。

但我无法找到拯救这一档案的方法。 是否有办法使其对准文本的看法(String)?

最佳回答

与此类似:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("output.xml"));
Source input = new DOMSource(myDocument);

transformer.transform(input, output);
问题回答

如果你使用xerces-j:

public void serialise(org.w3c.dom.Document document) {
  java.io.ByteArrayOutputStream data = new java.io.ByteArrayOutputStream();
  java.io.PrintStream ps = new java.io.PrintStream(data);

  org.apache.xml.serialize.OutputFormat of =
                      new org.apache.xml.serialize.OutputFormat("XML", "ISO-8859-1", true);
  of.setIndent(1);
  of.setIndenting(true);
  org.apache.xml.serialize.XMLSerializer serializer =
                      new org.apache.xml.serialize.XMLSerializer(ps, of);
  // As a DOM Serializer
  serializer.asDOMSerializer();
  serializer.serialize(document);

  return data.toString();
}

这将使你有可能确定Xml格式

new XMLWriter(new FileOutputStream(fileName),
              new OutputFormat(){{
                        setEncoding("UTF-8");
                        setIndent("    ");
                        setTrimText(false);
                        setNewlines(true);
                        setPadText(true);
              }}).write(document);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...