GPathResult to String without XML declaration

groovy xmlslurper iterate nodes
groovy xmlslurper attribute value
groovy xml parser find node
jenkins xmlslurper
groovy.xml.xmlutil.serialize examples
groovy xml to json
groovy xml to map

I'm converting GPathResult to String using

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

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?

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:

GPathResult to String without XML declaration - xml - html, I'm converting GPathResult to String using def gPathResult = new XmlSlurper(). parseText('<node/>') XmlUtil.serialize(gPathResult) It works fine, but I'm getting� Produce an XML declaration in the output. (In this case we only use the local part without any namespace). GPathResult, String… ) Getting a node as a string.

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


Convert GPathResult to String without tag0, In Groovy, you can use the follow code to convert node to XML String.1groovy. xml.XmlUtil.serialize(GPathResult node)But sometimes the XML� 9 GPathResult to String without XML declaration Aug 14 '16. 6 Sorting ArrayList of Maps based on a map value Aug 14 '15. 6 Does Spock have Test Event Listeners Aug 14

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))
    return stringWriter.toString()

Processing XML, It parses a XML String and recursively converts it to a list or map of objects. XmlSlurper returns GPathResult instances when parsing XML If you just have to read a few nodes XmlSlurper should be your choice, since it will not have to create a complete structure in memory" Produce an XML declaration in the output. Note that the root category is products // (plural), but the GPathResult you get from XmlSlurper assumes you're already // in the root category. That's why it's not productsCategories.products.product.each productsCategories.product.each { // Note you must put the id in a String (Groovy style shown here) // in order to have a String key.

XmlUtil (Groovy 3.0.5), Return a pretty String version of the XML content produced by the Writable. static void Write a pretty version of the GPathResult to the OutputStream. The created SAXParser will be namespace-aware and not validate against DTDs. At work we use .Net XML Serialization pretty extensively. The simplest case of XmlSerializer.Serialize produces some ugly XML. I mean, XML with a declaration, some useless w3c xmlns attributes and headaches if you don't intend for your object to be an entire well formed XML doc by itself after serialization.

groovy.xml.XmlUtil.serialize java code examples, (The resulting string does not contain a xml declaration. @param node a GPathResult to serialize to a String * @return the pretty String representation of the� schemaLanguage - the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants) namespaceAware - will the parser be namespace aware validating - will the parser also validate against DTDs schema - a URL pointing to the schema to validate against Returns: the created SAXParser Throws: SAXException

Groovy Goodness: Pretty Print XML, Pretty print a non-formatted XML String. Pretty print a GPathResult. For example if we use XmlParser to parse XML we get a Node object. doesn't put a new line after the heading declaration. Goodness: Customizing JSON Output � Groovy Goodness: Remove Last Item From List Using RemoveLast� So I used XmlTextReader for some webservices and on others I serialized the whole Xml in a string and removed the unused namespaces by using String.Replace. Not the best thing ever, but it has proved to be good also. – rodrigogq Aug 14 '14 at 16:38