加载 XML
许多现有的企业应用、端点和文件使用 XML 作为数据交换格式。Load XML 过程允许我们处理这些文件。
过程和函数概述
下表描述了可用的过程和函数
| 限定名称 | 类型 |
|---|---|
apoc.load.xml |
|
apoc.xml.parse |
|
apoc.import.xml |
|
apoc.load.xml
此过程获取文件或 HTTP URL 并将 XML 解析为 Map 数据结构。
| 签名 |
|---|
|
Map 的创建遵循以下规则
-
在简单模式(simple mode)下,每种类型的子元素在父级 Map 中都有自己的条目。
-
作为键的元素类型会加上
_前缀,以防止与属性冲突。 -
如果只有一个元素,条目将直接包含该元素作为值,而不是集合。
-
如果存在多个元素,则会生成一个值列表。
-
每个子元素仍保留其
_type字段以进行区分。
此过程支持以下配置参数
| 名称 (name) | type | 默认 | description(描述) |
|---|---|---|---|
failOnError |
布尔值 (BOOLEAN) |
true |
解析 XML 时如果遇到错误则失败 |
headers |
MAP |
{} |
查询 XML 文档时使用的 HTTP 请求头 |
binary |
|
|
如果不为空,则允许接收二进制数据,而不是将文件名/URL 作为第一个参数。类似于 二进制文件示例 |
charset |
java.nio.charset.Charset |
|
可选的字符集,当 |
apoc.xml.parse
如果我们的数据集包含以 XML 作为属性值的节点,它们可以使用 apoc.xml.parse 函数解析为 Map。
| 签名 |
|---|
|
此函数支持以下配置参数
| 名称 (name) | type | 默认 | description(描述) |
|---|---|---|---|
failOnError |
布尔值 (BOOLEAN) |
true |
解析 XML 时如果遇到错误则失败 |
WITH '<?xml version="1.0"?><table><tr><td><img src="pix/logo-tl.gif"></img></td></tr></table>' AS xmlString
RETURN apoc.xml.parse(xmlString) AS value
| 值 |
|---|
{_type: "table", _children: [{_type: "tr", _children: [{_type: "td", _children: [{_type: "img", src: "pix/logo-tl.gif"}]}]}]} |
apoc.import.xml
如果我们不想在创建图结构之前对 XML 进行任何转换,可以使用 apoc.import.xml 过程将 XML 1:1 映射到图中。
| 签名 |
|---|
|
此过程将返回一个代表 XML 文档的节点,其中包含映射到 XML 结构的节点和关系。
应用以下映射规则
| xml | 标签 (label) | 属性 |
|---|---|---|
文档 |
XmlDocument |
_xmlVersion, _xmlEncoding |
处理指令 |
XmlProcessingInstruction |
_piData, _piTarget |
元素/标签 |
XmlTag |
_name |
属性 |
不适用 |
XmlTag 节点中的属性 |
文本 |
XmlWord |
为每个单词创建一个单独的节点 |
XML 文档的节点按以下方式连接
| 关系类型 | description(描述) |
|---|---|
:IS_CHILD_OF |
指向嵌套的 XML 元素 |
:FIRST_CHILD_OF |
指向第一个子元素 |
:NEXT_SIBLING |
指向同一嵌套层级的下一个 XML 元素 |
:NEXT |
在整个文档中生成线性链 |
:NEXT_WORD |
仅当配置 Map 具有 |
此过程支持以下配置参数
| 配置选项 | 默认值 | description(描述) |
|---|---|---|
connectCharacters |
false |
如果为 |
filterLeadingWhitespace |
false |
如果为 |
delimiter |
|
如果提供,使用该分隔符将文本元素拆分为单独的节点 |
标签 (label) |
XmlCharacter |
用于文本元素表示的标签 |
relType |
|
用于将文本元素连接成一个链表的关系类型 |
charactersForTag |
{} |
标签名 → 字符串的 Map。对于给定的标签名,会添加一个额外的文本元素,并将值包含为 |
从文件导入
默认情况下,禁止从文件系统导入。我们可以通过在 apoc.conf 中设置以下属性来启用它
apoc.import.file.enabled=true
如果我们尝试在未先设置此属性的情况下使用任何导入过程,将会收到以下错误消息
Failed to invoke procedure: Caused by: java.lang.RuntimeException: Import from files not enabled, please set apoc.import.file.enabled=true in your apoc.conf |
导入文件从 import 目录读取,该目录由 server.directories.import 属性定义。这意味着我们提供的任何文件路径都是相对于此目录的。如果我们尝试从绝对路径(例如 /tmp/filename)读取,将会收到类似于以下的错误消息
Failed to invoke procedure: Caused by: java.lang.RuntimeException: Can’t read url or key file:/path/to/neo4j/import/tmp/filename as json: /path/to/neo4j//import/tmp/filename (No such file or directory) |
我们可以通过在 apoc.conf 中设置以下属性来启用从文件系统任意位置读取文件
apoc.import.file.use_neo4j_config=false
|
Neo4j 现在可以从文件系统的任何位置读取,因此在设置此属性之前,请确保这是您的意图。 |
示例
本节中的示例基于 Microsoft 的 book.xml 文件。
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
...
该文件可从 GitHub 下载。
从本地文件导入
下文描述的 books.xml 文件包含 Microsoft Books XML 文件中的前两本书。我们将在本节中使用该较小的文件来简化示例。
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<author>Arciniegas, Fabio</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
我们将此文件放入 Neo4j 实例的 import 目录中。现在让我们编写一个使用 apoc.load.xml 过程的查询来探索该文件。
books.xml 并将内容作为 Cypher 数据结构返回CALL apoc.load.xml("file:///books.xml")
YIELD value
RETURN value
| 值 |
|---|
{_type: "catalog", _children: [{_type: "book", _children: [{_type: "author", _text: "Gambardella, Matthew"}, {_type: "author", _text: "Arciniegas, Fabio"}, {_type: "title", _text: "XML Developer’s Guide"}, {_type: "genre", _text: "Computer"}, {_type: "price", _text: "44.95"}, {_type: "publish_date", _text: "2000-10-01"}, {_type: "description", _text: "An in-depth look at creating applications with XML."}], id: "bk101"}, {_type: "book", _children: [{_type: "author", _text: "Ralls, Kim"}, {_type: "title", _text: "Midnight Rain"}, {_type: "genre", _text: "Fantasy"}, {_type: "price", _text: "5.95"}, {_type: "publish_date", _text: "2000-12-16"}, {_type: "description", _text: "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."}], id: "bk102"}]} |
我们得到了一个代表 XML 结构的 Map。每当 XML 元素嵌套在另一个元素中时,都可以通过 .children 属性访问它。我们可以编写以下查询来更好地理解我们的文件包含什么。
book.xml 并解析结果以提取标题、描述、类型和作者CALL apoc.load.xml("file:///books.xml")
YIELD value
UNWIND value._children AS book
RETURN book.id AS bookId,
[item in book._children WHERE item._type = "title"][0] AS title,
[item in book._children WHERE item._type = "description"][0] AS description,
[item in book._children WHERE item._type = "author"] AS authors,
[item in book._children WHERE item._type = "genre"][0] AS genre;
| bookId | 标题 | description(描述) | 作者 | 类型 |
|---|---|---|---|---|
"bk101" |
{_type: "title", _text: "XML Developer’s Guide"} |
{_type: "description", _text: "An in-depth look at creating applications with XML."} |
[{_type: "author", _text: "Gambardella, Matthew"}, {_type: "author", _text: "Arciniegas, Fabio"}] |
{_type: "genre", _text: "Computer"} |
"bk102" |
{_type: "title", _text: "Midnight Rain"} |
{_type: "description", _text: "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."} |
[{_type: "author", _text: "Ralls, Kim"}] |
{_type: "genre", _text: "Fantasy"} |
现在让我们创建一个包含书籍及其元数据、作者和类型的图。
book.xml 并解析结果以提取标题、描述、类型和作者CALL apoc.load.xml("file:///books.xml")
YIELD value
UNWIND value._children AS book
WITH book.id AS bookId,
[item in book._children WHERE item._type = "title"][0] AS title,
[item in book._children WHERE item._type = "description"][0] AS description,
[item in book._children WHERE item._type = "author"] AS authors,
[item in book._children WHERE item._type = "genre"][0] AS genre
MERGE (b:Book {id: bookId})
SET b.title = title._text, b.description = description._text
MERGE (g:Genre {name: genre._text})
MERGE (b)-[:HAS_GENRE]->(g)
WITH b, authors
UNWIND authors AS author
MERGE (a:Author {name:author._text})
MERGE (a)-[:WROTE]->(b);
下方的 Neo4j Browser 可视化显示了导入的图
您可以使用 failOnError 配置来处理错误 URL 或 XML 的结果。例如,借助 apoc.when 过程,您可以在 URL 错误时返回 nothingToDo 作为结果
CALL apoc.load.xml("MY_XML_URL", '', {failOnError:false})
YIELD value
WITH value as valueXml
call apoc.do.when(valueXml["_type"] is null, "return 'nothingToDo' as result", "return valueXml as result", {valueXml: valueXml})
YIELD value
UNWIND value["result"] as result
RETURN result
从 GitHub 导入
我们也可以处理来自 HTTP 或 HTTPS URI 的 XML 文件。让我们从处理托管在 GitHub 上的 books.xml 文件开始。
这次我们将 true 作为过程的第 4 个参数传入。这意味着 XML 将以简单模式(simple mode)进行解析。
WITH "https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml" AS uri
CALL apoc.load.xml(uri, '', {}, true)
YIELD value
RETURN value;
| 值 |
|---|
{_type: "catalog", _catalog: [{_type: "book", _book: [{_type: "author", _text: "Gambardella, Matthew"}, {_type: "author", _text: "Arciniegas, Fabio"}, {_type: "title", _text: "XML Developer’s Guide"}, {_type: "genre", _text: "Computer"}, {_type: "price", _text: "44.95"}, {_type: "publish_date", _text: "2000-10-01"}, {_type: "description", _text: "An in-depth look at creating applications with XML."}], id: "bk101"}, {_type: "book", _book: [{_type: "author", _text: "Ralls, Kim"}, {_type: "title", _text: "Midnight Rain"}, {_type: "genre", _text: "Fantasy"}, {_type: "price", _text: "5.95"}, {_type: "publish_date", _text: "2000-12-16"}, {_type: "description", _text: "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."}], id: "bk102"}, {_type: "book", _book: [{_type: "author", _text: "Corets, Eva"}, {_type: "title", _text: "Maeve Ascendant"}, {_type: "genre", _text: "Fantasy"}, {_type: "price", _text: "5.95"}, {_type: "publish_date", _text: "2000-11-17"}, {_type: "description", _text: "After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society."}], id: "bk103"}, {_type: "book", _book: [{_type: "author", _text: "Corets, Eva"}, {_type: "title", _text: "Oberon’s Legacy"}, {_type: "genre", _text: "Fantasy"}, {_type: "price", _text: "5.95"}, {_type: "publish_date", _text: "2001-03-10"}, {_type: "description", _text: "In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant."}], id: "bk104"}, {_type: "book", _book: [{_type: "author", _text: "Corets, Eva"}, {_type: "title", _text: "The Sundered Grail"}, {_type: "genre", _text: "Fantasy"}, {_type: "price", _text: "5.95"}, {_type: "publish_date", _text: "2001-09-10"}, {_type: "description", _text: "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon’s Legacy."}], id: "bk105"}, {_type: "book", _book: [{_type: "author", _text: "Randall, Cynthia"}, {_type: "title", _text: "Lover Birds"}, {_type: "genre", _text: "Romance"}, {_type: "price", _text: "4.95"}, {_type: "publish_date", _text: "2000-09-02"}, {_type: "description", _text: "When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled."}], id: "bk106"}, {_type: "book", _book: [{_type: "author", _text: "Thurman, Paula"}, {_type: "title", _text: "Splish Splash"}, {_type: "genre", _text: "Romance"}, {_type: "price", _text: "4.95"}, {_type: "publish_date", _text: "2000-11-02"}, {_type: "description", _text: "A deep sea diver finds true love twenty thousand leagues beneath the sea."}], id: "bk107"}, {_type: "book", _book: [{_type: "author", _text: "Knorr, Stefan"}, {_type: "title", _text: "Creepy Crawlies"}, {_type: "genre", _text: "Horror"}, {_type: "price", _text: "4.95"}, {_type: "publish_date", _text: "2000-12-06"}, {_type: "description", _text: "An anthology of horror stories about roaches, centipedes, scorpions and other insects."}], id: "bk108"}, {_type: "book", _book: [{_type: "author", _text: "Kress, Peter"}, {_type: "title", _text: "Paradox Lost"}, {_type: "genre", _text: "Science Fiction"}, {_type: "price", _text: "6.95"}, {_type: "publish_date", _text: "2000-11-02"}, {_type: "description", _text: "After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum."}], id: "bk109"}, {_type: "book", _book: [{_type: "author", _text: "O’Brien, Tim"}, {_type: "title", _text: "Microsoft .NET: The Programming Bible"}, {_type: "genre", _text: "Computer"}, {_type: "price", _text: "36.95"}, {_type: "publish_date", _text: "2000-12-09"}, {_type: "description", _text: "Microsoft’s .NET initiative is explored in detail in this deep programmer’s reference."}], id: "bk110"}, {_type: "book", _book: [{_type: "author", _text: "O’Brien, Tim"}, {_type: "title", _text: "MSXML3: A Comprehensive Guide"}, {_type: "genre", _text: "Computer"}, {_type: "price", _text: "36.95"}, {_type: "publish_date", _text: "2000-12-01"}, {_type: "description", _text: "The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more."}], id: "bk111"}, {_type: "book", _book: [{_type: "author", _text: "Galos, Mike"}, {_type: "title", _text: "Visual Studio 7: A Comprehensive Guide"}, {_type: "genre", _text: "Computer"}, {_type: "price", _text: "49.95"}, {_type: "publish_date", _text: "2001-04-16"}, {_type: "description", _text: "Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C+, C#, and ASP are integrated into a comprehensive development environment."}], id: "bk112"}]} |
我们再次得到了一个代表 XML 结构的 Map,但其结构与不使用简单模式时不同。这一次,嵌套的 XML 元素可以通过以 _ 为前缀的元素名称属性来访问。
我们可以编写以下查询来更好地理解我们的文件包含什么。
book.xml 并解析结果以提取标题、描述、类型和作者WITH "https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml" AS uri
CALL apoc.load.xml(uri, '', {}, true)
YIELD value
UNWIND value._catalog AS catalog
RETURN catalog.id AS bookId,
[item in catalog._book WHERE item._type = "title"][0] AS title,
[item in catalog._book WHERE item._type = "description"][0] AS description,
[item in catalog._book WHERE item._type = "author"] AS authors,
[item in catalog._book WHERE item._type = "genre"][0] AS genre;
| bookId | 标题 | description(描述) | 作者 | 类型 |
|---|---|---|---|---|
"bk101" |
{_type: "title", _text: "XML Developer’s Guide"} |
{_type: "description", _text: "An in-depth look at creating applications with XML."} |
[{_type: "author", _text: "Gambardella, Matthew"}, {_type: "author", _text: "Arciniegas, Fabio"}] |
{_type: "genre", _text: "Computer"} |
"bk102" |
{_type: "title", _text: "Midnight Rain"} |
{_type: "description", _text: "A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world."} |
[{_type: "author", _text: "Ralls, Kim"}] |
{_type: "genre", _text: "Fantasy"} |
"bk103" |
{_type: "title", _text: "Maeve Ascendant"} |
{_type: "description", _text: "After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society."} |
[{_type: "author", _text: "Corets, Eva"}] |
{_type: "genre", _text: "Fantasy"} |
"bk104" |
{_type: "title", _text: "Oberon’s Legacy"} |
{_type: "description", _text: "In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant."} |
[{_type: "author", _text: "Corets, Eva"}] |
{_type: "genre", _text: "Fantasy"} |
"bk105" |
{_type: "title", _text: "The Sundered Grail"} |
{_type: "description", _text: "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon’s Legacy."} |
[{_type: "author", _text: "Corets, Eva"}] |
{_type: "genre", _text: "Fantasy"} |
"bk106" |
{_type: "title", _text: "Lover Birds"} |
{_type: "description", _text: "When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled."} |
[{_type: "author", _text: "Randall, Cynthia"}] |
{_type: "genre", _text: "Romance"} |
"bk107" |
{_type: "title", _text: "Splish Splash"} |
{_type: "description", _text: "A deep sea diver finds true love twenty thousand leagues beneath the sea."} |
[{_type: "author", _text: "Thurman, Paula"}] |
{_type: "genre", _text: "Romance"} |
"bk108" |
{_type: "title", _text: "Creepy Crawlies"} |
{_type: "description", _text: "An anthology of horror stories about roaches, centipedes, scorpions and other insects."} |
[{_type: "author", _text: "Knorr, Stefan"}] |
{_type: "genre", _text: "Horror"} |
"bk109" |
{_type: "title", _text: "Paradox Lost"} |
{_type: "description", _text: "After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum."} |
[{_type: "author", _text: "Kress, Peter"}] |
{_type: "genre", _text: "Science Fiction"} |
"bk110" |
{_type: "title", _text: "Microsoft .NET: The Programming Bible"} |
{_type: "description", _text: "Microsoft’s .NET initiative is explored in detail in this deep programmer’s reference."} |
[{_type: "author", _text: "O’Brien, Tim"}] |
{_type: "genre", _text: "Computer"} |
"bk111" |
{_type: "title", _text: "MSXML3: A Comprehensive Guide"} |
{_type: "description", _text: "The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more."} |
[{_type: "author", _text: "O’Brien, Tim"}] |
{_type: "genre", _text: "Computer"} |
"bk112" |
{_type: "title", _text: "Visual Studio 7: A Comprehensive Guide"} |
{_type: "description", _text: "Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C+, C#, and ASP are integrated into a comprehensive development environment."} |
[{_type: "author", _text: "Galos, Mike"}] |
{_type: "genre", _text: "Computer"} |
与其仅仅返回数据,我们还可以创建一个包含书籍及其元数据、作者和类型的图。
book.xml 并解析结果以提取标题、描述、类型和作者WITH "https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml" AS uri
CALL apoc.load.xml(uri, '', {}, true)
YIELD value
UNWIND value._catalog AS catalog
WITH catalog.id AS bookId,
[item in catalog._book WHERE item._type = "title"][0] AS title,
[item in catalog._book WHERE item._type = "description"][0] AS description,
[item in catalog._book WHERE item._type = "author"] AS authors,
[item in catalog._book WHERE item._type = "genre"][0] AS genre
MERGE (b:Book {id: bookId})
SET b.title = title._text, b.description = description._text
MERGE (g:Genre {name: genre._text})
MERGE (b)-[:HAS_GENRE]->(g)
WITH b, authors
UNWIND authors AS author
MERGE (a:Author {name:author._text})
MERGE (a)-[:WROTE]->(b);
下方的 Neo4j Browser 可视化显示了导入的图
xPath 表达式
我们还可以提供 xPath 表达式来从 XML 文档中选择节点。如果我们只想返回 Computer 类型的书,可以编写以下查询
CALL apoc.load.xml(
"https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml",
'/catalog/book[genre=\"Computer\"]'
)
YIELD value as book
WITH book.id as id, [attr IN book._children WHERE attr._type IN ['title','price'] | attr._text] as pairs
RETURN id, pairs[0] as title, pairs[1] as price;
| id | 标题 | 价格 |
|---|---|---|
"bk101" |
"XML Developer’s Guide" |
"44.95" |
"bk110" |
"Microsoft .NET: The Programming Bible" |
"36.95" |
"bk111" |
"MSXML3: A Comprehensive Guide" |
"36.95" |
"bk112" |
"Visual Studio 7: A Comprehensive Guide" |
"49.95" |
在这种情况下,我们只返回 id、title 和 price,但我们可以返回任何其他元素
我们也可以只返回一个特定的元素。例如,以下查询返回 id = bk102 的书的 author
CALL apoc.load.xml(
'https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml',
'/catalog/book[@id="bk102"]/author'
)
YIELD value as result
WITH result._text as author
RETURN author;
| 作者 |
|---|
"Ralls, Kim" |
提取数据结构
我们可以使用 apoc.map.fromPairs 函数将值转换为 Map。
call apoc.load.xml("https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml")
yield value as catalog
UNWIND catalog._children as book
WITH book.id as id, [attr IN book._children WHERE attr._type IN ['author','title'] | [attr._type, attr._text]] as pairs
WITH id, apoc.map.fromPairs(pairs) AS value
RETURN id, value
| id | 值 |
|---|---|
"bk101" |
{title: "XML Developer’s Guide", author: "Arciniegas, Fabio"} |
"bk102" |
{title: "Midnight Rain", author: "Ralls, Kim"} |
"bk103" |
{title: "Maeve Ascendant", author: "Corets, Eva"} |
"bk104" |
{title: "Oberon’s Legacy", author: "Corets, Eva"} |
"bk105" |
{title: "The Sundered Grail", author: "Corets, Eva"} |
"bk106" |
{title: "Lover Birds", author: "Randall, Cynthia"} |
"bk107" |
{title: "Splish Splash", author: "Thurman, Paula"} |
"bk108" |
{title: "Creepy Crawlies", author: "Knorr, Stefan"} |
"bk109" |
{title: "Paradox Lost", author: "Kress, Peter"} |
"bk110" |
{title: "Microsoft .NET: The Programming Bible", author: "O’Brien, Tim"} |
"bk111" |
{title: "MSXML3: A Comprehensive Guide", author: "O’Brien, Tim"} |
"bk112" |
{title: "Visual Studio 7: A Comprehensive Guide", author: "Galos, Mike"} |
现在我们可以清晰地从 Map 中访问属性。
call apoc.load.xml("https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml")
yield value as catalog
UNWIND catalog._children as book
WITH book.id as id, [attr IN book._children WHERE attr._type IN ['author','title'] | [attr._type, attr._text]] as pairs
WITH id, apoc.map.fromPairs(pairs) AS value
RETURN id, value.title, value.author
| id | value.title | value.author |
|---|---|---|
"bk101" |
"XML Developer’s Guide" |
"Arciniegas, Fabio" |
"bk102" |
"Midnight Rain" |
"Ralls, Kim" |
"bk103" |
"Maeve Ascendant" |
"Corets, Eva" |
"bk104" |
"Oberon’s Legacy" |
"Corets, Eva" |
"bk105" |
"The Sundered Grail" |
"Corets, Eva" |
"bk106" |
"Lover Birds" |
"Randall, Cynthia" |
"bk107" |
"Splish Splash" |
"Thurman, Paula" |
"bk108" |
"Creepy Crawlies" |
"Knorr, Stefan" |
"bk109" |
"Paradox Lost" |
"Kress, Peter" |
"bk110" |
"Microsoft .NET: The Programming Bible" |
"O’Brien, Tim" |
"bk111" |
"MSXML3: A Comprehensive Guide" |
"O’Brien, Tim" |
"bk112" |
"Visual Studio 7: A Comprehensive Guide" |
"Galos, Mike" |
直接导入 XML
我们可以编写以下查询来为 Microsoft 书籍 XML 文件创建一个图结构。
books.xml 的内容创建了一个图结构CALL apoc.import.xml(
"https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml",
{relType:'NEXT_WORD', label:'XmlWord'}
)
YIELD node
RETURN node;
| 节点 |
|---|
(:XmlDocument {_xmlVersion: "1.0", _xmlEncoding: "UTF-8", url: "https://raw.githubusercontent.com/neo4j/apoc/2026.03/core/src/test/resources/xml/books.xml"}) |
下方的 Neo4j Browser 可视化显示了导入的图