我用下面的代码写了xml
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
Element root = new Element("ADT");
root.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema");
root.setNamespaceURI("urn:hl7-org:v3");
Element msh = new Element("MSH", "urn:hl7-org:v3");
Element msh_1 = new Element("MSH.1", "urn:hl7-org:v3");
msh_1.appendChild(m.getFieldSeperator());
Element msh_2 = new Element("MSH.2", "urn:hl7-org:v3");
msh_2.appendChild(m.getEncodingCharacters());
msh.appendChild(msh_1);
msh.appendChild(msh_2);
root.appendChild(msh);
Document document = new Document(root);
Serializer serializer;
serializer = new Serializer(System.out, "UTF-8");
serializer.setIndent(4);
serializer.write(document);
这样可以在控制台上以漂亮的打印效果来打印我的xml文档。是否有人可以帮助我将相同的内容保存到xml文件?谢谢
最佳答案
FileOutputStream fileOutputStream = new FileOutputStream ("out.xml")
serializer = new Serializer(fileOutputStream, "UTF-8");
serializer.setIndent(4);
serializer.write(document);
//close fileOutputStream ...
关于java - 如何用 pretty-print 将XOM文档写入XML文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26536428/