How to parse XML using User Defined Function

In the previous post, we have seen parsing JSON value using User Defined Function..

Let’s check how to parse XML using User Defined Function.

Example

  • Use parseXML function as a Groovy function to parse an XML and store the values in an object.
  • The object returned from the parseXML is a JsonNode. (from JsonNode (jackson-databind 2.7.0 API))
  • Use any of the methods in the above docs on the object to extract or manipulate the JSON before returning it.
  • parseStudentXML function below parses the student data in an XML format.
type Query {
    parseStudentXML(value: String): Json  @tan(type:Groovy, inline: """return parseXML(value)""")
}

Let’s execute the function!

{
  parseStudentXML(
    value: "<student><name>Hypi</name><platform>low code</platform><url>https://hypi.io/</url><body>Build App</body></student>"
  )
}
#result
{
  "data": {
    "parseStudentXML": {
      "name": "Hypi",
      "platform": "low code",
      "url": "https://hypi.io/",
      "body": "Build App"
    }
  }
}