English 中文(简体)
B. 星号测绘格式作为星号定义的目标
原标题:Object to xml mapping formated as spring bean definition

I am looking for a way to serialize java object into XML in format same as spring bean defination. For example, the class defined as:

package x.y.z;
class foo {
    String name;
    int counter;

    ...setter and getter omitted for simplicity ....
}

班级 f的序号为:

<bean id="" class="x.y.z.foo">
   <property name="name" value="some random value"/>
   <property name="counter" value="1" />
</bean>

目的是,后来,我可以通过复制/剪辑Xml,将物体注入单位测试,以生成背景档案。

问题回答

If you are ok with annotations, something like Simple XML Serialization can do something like what you are looking for. I also like XStream, but it may not give itself to an XML that is as different from the class structure as yours is.

http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php

确实,你也许会更不用说写一部具有像这样强大缓冲的长官:

public Element marshal() {
    StringBuffer sb = new StringBuffer();
    sb.append("	<bean id="">
");
        sb.append("		<property name="").append(name).append(""/>
");
    sb.append("	</bean>
");
    InputStream istream = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    Document myDoc = new SAXBuilder().build(istream);
}

或更好地形成文件并增加内容。

然后产出

    // Save it to a file:
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    java.io.FileWriter writer = new java.io.FileWriter(fileName);
    out.output(document, writer);
    writer.flush();
    writer.close();

我认为,如果你的计划非常简单,但不同于你的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 ...

热门标签