【问题标题】:GPathResult to String without XML declarationGPathResult 到没有 XML 声明的字符串
【发布时间】:2016-08-12 12:22:21
【问题描述】:

我正在将 GPathResult 转换为 String 使用

def gPathResult = new XmlSlurper().parseText('<node/>')
XmlUtil.serialize(gPathResult)

它工作正常,但我的 XML 前面有 XML 声明

<?xml version="1.0" encoding="UTF-8"?><node/>

如果开头没有&lt;?xml version="1.0" encoding="UTF-8"?&gt;,如何将GPathResult 转换为String

【问题讨论】:

    标签: xml groovy gpath


    【解决方案1】:

    使用XmlParser 代替XmlSlurper

    def root = new XmlParser().parseText('<node/>')
    new XmlNodePrinter().print(root)
    

    使用new XmlNodePrinter(preserveWhitespace: true) 可能也是您尝试做的事情的朋友。请参阅文档中的其余选项:http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html

    【讨论】:

      【解决方案2】:

      这是 XmlUtil 类中的代码。您会注意到它预先添加了 xml 声明,因此很容易复制并删除它:

      private static String asString(GPathResult node) {
          try {
              Object builder = Class.forName("groovy.xml.StreamingMarkupBuilder").newInstance();
              InvokerHelper.setProperty(builder, "encoding", "UTF-8");
              Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
              return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
          } catch (Exception e) {
              return "Couldn't convert node to string because: " + e.getMessage();
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用 XmlNodePrinter 并传递一个自定义编写器,因此它不是打印输出而是打印到一个字符串:

        public static String convert(Node xml) {
            StringWriter stringWriter = new StringWriter()
            XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(stringWriter))
            nodePrinter.print(xml)
            return stringWriter.toString()
        }
        

        【讨论】:

          【解决方案4】:

          您仍然可以使用 XmlSlurper,而不是使用序列化它并替换第一个 replaceFirst

           def oSalesOrderCollection = new XmlSlurper(false,false).parse(xas)     
                      def xml = XmlUtil.serialize(oSalesOrderSOAPMarkup).replaceFirst("<\\?xml version=\"1.0\".*\\?>", "");
                     //Test the output or just print it 
                     File outFile = new File("aes.txt")
                     Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF8"));
                              out.append(xml.toString())
                              out.flush()
                              out.close()
          

          GroovyCore Snip

            /**
             * Transforms the element to its text equivalent.
             * (The resulting string does not contain a xml declaration. Use {@code XmlUtil.serialize(element)} if you need the declaration.)
             *
             * @param element the element to serialize
             * @return the string representation of the element
             * @since 2.1
             */
            public static String serialize(Element element) {
              return XmlUtil.serialize(element).replaceFirst("<\\?xml version=\"1.0\".*\\?>", "");
            }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多