简体   繁体   中英

JAXB: Remove parent node keeping it's children

I know it doesn't make much sense, but I have to generate an XML from a Java object without the parent node of some elements, like explained below.

This is the example Java class model for the XML:

@XmlRootElement(name = "person")
public class PersonXml {

    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "car")
    private List<CarXml> cars;

.

@XmlRootElement(name = "car")
public class CarXml {

    @XmlElement(name = "model")
    private String model;

    @XmlElement(name = "brand")
    private String brand;

By default, if I generate the XML from an object of PersonXml like this:

StringWriter writer = new StringWriter();

JAXBContext ctx = JAXBContext.newInstance(PersonXml.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.marshal(xml, writer);

I would get:

<person>
    <name>Pedro</name>
    <car>
        <model>Logan</model>
        <brand>Renault</brand>
    </car>
    <car>
        <model>Duster</model>
        <brand>Renault</brand>
    </car>
</person>

What I need is to remove the <car> tag, or even to prevent it to be generated at all.

I need the XML to be like this:

<person>
    <name>Pedro</name>
    <model>Logan</model>
    <brand>Renault</brand>
    <model>Duster</model>
    <brand>Renault</brand>
</person>

Of course I could convert the XML to a String and remove the tags with replaceAll or something like this, but I was wondering if there is a nicer way to achieve this.

If you need to generate this output, you can use JAXB as follows:

1) Create a new Person class:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "field"
})
@XmlRootElement(name = "person")
public class Person {

    @XmlElementRefs({
        @XmlElementRef(name = "name", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "model", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "brand", type = JAXBElement.class, required = false)
    })
    protected List<JAXBElement<String>> field;

    public List<JAXBElement<String>> getNameOrModelOrBrand() {
        if (field == null) {
            field = new ArrayList<>();
        }
        return this.field;
    }

}

2) Create an ObjectFactory to make it easier to use the person class:

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    private final static QName _PersonName_QNAME = new QName("", "name");
    private final static QName _PersonModel_QNAME = new QName("", "model");
    private final static QName _PersonBrand_QNAME = new QName("", "brand");

    public ObjectFactory() {
    }

    public Person createPerson() {
        return new Person();
    }

    @XmlElementDecl(namespace = "", name = "name", scope = Person.class)
    public JAXBElement<String> createPersonName(String value) {
        return new JAXBElement<>(_PersonName_QNAME, String.class, Person.class, value);
    }

    @XmlElementDecl(namespace = "", name = "model", scope = Person.class)
    public JAXBElement<String> createPersonModel(String value) {
        return new JAXBElement<>(_PersonModel_QNAME, String.class, Person.class, value);
    }

    @XmlElementDecl(namespace = "", name = "brand", scope = Person.class)
    public JAXBElement<String> createPersonBrand(String value) {
        return new JAXBElement<>(_PersonBrand_QNAME, String.class, Person.class, value);
    }

}
  1. Use the factory as follows:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.List;

...

ObjectFactory factory = new ObjectFactory();

Person person = factory.createPerson();
List<JAXBElement<String>> list = person.getNameOrModelOrBrand();
list.add(factory.createPersonName("Pedro"));
list.add(factory.createPersonModel("Logan"));
list.add(factory.createPersonBrand("Renault"));
list.add(factory.createPersonModel("Duster"));
list.add(factory.createPersonBrand("Renault"));

JAXBContext ctx = JAXBContext.newInstance(Person.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writer = new StringWriter();
marshaller.marshal(person, writer);
System.out.println(writer.toString());

The end result is XML as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <name>Pedro</name>
    <model>Logan</model>
    <brand>Renault</brand>
    <model>Duster</model>
    <brand>Renault</brand>
</person>

Creating elements in this way is the only way I know to get the end result you need.

There are probably various things you could do to refactor the above code, to streamline the creation of the list of elements - but this shows you the basic approach.

As you already know - this is far from ideal. The end result is not any type of XML that I would want to receive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM