142.7. 使用 XJ 端点


142.7.1. 将 JSON 转换为 XML

以下路由对消息进行"身份"转换,因为未给出 xslt 风格表。在 xml 到 xml 转换的情况下,"Identity"转换意味着输出文档只是输入文档的一个副本。如果是 XJ,这意味着它会将 json 文档转换为等同的 xml 表示。

from("direct:start").
  to("xj:identity?transformDirection=JSON2XML");
Copy to Clipboard Toggle word wrap

示例:

输入:

{
  "firstname": "camel",
  "lastname": "apache",
  "personalnumber": 42,
  "active": true,
  "ranking": 3.1415926,
  "roles": [
    "a",
    {
      "x": null
    }
  ],
  "state": {
    "needsWater": true
  }
}
Copy to Clipboard Toggle word wrap

将输出

<?xml version="1.0" encoding="UTF-8"?>
<object xmlns:xj="http://camel.apache.org/component/xj" xj:type="object">
    <object xj:name="firstname" xj:type="string">camel</object>
    <object xj:name="lastname" xj:type="string">apache</object>
    <object xj:name="personalnumber" xj:type="int">42</object>
    <object xj:name="active" xj:type="boolean">true</object>
    <object xj:name="ranking" xj:type="float">3.1415926</object>
    <object xj:name="roles" xj:type="array">
        <object xj:type="string">a</object>
        <object xj:type="object">
            <object xj:name="x" xj:type="null">null</object>
        </object>
    </object>
    <object xj:name="state" xj:type="object">
        <object xj:name="needsWater" xj:type="boolean">true</object>
    </object>
</object>
Copy to Clipboard Toggle word wrap

正如在上面的输出中所见,XJ 在生成的 xml 中写入一些元数据,这些元数据可用于进一步处理:

  • XJ 元数据节点总是位于 http://camel.apache.org/component/xj 命名空间中。
  • JSON 键名称放置在 xj:name 属性中。
  • 解析的 JSON 类型可以在 xj:type 属性中找到。上例已包含所有可能的类型。
  • 生成的 XML 元素始终命名为 "object"。

现在,我们可以应用风格表,例如:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xj="http://camel.apache.org/component/xj"
                exclude-result-prefixes="xj">

    <xsl:output omit-xml-declaration="no" encoding="UTF-8" method="xml" indent="yes"/>

    <xsl:template match="/">
        <person>
            <xsl:apply-templates select="//object"/>
        </person>
    </xsl:template>

    <xsl:template match="object[@xj:type != 'object' and @xj:type != 'array' and string-length(@xj:name) > 0]">
        <xsl:variable name="name" select="@xj:name"/>
        <xsl:element name="{$name}">
            <xsl:value-of select="text()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*|node()"/>
</xsl:stylesheet>
Copy to Clipboard Toggle word wrap

在以上示例中指定端点上的模板:

from("direct:start").
  to("xj:com/example/json2xml.xsl?transformDirection=JSON2XML");
Copy to Clipboard Toggle word wrap

并获取以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <firstname>camel</firstname>
    <lastname>apache</lastname>
    <personalnumber>42</personalnumber>
    <active>true</active>
    <ranking>3.1415926</ranking>
    <x>null</x>
    <needsWater>true</needsWater>
</person>
Copy to Clipboard Toggle word wrap

142.7.2. 将 XML 转换为 JSON

当未给出风格表时,将根据上述"身份"转换的解释执行:

from("direct:start").
  to("xj:identity?transformDirection=XML2JSON");
Copy to Clipboard Toggle word wrap

给出示例输入

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <firstname>camel</firstname>
    <lastname>apache</lastname>
    <personalnumber>42</personalnumber>
    <active>true</active>
    <ranking>3.1415926</ranking>
    <roles>
        <entry>a</entry>
        <entry>
            <x>null</x>
        </entry>
    </roles>
    <state>
        <needsWater>true</needsWater>
    </state>
</person>
Copy to Clipboard Toggle word wrap

将导致

{
  "firstname": "camel",
  "lastname": "apache",
  "personalnumber": "42",
  "active": "true",
  "ranking": "3.1415926",
  "roles": [
    "a",
    {
      "x": "null"
    }
  ],
  "state": {
    "needsWater": "true"
  }
}
Copy to Clipboard Toggle word wrap

您可能注意到,当从 json 转换为 xml 完全没有特别特别时,输入 xml 和输出 json 与上面的示例非常相似。我们只将任意 XML 文档转换为 json。XJ 默认使用以下规则:

  • XML root 元素可以命名某种方式,它将始终以 json root 对象声明 '\{}' 结尾
  • json 键名称是 xml 元素的名称
  • 如果上面有一个名称 clash as in "<roles>",则会生成两个 "<entry>" 元素。
  • 带有 text-only-child-nodes 的 XML 元素将导致通常的键/字符串-值对。混合内容元素会导致键/子对象对,如上面的 "<state>" 中所示。

