GPathResult to String without XML declaration
Asked Answered
G

4

8

I'm converting GPathResult to String using

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

It works fine, but I'm getting XML declaration in front of my XML

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

How can I convert GPathResult to String without <?xml version="1.0" encoding="UTF-8"?> at the beginning?

Gadroon answered 12/8, 2016 at 12:22 Comment(0)
N
9

Use XmlParser instead of XmlSlurper:

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

Using new XmlNodePrinter(preserveWhitespace: true) may be your friend for what you're trying to do also. See the rest of the options in the docs: http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html.

Norris answered 14/8, 2016 at 4:9 Comment(0)
V
1

This is the code in the XmlUtil class. You'll notice it prepends the xml declaration so it's easy enough to just copy this and remove it:

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();
    }

}
Versicular answered 19/4, 2017 at 15:30 Comment(0)
D
1

You can still use the XmlSlurper than use the serialize it and replace first 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\".*\\?>", "");
  }
}
Denti answered 9/11, 2021 at 3:28 Comment(0)
F
0

You can use XmlNodePrinter and pass a custom writer, so instead of it print to output it will print to a string:

public static String convert(Node xml) {
    StringWriter stringWriter = new StringWriter()
    XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(stringWriter))
    nodePrinter.print(xml)
    return stringWriter.toString()
}
Forte answered 21/1, 2020 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.