Skip to content

Xml

Purpose

Since 6.10.7

Provides methods for manipulating XML.

Methods

Binding name: p6.xml

escape

Escape the given XML string.

Syntax

String p6.xml.escape(String xmlStr)
Example
final def xml = p6.xml.escape '<root><node>value</node></root>'
assert xml == '&lt;root&gt;&lt;node&gt;value&lt;/node&gt;&lt;/root&gt;'

unescape

Unescape the given escaped XML string.

Syntax

String p6.xml.unescape(String escStr)
Example
final def xml = p6.xml.unescape('&lt;root&gt;&lt;node&gt;value&lt;/node&gt;&lt;/root&gt;')
assert xml == '<root><node>value</node></root>'

getXPath

Provide a javax.xml.xpath.XPath using the Saxon factory.

Syntax

XPath p6.xml.getXPath()
Example
def xpath = p6.xml.getXPath()

fromJson

Convert a json string into xml. (identical to toXml in json dsl)

If no root node is in the json, a node named ‘root’ will be added.

Syntax

Object p6.xml.fromJson(String json)
Example
final def json = """
       {
       "node1": "xyz",
       "node2": "abc"
       }
       """

final def xml = p6.xml.fromJson json
final def validXml = new XmlSlurper().parseText '<root><node1>xyz</node1><node2>abc</node2></root>'
assert xml == validXml

toJson

Convert xml string to json. (identical to fromXml in json dsl)

Syntax

Object p6.xml.toJson(String xml)
Example
final def xml = "<root><node1>xyz</node1><node2>abc</node2></root>"

final def json = p6.xml.toJson xml
final def validJson = [node1: 'xyz', node2: 'abc']
assert json == validJson