现在,我们可以再次应用风格表,例如:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xj="http://camel.apache.org/component/xj"
                exclude-result-prefixes="xj">

    <xsl:output omit-xml-declaration="no" encoding="UTF-8" method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="personalnumber">
        <xsl:element name="{local-name()}">
            <xsl:attribute name="xj:type">
                <xsl:value-of select="'int'"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="active|needsWater">
        <xsl:element name="{local-name()}">
            <xsl:attribute name="xj:type">
                <xsl:value-of select="'boolean'"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="ranking">
        <xsl:element name="{local-name()}">
            <xsl:attribute name="xj:type">
                <xsl:value-of select="'float'"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="roles">
        <xsl:element name="{local-name()}">
            <xsl:attribute name="xj:type">
                <xsl:value-of select="'array'"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*[normalize-space(text()) = 'null']">
        <xsl:element name="{local-name()}">
            <xsl:attribute name="xj:type">
                <xsl:value-of select="'null'"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Copy to Clipboard Toggle word wrap

通过指定端点上的模板来进入上例:

from("direct:start").
  to("xj:com/example/xml2json.xsl?transformDirection=XML2JSON");
Copy to Clipboard Toggle word wrap

并获取以下输出:

{
  "firstname": "camel",
  "lastname": "apache",
  "personalnumber": 42,
  "active": true,
  "ranking": 3.1415926,
  "roles": [
    "a",
    {
      "x": null
    }
  ],
  "state": {
    "needsWater": true
  }
}
Copy to Clipboard Toggle word wrap

请注意,这个转换会导致与对 json2xml 转换的输入完全相同的 json 文档。以下 XML 文档是在 xsl 转换后传递给 XJ 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <firstname>camel</firstname>
    <lastname>apache</lastname>
    <personalnumber xmlns:xj="http://camel.apache.org/component/xj" xj:type="int">42</personalnumber>
    <active xmlns:xj="http://camel.apache.org/component/xj" xj:type="boolean">true</active>
    <ranking xmlns:xj="http://camel.apache.org/component/xj" xj:type="float">3.1415926</ranking>
    <roles xmlns:xj="http://camel.apache.org/component/xj" xj:type="array">
        <entry>a</entry>
        <entry>
            <x xj:type="null">null</x>
        </entry>
    </roles>
    <state>
        <needsWater xmlns:xj="http://camel.apache.org/component/xj" xj:type="boolean">true</needsWater>
    </state>
</person>
Copy to Clipboard Toggle word wrap

在风格表中,我们只提供了最小的所需类型提示,以获得同样的结果。从 json 转换为 xml 时,支持的类型提示与 XJ 写入 XML 文档完全相同。

在结尾处,我们可以返回 json 到 xml 转换示例的结果文档:

<?xml version="1.0" encoding="UTF-8"?>
<object xmlns:xj="http://camel.apache.org/component/xj" xj:type="object">
    <object xj:name="firstname" xj:type="string">camel</object>
    <object xj:name="lastname" xj:type="string">apache</object>
    <object xj:name="personalnumber" xj:type="int">42</object>
    <object xj:name="active" xj:type="boolean">true</object>
    <object xj:name="ranking" xj:type="float">3.1415926</object>
    <object xj:name="roles" xj:type="array">
        <object xj:type="string">a</object>
        <object xj:type="object">
            <object xj:name="x" xj:type="null">null</object>
        </object>
    </object>
    <object xj:name="state" xj:type="object">
        <object xj:name="needsWater" xj:type="boolean">true</object>
    </object>
</object>
Copy to Clipboard Toggle word wrap

再次获取相同的输出:

{
  "firstname": "camel",
  "lastname": "apache",
  "personalnumber": 42,
  "active": true,
  "ranking": 3.1415926,
  "roles": [
    "a",
    {
      "x": null
    }
  ],
  "state": {
    "needsWater": true
  }
}
Copy to Clipboard Toggle word wrap

如上例中所示:* xj:type 可让您指定所需的输出类型 * xj:name 可让您覆盖 json 键名称。当您要生成包含在 XML 元素名称中不允许的字符的密钥名称时需要这样做。

142.7.2.1. 可用类型提示

Expand
@xj:type=描述

object

生成 json 对象

数组

生成 json 数组

string

生成 json 字符串

int

生成 json 号而无需部分部分

浮点值

使用部分部分生成 json 号

布尔值

生成 json 布尔值

null

使用 null 一词生成空值

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat