apoc.import.xml

详细信息

语法

apoc.import.xml(urlOrBinary [, config ]) :: (node)

描述

从提供的 XML 文件导入图数据。

输入参数

名称

类型

描述

urlOrBinary

ANY

用于导入数据的文件名或二进制数据。

config

MAP

{ connectCharacters = false :: BOOLEAN, filterLeadingWhitespace = false :: BOOLEAN, delimiter = " " :: STRING, label :: STRING, relType :: STRING, charactersForTag :: MAP }。默认值为:{}

返回参数

名称

类型

描述

节点

NODE

导入的节点。

从文件读取

默认情况下,禁止从文件系统导入。我们可以通过在 apoc.conf 中设置以下属性来启用它

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.conf
apoc.import.file.use_neo4j_config=false

Neo4j 现在可以从文件系统的任何位置读取,因此在设置此属性之前,请确保这是您的意图。

使用示例

本节中的示例基于 Microsoft 的 book.xml 文件。

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 下载。

我们可以编写以下查询来创建微软书籍 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 可视化显示了导入的图

apoc.import.xml

二进制文件

您还可以从二进制 byte[](未压缩)或压缩文件中导入(允许的压缩算法有:GZIPBZIP2DEFLATEBLOCK_LZ4FRAMED_SNAPPY)。

CALL apoc.import.xml(`binaryGzipByteArray`,  {compression: 'GZIP'})

CALL apoc.import.xml(`binaryFileNotCompressed`,  {compression: 'NONE'})

例如,这可以很好地与 apoc.util.compress 函数配合使用

WITH apoc.util.compress('<?xml version="1.0" encoding="UTF-8"?>
<parent name="databases">
    <child name="Neo4j">
        Neo4j is a graph database
    </child>
    <child name="relational">
        <grandchild name="MySQL"><![CDATA[
            MySQL is a database & relational
            ]]>
        </grandchild>
        <grandchild name="Postgres">
            Postgres is a relational database
        </grandchild>
    </child>
</parent>', {compression: 'DEFLATE'}) as xmlCompressed
CALL apoc.import.xml(xmlCompressed,  {compression: 'DEFLATE'})
YIELD node
RETURN node
结果
节点

[source,json] ---- { "identity": 11, "labels": [ "XmlDocument" ], "properties": { "_xmlEncoding": "UTF-8", "_xmlVersion": "1.0" } } ----