【问题标题】:dom4j XML declaration in document文档中的 dom4j XML 声明
【发布时间】:2011-05-11 23:19:04
【问题描述】:

我需要从 dom4j 文档类型中删除 XML 声明

我正在通过

创建文档
doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);

DocumenHelper 解析为 Document doc 的字符串不包含 XML 声明(它来自 XML => XSL => XML 转换) 我认为 DocumentHelper 正在向文档正文添加声明?

有没有办法从

的正文中删除 XML 声明
doc

【问题讨论】:

    标签: java xml declaration dom4j


    【解决方案1】:

    我选择的更简单的解决方案是

    doc.getRootElement().asXML();
    

    【讨论】:

    • 非常简单好用。这对我有帮助!
    【解决方案2】:

    我不确定该声明在您的代码中究竟是哪里出了问题。 当我想编写一个没有声明的 xml 文件(使用 dom4j)时,我曾经有过这种情况。

    因此,如果这是您的用例:“省略声明”就是您要寻找的。 http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/OutputFormat.html

    Google 说这也可以设置为属性,但不确定它的作用。

    【讨论】:

      【解决方案3】:

      您需要与根元素而不是文档进行交互。 例如,使用PhilW 提到的默认、紧凑的OutputFormat:

      Document doc = (Document) DocumentHelper.parseText(someXMLstringWithoutXMLDeclaration);    
      final Writer writer = new StringWriter();
      new XMLWriter(writer).write(doc.getRootElement());
      String out = writer.toString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 1970-01-01
        • 2011-08-23
        • 2013-02-25
        • 2013-12-13
        相关资源
        最近更新 更多