【问题标题】:how to write XOM document to XML file with pretty print?如何使用漂亮的打印将 XOM 文档写入 XML 文件?
【发布时间】:2014-10-23 20:02:31
【问题描述】:

我用下面的代码写了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 文件?谢谢你

【问题讨论】:

    标签: java xml xml-serialization xom


    【解决方案1】:
    FileOutputStream fileOutputStream = new FileOutputStream ("out.xml")
    serializer = new Serializer(fileOutputStream, "UTF-8");
    serializer.setIndent(4);
    serializer.write(document);
    //close fileOutputStream  ...
    

    【讨论】:

    • 非常感谢您的回答。
    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2020-01-24
    • 2013-06-21
    • 1970-01-01
    • 2015-05-03
    相关资源
    最近更新 更多