from("direct:start").
to("xj:identity?transformDirection=XML2JSON");
from("direct:start").
to("xj:identity?transformDirection=XML2JSON");
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
<?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>
<?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
Copied!
Toggle word wrap
Toggle overflow
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": "42",
"active": "true",
"ranking": "3.1415926",
"roles": [
"a",
{
"x": "null"
}
],
"state": {
"needsWater": "true"
}
}
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": "42",
"active": "true",
"ranking": "3.1415926",
"roles": [
"a",
{
"x": "null"
}
],
"state": {
"needsWater": "true"
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
ここでは何も特別なことは行っていませんが、json から xml への変換において、入力 xml と出力 json が上記の例と非常に似ていることがわかります。任意の XML ドキュメントを json に変換しただけです。XJ は、デフォルトで次のルールを使用します。
<?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>
<?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
Copied!
Toggle word wrap
Toggle overflow
from("direct:start").
to("xj:com/example/xml2json.xsl?transformDirection=XML2JSON");
from("direct:start").
to("xj:com/example/xml2json.xsl?transformDirection=XML2JSON");
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": 42,
"active": true,
"ranking": 3.1415926,
"roles": [
"a",
{
"x": null
}
],
"state": {
"needsWater": true
}
}
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": 42,
"active": true,
"ranking": 3.1415926,
"roles": [
"a",
{
"x": null
}
],
"state": {
"needsWater": true
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
この変換により、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>
<?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
Copied!
Toggle word wrap
Toggle overflow
スタイルシートでは、同じ結果を得るために必要最小限の型ヒントのみを指定しました。サポートされている型ヒントは、json から xml への変換時に XJ が 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>
<?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
Copied!
Toggle word wrap
Toggle overflow
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": 42,
"active": true,
"ranking": 3.1415926,
"roles": [
"a",
{
"x": null
}
],
"state": {
"needsWater": true
}
}
{
"firstname": "camel",
"lastname": "apache",
"personalnumber": 42,
"active": true,
"ranking": 3.1415926,
"roles": [
"a",
{
"x": null
}
],
"state": {
"needsWater": true
}
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
上記の例からわかるように、* xj:type では出力型を正確に指定でき、* xj:name では json キー名を却下できます。これは、XML 要素名で許可されていない文字を含むキー名を生成する場合に必要です。