本文介绍了我需要编组到XML,我在XML文件中得到重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要编组为XML的对象.为此,我正在使用JAXB.生成的XML文件已创建,但是, 我在XML文件中得到重复的值,我不知道为什么得到这个,请检查输出XML文件

I have an object that I need to marshal to XML. To do this I am using JAXB. The resulting XML file is created, however, I am getting duplicate values in my XML file I don't know why I am getting this please chek the output XML file

以下是用于客户对象的Customer类

The following is Customer class for customer objects

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Customer")
public class JaxBExample {
    int id;
    String name;
    int age;
    //SETTERS AND GETTERS

    public int getId() {
        return id;
    }
    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "JaxBExample [id=" + id + ", name=" + name + ", age=" + age + "]";
    }        
}

该类用于列表以创建客户的对象列表

This Class is for List to make object list of customer

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Customers")
@XmlAccessorType (XmlAccessType.FIELD)
public class TestList implements Serializable {
    private List<JaxBExample> Customers=null;
    public List<JaxBExample> getCustomers() {
        return Customers;
    }
    @XmlElement
    public void setCustomers(List<JaxBExample> customers) {
        Customers = customers;
    }
    @Override
    public String toString() {
        return "TestList [Customers=" + Customers + "]";
    }
}

编组类将对象转换为XML

Marshalling class to convert object to XML

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class ConvertObj2XML {

public static void main(String args[]){

    JaxBExample customer=new JaxBExample();
    customer.setId(1);
    customer.setName("Raghava");
    customer.setAge(26);
    System.out.println("OBJ "+customer);

    JaxBExample customer1=new JaxBExample();
    customer1.setId(2);
    customer1.setName("mani");
    customer1.setAge(26);
    System.out.println("OBJ "+customer1);
    List<JaxBExample> customerLst=new ArrayList<JaxBExample>();
    customerLst.add(customer);
    customerLst.add(customer1);
    TestList customers=new TestList();
    customers.setCustomers(customerLst);
    System.out.println(customers.toString());

    try {

        File file = new File("E:\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(TestList.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(customers, file);
        jaxbMarshaller.marshal(customers, System.out);

          } catch (JAXBException e) {
        e.printStackTrace();
          }
    }
}

我在XML文件中获取重复的值,我不知道为什么要获取此值,请检查输出XML文件

I am getting duplicate values in my XML file I don't know why I am getting this please check the output XML file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customers>
    <Customers id="1">
        <age>26</age>
        <name>Raghava</name>
    </Customers>
    <Customers id="2">
        <age>26</age>
        <name>mani</name>
    </Customers>
    <customers id="1">
        <age>26</age>
        <name>Raghava</name>
    </customers>
    <customers id="2">
        <age>26</age>
        <name>mani</name>
    </customers>
</Customers>

在这种情况下,有人可以帮助我吗?

can anyone help me out in this case?

推荐答案

XmlAccessorType应该设置为None,如下所示:

The XmlAccessorType should be set to None as below

@XmlAccessorType (XmlAccessType.NONE)

根据文档

我已经进行了更改并运行了代码,它可以按预期工作.

I have made the change and run the code and it is working as expected.

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Customer")
public class JaxBExample {
     int id;
     String name;
     int age;
    //SETTERS AND GETTERS
    public int getId() {
        return id;
    }
    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "JaxBExample [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
}

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Customers")
@XmlAccessorType (XmlAccessType.NONE)
public class TestList implements Serializable {

    private List<JaxBExample> Customers=null;

    public List<JaxBExample> getCustomers() {
        return Customers;
    }
     @XmlElement
    public void setCustomers(List<JaxBExample> customers) {
        Customers = customers;
    }
    @Override
    public String toString() {
        return "TestList [Customers=" + Customers + "]";
    }
}


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


public class ConvertObj2XML {

public static void main(String args[]){

    JaxBExample customer=new JaxBExample();
    customer.setId(1);
    customer.setName("Raghava");
    customer.setAge(26);
    System.out.println("OBJ "+customer);

    JaxBExample customer1=new JaxBExample();
    customer1.setId(2);
    customer1.setName("mani");
    customer1.setAge(26);
    System.out.println("OBJ "+customer1);
    List<JaxBExample> customerLst=new ArrayList<JaxBExample>();
    customerLst.add(customer);
    customerLst.add(customer1);
    TestList customers=new TestList();
    customers.setCustomers(customerLst);
    System.out.println(customers.toString());

    try {
        File file = new File("E:\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(TestList.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(customers, file);
        jaxbMarshaller.marshal(customers, System.out);

          } catch (JAXBException e) {
        e.printStackTrace();
          }
}
}

OBJ JaxBExample [id=1, name=Raghava, age=26]
    OBJ JaxBExample [id=2, name=mani, age=26]
    TestList [Customers=[JaxBExample [id=1, name=Raghava, age=26], JaxBExample [id=2, name=mani, age=26]]]
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Customers>
    <customers id="1">
        <age>26</age>
        <name>Raghava</name>
    </customers>
    <customers id="2">
        <age>26</age>
        <name>mani</name>
    </customers>
</Customers>

这篇关于我需要编组到XML,我在XML文件中得到重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:11