English 中文(简体)
将XML序列化重新改为java代码
原标题:Converting an XML serialization back to java code

I know this questions might seem a little odd, but I want to make sure. One of my superiors is kind of convinced that there is a more or less easy way to convert the XML serialization of an object back to the java code that created it. I am, however, pretty sure that the best I can get is the object.

因此,基本上我的问题是:是否有办法改变类似情况。

<java version="1.6.0_10" class="java.beans.XMLDecoder"> 
 <object class="javax.swing.JPanel"> 
  <void property="size"> 
   <object class="java.awt.Dimension"> 
    <int>42</int> 
    <int>23</int> 
   </object> 
  </void> 
 </object> 
</java>

回到类似的东西

JPanel jPanel = new JPanel();
jPanel.setSize(42,23);

提前感谢。

最佳回答

如果所有序列物体都符合 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() &gt; 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的转变,后者作为练习者:-

问题回答

Yes, I think there are several ways to implement this. First of all you can use JAXB technology, read about it http://www.oracle.com/technetwork/articles/javase/index-140168.html#xmp1. Second way: you always can read xml in runtime (DOM, SAX) and create objects dynamically using reflection.

我认为,这样做是不可能的,因为如果目标类别是一切,那么你会知道什么方法要求确定大小×至42? 也许有这方面的一套办法,可能只是建筑商,或者计算数字是多少。

我可以想象的唯一可能性是利用思考,这种思考越来越像XStream这样的框架。 因此,你可以制造同样的物体,但并非原先用于制造该物体的相同的代码。

It is not that difficult. I am not sure how this would work with Swing class like JPanel, but since it is a Java bean, it should not be a problem to use some kind of XML library like XStream, which is one of the easiest way of how to do such things. Or you could use more verbose JAXB or XML Beans.

Edit: I m sorry I didn t notice that there already is an XMLDecoder mentioned, and there seems to be an article on how to read JavaBean from XML file using XMLDecoder .

我怀疑,上级的信仰没有得到很好的表述和/或理解。 具体来说,“XML物体的序列化”可能是指某一特定物体的chem体,而不是XML,因此,“将物体的XML序列化与制造该物体的java代码相重”,系指从chem法到 mar子法,或类似法。

我从未看到过任何能够可靠地重建实际的<>代码指示的、用于根据普通XML序列化制造物体的通用技术。 如日本宇宙航空研究开发机构、XStream或xmlbeans一样,根据XML序列化的信息,你可以重启。 这一点在你看来是显而易见的,因为可以有多种方式将反对排在某个具体国家。 仅仅知道国家(实际上是XML序列化——物体在某个时间点的状态)不足以推断出该物体。

此外,还有许多类型的信息,性质上易变,无法序列(阅读处理、袖珍、窗口处理、装置环境等),因此,序列化不适用于所有物体/类别。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签