如果所有序列物体都符合 j豆合同,你可以重新启动XML de-serializer随后进行的分离 objects物体的程序,以便重新制定与其相符的法典。
在金森XML日,我开展了一些项目,利用类似程序从XML定义中产生 Java代码。
Departing from your serialized model, you can use a XSL-T transformation to recreate the code that lead to the serialized objects. This process will create very linear code (as in non-modular), but you ll have what you re looking for.
An example to get you started: To process the XML you provided, you can use the following recursive transformation: copy/paste it & try it here: online XSL-T (the template is based on Xpath 1.0 to be able to use the online tool. Xpath 2.0 will improve the code in some areas, like string functions)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xf="http://www.w3.org/2005/xpath-functions">
<xsl:template match="object">
<xsl:call-template name="objectClass" /> <xsl:value-of select="string( )" />
<xsl:call-template name="objectNodeName" />
= new <xsl:call-template name="objectClass" />(<xsl:call-template name="objectParams" />);
<xsl:for-each select="*[@property]">
<xsl:apply-templates />
<xsl:call-template name="setProperty" />
</xsl:for-each>
</xsl:template>
<xsl:template match="/" >
<xsl:apply-templates match="/object" />
</xsl:template>
<xsl:template match="text()" />
<xsl:template name="objectNodeName">
<xsl:param name="node" select="." />
<xsl:value-of select="translate($node/@class, . , _ )" />_<xsl:value-of select="count($node/ancestor-or-self::*)" />
</xsl:template>
<xsl:template name="setProperty">
<xsl:call-template name="objectNodeName" > <xsl:with-param name="node" select="parent::node()"/></xsl:call-template>
.set<xsl:call-template name="capitalize"><xsl:with-param name="str" select="@property"/></xsl:call-template>(<xsl:call-template name="objectNodeName" > <xsl:with-param name="node" select="node()"/></xsl:call-template>);
</xsl:template>
<xsl:template name="objectClass">
<xsl:param name="fqn" select="@class" />
<xsl:value-of select="$fqn" />
</xsl:template>
<xsl:template name="objectParams">
<xsl:for-each select="*[not(child::object)]">
<xsl:if test="position() > 1">,</xsl:if><xsl:value-of select="." />
</xsl:for-each>
</xsl:template>
<xsl:variable name="smallcase" select=" abcdefghijklmnopqrstuvwxyz " />
<xsl:variable name="uppercase" select=" ABCDEFGHIJKLMNOPQRSTUVWXYZ " />
<xsl:template name="capitalize">
<xsl:param name="str" select="." />
<xsl:value-of select="concat(translate(substring($str,1,1),$smallcase,$uppercase),substring($str,2))">
</xsl:template>
</xsl:stylesheet>
申斥: 我测试了所提供的样本的模板和其中的一些改动,包括一些含有另外几个物体。 我没有测试更深的物体nes。 它举了一个例子,而不是一个完全功能的XML Serialization至Java的转变,后者作为练习者:-