0

I have an XML request in the form of a file, that I want to add to a taurus yaml file. The yaml file requires a single line string.

I have it working with a json file by doing this;

    strMessage=getFileContents("request.json");
    //Use a jsonslurper to process the message
    def jsonSlurper = new groovy.json.JsonSlurper()
    def mapMessage = jsonSlurper.parseText(strMessage)
    def jsonMessage= groovy.json.JsonOutput.toJson(mapMessage)
    request = jsonMessage

So I'm trying the same process with the xml equivalents;

        strMessage=getFileContents("request.xml")
        //Use an xmlslurper to process the message
        def xmlSlurper = new groovy.util.XmlSlurper()
        def mapMessage = xmlSlurper.parseText(strMessage)
        def xmlMessage = groovy.util.XmlUtil.serialize(mapMessage)
        request = xmlMessage

Unfortunately while the json output comes out all on one line, the xml version comes out similar to the file with lots of line breaks.

The reason why I'm using slurpers is to reformat the files as I don't have control over the source files. It did occur to me that I could just ingest the file and remove the line breaks but the request in the yaml file would have huge amounts of whitespace. And obviously I can't remove line breaks and whitespace because some whitespace is valid.

Up to the 'mapMessage' it is working as expected, it's just the last bit to convert the map into a single XML string that has me beat. what command will output a Groovy map object to a single line XML?

Slimy43
  • 341
  • 4
  • 17
  • Do you need to use xmlSlurper ? You could just handle it as text. `println new File('request.xml').readLines().join('')` This does however preserve indentation whitespace if the file is formatted. – ou_ryperd Mar 05 '20 at 06:58
  • I tried that, but the XML files I'm getting in are 'poor quality' to put it mildly. The json and xml utilities do a great job to format and validate the files, it's just the last step to get it back out that I was having issues with. – Slimy43 Mar 05 '20 at 08:35
  • try `println XmlUtil.asString(new XmlSlurper().parseText(new File('request.xml').text)).replace("\n", '')` – ou_ryperd Mar 05 '20 at 09:10

1 Answers1

1

no single line of code, but possible to do with native groovy classes and XmlParser (not XmlSlurper)

Note: data inside xml could contain line breaks and the following code will keep them

def strMessage='''
<a>
    <b>123</b>
    <c>the message</c>
    <d>multiline
message</d>
</a>
'''

def xmlWritable(groovy.util.Node n, boolean indent, boolean xmlDeclaration=true){
    return new Writable(){
        Writer writeTo(Writer w){
            if(xmlDeclaration){
                w << '<?xml version="1.0" encoding="UTF-8"?>'
                w << ( indent ? '\n' : '' )
            }
            IndentPrinter p = new IndentPrinter(w,indent?"  ":"",indent)
            XmlNodePrinter nodePrinter = new XmlNodePrinter(p)
            nodePrinter.setPreserveWhitespace(true)
            nodePrinter.print(n)
            return w
        }
        String toString(){
            writeTo(new StringWriter()).toString()
        }
    }
}


def xmlParser = new XmlParser()
def mapMessage = xmlParser.parseText(strMessage)
def xmlMessage = xmlWritable(mapMessage,false,false).toString()

result:

<a><b>123</b><c>the message</c><d>multiline
message</d></a>

PS:

daggett
  • 26,404
  • 3
  • 40
  • 56
  • That works for the single line, but it excludes the XML declaration. What needs to be updated to include the declaration (it is present in the original file, but not in the output). Given that the declaration for this work is pretty much always the same, I could manually add it, but I'd like to have it picked up by code as a preference. – Slimy43 Mar 05 '20 at 09:09
  • Unfortunately in groovy it's done in a not nice way. Just check the code used in XmlUtil.java for GPathResult: https://github.com/apache/groovy/blob/d6226958378206224b8e1adf72cdeea3f42a6957/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java#L408 Both groovy xml representations finally using standard javax-xml transformer to make xml re-formatting: https://github.com/apache/groovy/blob/d6226958378206224b8e1adf72cdeea3f42a6957/subprojects/groovy-xml/src/main/java/groovy/xml/XmlUtil.java#L452 . Let me add an update to answer... – daggett Mar 05 '20 at 15:09