如何更改根元素

时间:2018-06-13 12:30:26

标签: java xml jaxb

我正在使用JAXB库将Java对象转换为XML文件。 现在我得到的输出是:

<company>
     <name>Amazon</name>
</company>

我想得到的是:

<companies>
      <company>
         <name>Amazon</name>
    </company>
</companies>

我如何达到目标?我是否必须从Java对象中删除@XmlRootElement(以及诅咒做其他事情)?

@XmlRootElement
public class Company {  
    private String name;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
}

或者我应该使用Marshaller方法编辑类吗?

public String marshall(BasicModel basicModel){      
            JAXBContext jaxbContext = JAXBContext.newInstance(basicModel.getClass());
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            StringWriter writer = new StringWriter();       
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);     

            jaxbMarshaller.marshal(basicModel, writer);             

            return writer.toString();   
    }

3 个答案:

答案 0 :(得分:0)

您可以创建一个包装类并对其进行序列化,无论Company@XmlRootElement还是{<1}},它都会有效。

@XmlRootElement(name = "companies")
@XmlAccessorType(XmlAccessType.FIELD)
public class Companies {  

    @XmlElement(name = "company")
    List<Company> companies = new ArrayList<Company>();

    // getters , setters...

}

答案 1 :(得分:0)

包装它:

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class Test {
    @XmlRootElement
    public static class Companies {
        private Company[] company;

        public Companies(){}
        public Companies(Company[] company){this.company = company;}

        public Company[] getCompany() {
            return company;
        }

        public void setCompany(Company[] company) {
            this.company = company;
        }
    }

    @XmlRootElement
    public static class Company {  
        private String name;

        public Company(){}
        public Company(String name){this.name = name;}

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static String marshall(Object basicModel) throws JAXBException{      
        JAXBContext jaxbContext = JAXBContext.newInstance(basicModel.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        StringWriter writer = new StringWriter();       
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);     

        jaxbMarshaller.marshal(basicModel, writer);             

        return writer.toString();   
    }

    public static void main(String[] args) throws JAXBException {
        Company comp1 = new Company("test");
        System.out.println(marshall(comp1));
        Companies c = new Companies();
        Company[] compArr = new Company[1];
        compArr[0] = comp1;
        c.setCompany(compArr);
        System.out.println(marshall(c));
    }
}

这会产生以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<company>
    <name>test</name>
</company>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<companies>
    <company>
        <name>test</name>
    </company>
</companies>

(我使用了一个数组,因为我假设你想保留一份公司名单。)

答案 2 :(得分:0)

SimpleXml可以做到:

final String data = ...
final SimpleXml simple = new SimpleXml();
final Element element = element("companies").child(simple.fromXml(data));
System.out.println(simple.domToXml(element));

将输出;

<companies><company><name>Amazon</name></company></companies>

从Maven Central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>