如何比较jaxb,object,string来查找差异 [英] How to compare jaxb, object, string to find differences
问题描述
我想知道如何比较两个String / Object(JaxB)以找出差异。
I would like to know how I can compare two String/Object (JaxB) to find the differences.
我的意思是一个例子:
我有这个要求:
<zoo>
<zooId>12321</zooId>
<zooName>From Belgium</zooName>
<zooAddress>berlin</zooAddress></zoo>
第二天,我有这个请求(更新):
The next day, I have this request (for an update):
<zoo>
<zooId>12321</zooId>
<zooName>From Germany</zooName>
<phone>0123456789</phone>
<zooAddress>berlin</zooAddress></zoo>
我想要的差异是:
<zoo>
<zooName>From Germany</zooName>
<phone>0123456789</phone></zoo>
我谈到了String,Object和Jaxb Object因为我使用了JaxB请求,我可以将它转换为String
,我有一个映射一对一,我的持久层有一个hibernate实体。我不想限制选择/想法; - )
I talked about String, Object and "Jaxb Object" because I work with a JaxB request, I can convert it in a String and I have a mapping "one to one" with an hibernate entity for my persistence layer. I don't want to limit the choices/ideas ;-)
提前致谢,
François
推荐答案
步骤1:请创建以下 Zoo.java
,如下所示: / p>
Step 1: Please create following Zoo.java
as follows:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Zoo {
private Long zooID;
private String zooName;
private String phone;
private String zooAddress;
public Long getZooID() {
return zooID;
}
@XmlElement(name = "zooId")
public void setZooID(Long zooID) {
this.zooID = zooID;
}
public String getZooName() {
return zooName;
}
@XmlElement
public void setZooName(String zooName) {
this.zooName = zooName;
}
public String getPhone() {
return phone;
}
@XmlElement
public void setPhone(String phone) {
this.phone = phone;
}
public String getZooAddress() {
return zooAddress;
}
@XmlElement
public void setZooAddress(String zooAddress) {
this.zooAddress = zooAddress;
}
@Override
public String toString() {
return "Zoo [zooID=" + zooID + ", zooName=" + zooName + ", phone=" + phone + ", zooAddress=" + zooAddress + "]";
}
public Zoo getDiff(Zoo that) {
Zoo diff = new Zoo();
if (that.getZooID() == null || (this.getZooID() != null && !this.getZooID().equals(that.getZooID()))) {
diff.setZooID(this.getZooID());
}
if (that.getZooName() == null || (this.getZooName() != null && !this.getZooName().equals(that.getZooName()))) {
diff.setZooName(this.getZooName());
}
if (that.getPhone() == null || (this.getPhone() != null && !this.getPhone().equals(that.getPhone()))) {
diff.setPhone(this.getPhone());
}
if (that.getZooAddress() == null || (this.getZooAddress() != null && !this.getZooAddress().equals(that.getZooAddress()))) {
diff.setZooAddress(this.getZooAddress());
}
return diff;
}
}
第2步:请在您系统的任何位置创建以下两个文件。
Step 2: Please create following two files in any location on your system.
(a)zoo1.xml
(a) zoo1.xml
<zoo>
<zooId>12321</zooId>
<zooName>From Belgium</zooName>
<zooAddress>berlin</zooAddress>
</zoo>
(b)zoo2.xml
(b) zoo2.xml
<zoo>
<zooId>12321</zooId>
<zooName>From Germany</zooName>
<phone>0123456789</phone>
<zooAddress>berlin</zooAddress>
</zoo>
第3步:请创建以下 ZooMain.java
如下:
Step 3: Please create following ZooMain.java
as follows:
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ZooMain {
public static void main(String[] args) throws IOException, JAXBException {
String zoo1Str = new String(Files.readAllBytes(new File("D:/zoo1.xml").toPath()));
String zoo2Str = new String(Files.readAllBytes(new File("D:/zoo2.xml").toPath()));
JAXBContext jaxbContext = JAXBContext.newInstance(Zoo.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Zoo zoo1 = (Zoo) jaxbUnmarshaller.unmarshal(new StringReader(zoo1Str));
Zoo zoo2 = (Zoo) jaxbUnmarshaller.unmarshal(new StringReader(zoo2Str));
Zoo diff = zoo2.getDiff(zoo1);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw=new StringWriter();
jaxbMarshaller.marshal(diff, sw);
System.out.println(sw);
}
}
希望,这个帮助。
这篇关于如何比较jaxb,object,string来查找差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!