【问题标题】:How to compare jaxb, object, string to find differences如何比较 jaxb、对象、字符串以发现差异
【发布时间】:2016-06-21 14:14:50
【问题描述】:

我想知道如何比较两个字符串/对象 (JaxB) 以找出差异。

我的例子是什么:

我有这个要求:

<zoo>
<zooId>12321</zooId>
<zooName>From Belgium</zooName>
<zooAddress>berlin</zooAddress></zoo>

第二天,我有这个要求(更新):

<zoo>
<zooId>12321</zooId>
<zooName>From Germany</zooName>  
<phone>0123456789</phone>
<zooAddress>berlin</zooAddress></zoo>

而我想要的区别是:

<zoo>   
<zooName>From Germany</zooName>  
<phone>0123456789</phone></zoo>

我谈到了字符串、对象和“Jaxb 对象”,因为我处理的是 JaxB 请求,我可以将它转换为字符串 我的持久层有一个与休眠实体“一对一”的映射。我不想限制选择/想法;-)

提前致谢,

弗朗索瓦

【问题讨论】:

  • 请问你贴对应的实体类吗?
  • 公共类动物园{私人长动物园ID;私人字符串动物园名称;私人字符串电话;私有字符串 zooAddress;
  • 对不起,我尝试将其添加为“代码”但失败:-(

标签: java jaxb compare


【解决方案1】:

第1步:请按如下方式创建Zoo.java

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 步:请在系统的任意位置创建以下两个文件。

(a)zoo1.xml

<zoo>
    <zooId>12321</zooId>
    <zooName>From Belgium</zooName>
    <zooAddress>berlin</zooAddress>
</zoo>

(b)zoo2.xml

<zoo>
    <zooId>12321</zooId>
    <zooName>From Germany</zooName>  
    <phone>0123456789</phone>
    <zooAddress>berlin</zooAddress>
</zoo>

第3步:请创建ZooMain.java,如下:

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);

    }

}

希望,这会有所帮助。

【讨论】:

    猜你喜欢
    • 2010-10-31
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多