将java对象转换为动态xml
我正在使用动态 xml 表格式,该格式由指定列名称和类型的架构以及包含行的值标记组成。下面是 xsd 的简化版本:
<xs:complexType name="data">
<xs:sequence>
<xs:element name="schema" type="schema"/>
<xs:element name="values" type="values"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="schema">
<xs:anyAttribute/>
</xs:complexType>
<xs:complexType name="values">
<xs:anyAttribute/>
</xs:complexType>
以及由它生成的示例 xml:
<data>
<schema firstName="string" lastName="string" age="integer">
<values>
<value firstName="A" lastName="B" age="23"/>
<value firstName="C" lastName="D" age="63"/>
…
</values>
</data>
生成 xml 的数据来自数据对象列表,对于我们的示例:
public class Person {
private String firstName;
private String lastName;
private int age;
// getters and setters…
}
目前,我通过向类添加这样的代码来创建它(我正在使用jaxb 生成 xml):
private static QName FIRST_NAME = new QName("firstName");
private static QName LAST_NAME = new QName("lastName");
private static QName AGE = new QName("age");
private static Map<QName, String> schema;
static {
schema = new HashMap<QName, String>();
schema.put(FIRST_NAME, "string");
schema.put(LAST_NAME, "string");
schema.put(AGE, "integer"):
}
public Map<QName, String> asMap() {
Map<QName, String> map = new HashMap<QName, String>();
map.put(FIRST_NAME, firstName);
map.put(LAST_NAME, lastName);
map.put(AGE, Integer.toString(age)):
return map;
}
public static Map<QName, String> getSchema() {
return schema;
}
然后对将用于生成 xml 的每个数据对象执行此操作。尽管我不认为这是最好的解决方案,但这确实有效。我看到的主要问题是模式和值之间没有联系,如果其中之一发生变化,您也必须记住更新另一个。另外,将其添加到所有类中也有点吵。
有人可以建议一种更好的(更干净/更通用)的方法吗?也许有一些自动生成模式和/或值映射的好方法?或者任何其他建议....
(如果我能在不更改类的情况下拥有可以在任何 java bean 下工作的东西,那可能是最好的,但如果需要的话,我可以在类中装饰/添加东西。)
谢谢
I'm working with a dynamic xml table format consisting of a schema that specifies column names and types, and a values tag which contains the rows. Simplified version of xsd below:
<xs:complexType name="data">
<xs:sequence>
<xs:element name="schema" type="schema"/>
<xs:element name="values" type="values"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="schema">
<xs:anyAttribute/>
</xs:complexType>
<xs:complexType name="values">
<xs:anyAttribute/>
</xs:complexType>
And a example xml generated by it:
<data>
<schema firstName="string" lastName="string" age="integer">
<values>
<value firstName="A" lastName="B" age="23"/>
<value firstName="C" lastName="D" age="63"/>
…
</values>
</data>
Data to generate the xml comes from a list of data objects, for our example:
public class Person {
private String firstName;
private String lastName;
private int age;
// getters and setters…
}
Currently I'm creating it by adding code like this to the class (I'm using jaxb to generate the xml):
private static QName FIRST_NAME = new QName("firstName");
private static QName LAST_NAME = new QName("lastName");
private static QName AGE = new QName("age");
private static Map<QName, String> schema;
static {
schema = new HashMap<QName, String>();
schema.put(FIRST_NAME, "string");
schema.put(LAST_NAME, "string");
schema.put(AGE, "integer"):
}
public Map<QName, String> asMap() {
Map<QName, String> map = new HashMap<QName, String>();
map.put(FIRST_NAME, firstName);
map.put(LAST_NAME, lastName);
map.put(AGE, Integer.toString(age)):
return map;
}
public static Map<QName, String> getSchema() {
return schema;
}
And then do that for each data object that would be used to generate xml. This works though I don't feel it's the best solution. Major problem I see with it is that there is no connection between the schema and the values, if something changes in one of them you have to remember to update the other one too. Also it's a bit noisy adding this to all classes.
Could someone suggest a better (cleaner/more generic) way of doing this? Maybe some good way to auto generate the schema and/or values maps? Or any other suggestion....
(It probably would be nicest if I could have something that worked given any java bean without changing the class, but I'm fine with decorating / adding things to the class if necessary.)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
整个问题在于该表是动态的。有必要吗?
如果没有,解决方案很简单,因为您可以创建一个静态表,然后使用 JAXB 将其绑定到 Java bean。
如果它确实需要是动态的,那么您唯一能做的就是使用自定义绑定器将表的静态实例链接到 Java bean。这意味着如果您有一个包含firstName、lastName 和age 的表的静态实例,那么您可以编写一个自定义绑定器来从该表生成Java bean,该表将具有正确的绑定(firstName、lastName 和age),即您现在手动执行的操作。这种方法的问题是编写绑定工具。
但是使用现成的软件,最简单的做法是不使用动态表,而是为动态表的每个实例编写模式。这些模式中的 XML 文档仍然符合定义动态表的文档,但它允许您使用 JAXB 自动生成所有代码并使 Java 类与 XML 文档保持同步。
The whole problem is that the table is dynamic. Does it need to be?
If not, the solution is straightforward, because you can make a static table and then bind it to Java beans using JAXB.
If it does need to be dynamic, then the only thing you can do is link static instances of the table to Java beans using a custom binder. Meaning if you have a static instance of the table that contains firstName, lastName and age, then you can write a custom binder that will generate Java beans from that table, which will have the proper bindings (firstName, lastName and age), which is what you're now doing manually. The problem with this approach is writing the binding tool.
But using off-the-shelf software, the easiest thing to do would be to not use the dynamic table and instead write schemas for each instance of the dynamic table. The XML documents from these schemas would still conform to the one that defines the dynamic table, but it would allow you to use JAXB to automate all the code generation and keep the Java classes in sync with the XML documents.