我有一个 Java 应用程序可以与其他几个信息系统互操作
同一个对象可以根据所针对的信息系统映射到不同的XML文件中
我的问题是:是否有一个 Java 解决方案可以在同一对象上执行多个 XML 映射/绑定(bind)
类似于 Bean 验证组的东西,可以使用不同的验证配置文件验证对象
在 JAXB 风格中,它可能是这样的,例如:
// pseudocode
@XmlRootElement(name="person", , profile="profile1")
@XmlRootElement(name="individual", profile="profile2")
@XmlRootElement(name="human", profile="profile3")
public class Person {
@XmlElement(name = "lastName", profile="profile1")
@XmlElement(name = "surname", profile="profile2")
@XmlElement(name = "famillyName", profile="profile3")
private String lastName;
//...
}
然后
// pseudocode
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller("profile1");
jaxbMarshaller.marshal(person, file);
//...
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller("profile1");
Person person = (Person) jaxbUnmarshaller.unmarshal(file);
也许可以用 JAXB 做这样的事情,但我不知道
编辑
@Martin Serrano 的回复为我提供了一些使用 JAXB 进行优化的线索。
一个抽象类来统治它们:
@XmlTransient
public abstract class APerson {
protected String lastname;
public APerson() {
}
public APerson(APerson p) {
lastname = p.lastname;
}
public abstract String getLastname();
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" + lastname + "}";
}
}
以及进行映射的具体类:
默认映射的 Person 类:
@XmlRootElement
public class Person extends APerson {
public Person() {
}
public Person(APerson p) {
super(p);
}
@Override
public String getLastname() {
return lastname;
}
}
和其他用于替代映射的类:
@XmlRootElement
public class Human extends APerson {
public Human() {
}
public Human(APerson p) {
super(p);
}
@Override
@XmlElement(name = "famillyName")
public String getLastname() {
return lastname;
}
}
@XmlRootElement
public class Individual extends APerson{
public Individual() {
}
public Individual(APerson p) {
super(p);
}
@Override
@XmlElement(name = "surname")
public String getLastname() {
return lastname;
}
}
然后测试:
public class Main {
public static void main(String[] args) throws Exception {
Person person = new Person();
person.setLastname("Doe");
String fileName = "person.xml";
marshal(person, fileName);
person = unmarshal(Person.class, fileName);
System.out.println(person);
fileName = "human.xml";
Human human = new Human(person);
marshal(human, fileName);
human = unmarshal(Human.class, fileName);
System.out.println(human);
person = new Person(human);
System.out.println(person);
fileName = "individual.xml";
Individual individual = new Individual(person);
marshal(individual, fileName);
individual = unmarshal(Individual.class, fileName);
System.out.println(individual);
person = new Person(individual);
System.out.println(person);
}
private static <P extends APerson> void marshal(P person, String fileName) throws JAXBException {
File file = new File(fileName);
JAXBContext context = JAXBContext.newInstance(person.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(person, file);
marshaller.marshal(person, System.out);
}
private static <P extends APerson> P unmarshal(Class<P> cl, String fileName) throws JAXBException {
File file = new File(fileName);
JAXBContext context = JAXBContext.newInstance(cl);
Unmarshaller unmarshaller = context.createUnmarshaller();
P person = (P) unmarshaller.unmarshal(file);
return person;
}
}
结果:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<lastname>Doe</lastname>
</person>
Person{Doe}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<human>
<famillyName>Doe</famillyName>
</human>
Human{Doe}
Person{Doe}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<individual>
<surname>Doe</surname>
</individual>
Individual{Doe}
Person{Doe}
我发现最好的解决方案是具有不同的映射并避免代码重复,因此我们只需要使用映射来实现 getter。
让我们看看它是否在解决更大的问题!
但是虽然它比拥有 3 个不同的类要好得多,但它仍然不是同一类上的 3 个映射......
最佳答案
执行此类操作的最简单方法是为每个系统使用不同的外观。这将允许您更改 JAXB 反/序列化对象的方式。它还允许您根据所讨论的系统进行其他转换和调整。基本 Person 类将具有您的默认序列化,并且每个系统都可以在必要时使用适合它的外观。这基本上是关于 mapping interfaces 的非官方 jaxb 指南中概述的方法。 .
可以为每个系统注册这样的外观,允许您将系统特定的配置文件保留在每个核心类型的代码之外。
使用您的示例,核心界面将是:
@XmlRootElement(name="person")
public interface Person {
@XmlElement(name = "lastName")
private String lastName;
}
那么对于 System1,您将拥有:
@XmlRootElement(name="individual")
public class Individual implements Person {
}
另一个系统可能有:
@XmlRootElement(name="human")
public class Human implements Person {
}
关于java - 同一 Java 对象上的不同 XML 映射/绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777186/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/