apoc.xml.parse

Details

Syntax

apoc.xml.parse(data [, path, config, simple ])

Description

Parses the given XML STRING as a MAP.

Arguments

Name

Type

Description

data

STRING

The XML data to parse.

path

STRING

An xPath expression. The default is: /.

config

MAP

A config map describing whether or not to fail on an encountered error; { failOnError = true :: BOOLEAN } The default is: {}.

simple

BOOLEAN

Specify simple mode to make XML elements accessible via a property of the element name prefixed with an _. The default is: false.

Returns

MAP

Example

The following query parses the XML and uses an XPath expression to select a specific book, then extracts its type, attribute, and child element text values:

WITH
  '<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Matthew Gambardella</author>
  <title>XML Developer Guide</title>
  <price>44.95</price>
</book>
<book id="bk102">
  <author>Kim Ralls</author>
  <title>Midnight Rain</title>
  <price>5.95</price>
</book>
</catalog>' AS xmlData
WITH apoc.xml.parse(xmlData, '/catalog/book[@id="bk101"]/.', {}, false) AS book
RETURN
  book._type AS type,
  book.id AS id,
  [child IN book._children | child._text] AS values
Results
type id values

"book"

"bk101"

["Matthew Gambardella", "XML Developer Guide", "44.95"]

apoc.xml.parse converts the selected XML element into a nested MAP using these conventions:

  • _type - the element name

  • _children - a list of child element maps

  • _text - the text content of an element

  • XML attributes become top-level keys on the element map (here id)

Omitting the path argument (or passing '/') selects the root element and returns the full document as a nested map. Setting simpleMode to true stores child elements under a _<parentElementName> key instead of _children. XML input containing a DOCTYPE declaration is rejected for security reasons.