How to merge two .xml files in Gradle Kotlin?

I have following task written in Groovy in Gradle that merges order.xml file into inventory.xml as shown in the code below, how can I accomplish the same in Kotlin?

initial inventory.xml

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>GM</car>
    <car>Ford</car>
</cars>

order.xml

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>Acura</car>
    <car>Honda</car>
</cars>
// How to convert following code into Kotlin?
task mergeXml {
    final inventory = new File("$rootDir/src/main/resources/inventory.xml")
    final inventoryContent = inventory.getText()
    final order = new File("$rootDir/src/main/resources/order.xml")
    final orderContent = order.getText()

    doLast {
        def rootNode = new XmlParser().parseText(inventoryContent)
        def printWriter = new PrintWriter(inventory)
        def xmlNodePrinter = new XmlNodePrinter(printWriter)

        new XmlParser().parseText(orderContent).children().each { rootNode.append(it) }
        printWriter.print(inventoryContent.split("(?=<cars)")[0])
        xmlNodePrinter.print(rootNode)
    }
}

Expected inventory.xml after processing

<?xml version="1.0" encoding="UTF-8"?>
<cars>
  <car>
    GM
  </car>
  <car>
    Ford
  </car>
  <car>
    Acura
  </car>
  <car>
    Honda
  </car>
</cars>

Just in case, someone is looking for the answer, it’s at https://stackoverflow.com/questions/56229254/how-to-merge-two-xml-files-in-gradle-kotlin/56247382#56247382