karen
karen

Reputation: 943

JAXB Provide Empty XML Element as <xmlelement/> And Remove Namespace Name

I am writing a utility to integrate between an internal system and a third party product. I am trying to produce an xml file that can be loaded by the third party product but I am having trouble getting the xml to be produced exactly as they require it to be. I have created a simplified version just for testing.

The expected output should be as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Programme xmlns="http://www.awebsite.co.uk/ns/">
    <Editorial>
        <SeriesTitle>Series 1</SeriesTitle>
        <ProgrammeTitle>Test Programme</ProgrammeTitle>
        <EpisodeTitleNumber>Episode 1</EpisodeTitleNumber>
        <ProductionNumber/>
        <Synopsis/>
        <Originator/>
        <CopyrightYear/>
    </Editorial>
    <Technical>
        <ShimName/>
        <ShimVersion>1.0</ShimVersion>
        <ContactInformation>
            <ContactEmail/>
            <ContactTelephoneNumber/>
        </ContactInformation>
    </Technical>
</Programme>

The output that I am getting is as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Programme xmlns:ns2="http://www.awebsite.co.uk/ns/">
    <Editorial>
        <SeriesTitle>Series 1</SeriesTitle>
        <ProgrammeTitle>Test Programme</ProgrammeTitle>
        <EpisodeTitleNumber>Episode 1</EpisodeTitleNumber>
        <ProductionNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        <Synopsis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        <Originator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        <CopyrightYear xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    </Editorial>
    <Technical>
        <ShimName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        <ShimVersion>1.0</ShimVersion>
        <ContactInformation>
            <ContactEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
            <ContactTelephoneNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
        </ContactInformation>
    </Technical>
</ns2:Programme>

The 2 areas that I can't get right is the namespace having a value assigned to it and the null tags being self closing without the addition of: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true". I have tried empty tags but they get rejected as invalid xml.

My code is as follows:

Main:

public class TestXMLBuilder {

    public static void main (String ... args) {
        File file = new File("myxmltest.xml");
        JAXBContext jaxbContext;

        Editorial editorial = new Editorial();
        editorial.setProgrammeTitle("Test Programme");
        editorial.setEpisodeTitleNumber("Episode 1");
        editorial.setSeriesTitle("Series 1");
        Programme programme = new Programme();
        programme.setEditorial(editorial);

        try {
            jaxbContext = JAXBContext.newInstance(Programme.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            // output pretty printed
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


            marshaller.marshal(programme, file);
            marshaller.marshal(programme, System.out);
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Programme Class:

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 = "Programme", namespace = "http://www.awebsite.co.uk/ns/")
@XmlAccessorType(XmlAccessType.FIELD)
public class Programme {

    @XmlElement(name = "Editorial", required = true)
    private Editorial editorial = new Editorial();

    @XmlElement(name = "Technical", required = true)
    private Technical technical = new Technical();


    /**
     * @return the editorial
     */
    public Editorial getEditorial() {
        return editorial;
    }

    /**
     * @param editorial the editorial to set
     */
    public void setEditorial(Editorial editorial) {
        this.editorial = editorial;
    }

    /**
     * @return the technical
     */
    public Technical getTechnical() {
        return technical;
    }

    /**
     * @param technical the technical to set
     */
    public void setTechnical(Technical technical) {
        this.technical = technical;
    }

}

Editorial Class:

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 = "Editorial")
@XmlAccessorType(XmlAccessType.FIELD)
public class Editorial {

    @XmlElement(name = "SeriesTitle", required = true, nillable = true)
    private String seriesTitle = null;

    @XmlElement(name = "ProgrammeTitle", required = true, nillable = true)
    private String programmeTitle = null;

    @XmlElement(name = "EpisodeTitleNumber", required = true, nillable = true)
    private String episodeTitleNumber = null;

    @XmlElement(name = "ProductionNumber", required = true, nillable = true)
    private String productionNumber = null;

    @XmlElement(name = "Synopsis", required = true, nillable = true)
    private String synopsis = null;

    @XmlElement(name = "Originator", required = true, nillable = true)
    private String originator = null;

    @XmlElement(name = "CopyrightYear", required = true, nillable = true)
    private String copyrightYear = null;

    /**
     * @return the seriesTitle
     */
    public String getSeriesTitle() {
        return seriesTitle;
    }

    /**
     * @param seriesTitle the seriesTitle to set
     */
    public void setSeriesTitle(String seriesTitle) {
        this.seriesTitle = seriesTitle;
    }

    /**
     * @return the programmeTitle
     */
    public String getProgrammeTitle() {
        return programmeTitle;
    }

    /**
     * @param programmeTitle the programmeTitle to set
     */
    public void setProgrammeTitle(String programmeTitle) {
        this.programmeTitle = programmeTitle;
    }

    /**
     * @return the episodeTitleNumber
     */
    public String getEpisodeTitleNumber() {
        return episodeTitleNumber;
    }

    /**
     * @param episodeTitleNumber the episodeTitleNumber to set
     */
    public void setEpisodeTitleNumber(String episodeTitleNumber) {
        this.episodeTitleNumber = episodeTitleNumber;
    }

    /**
     * @return the productionNumber
     */
    public String getProductionNumber() {
        return productionNumber;
    }

    /**
     * @param productionNumber the productionNumber to set
     */
    public void setProductionNumber(String productionNumber) {
        this.productionNumber = productionNumber;
    }

    /**
     * @return the synopsis
     */
    public String getSynopsis() {
        return synopsis;
    }

    /**
     * @param synopsis the synopsis to set
     */
    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    /**
     * @return the originator
     */
    public String getOriginator() {
        return originator;
    }

    /**
     * @param originator the originator to set
     */
    public void setOriginator(String originator) {
        this.originator = originator;
    }

    /**
     * @return the copyrightYear
     */
    public String getCopyrightYear() {
        return copyrightYear;
    }

    /**
     * @param copyrightYear the copyrightYear to set
     */
    public void setCopyrightYear(String copyrightYear) {
        this.copyrightYear = copyrightYear;
    }


}

Technical Class:

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 = "Technical")
@XmlAccessorType(XmlAccessType.FIELD)
public class Technical {

    @XmlElement(name = "ShimName", required = true, nillable = true)
    private String shimName = null;

    @XmlElement(name = "ShimVersion", required = true, nillable = true)
    private String shimVersion = "1.0";


    @XmlElement(name = "ContactInformation", required = true, nillable = true)
    private ContactInformation contactInformation = new ContactInformation();

}

ContactInformation Class:

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 = "ContactInformation")
@XmlAccessorType(XmlAccessType.FIELD)
public class ContactInformation {


    @XmlElement(name = "ContactEmail", required = true, nillable = true)
    private String contactEmail = null;

    @XmlElement(name = "ContactTelephoneNumber", required = true, nillable = true)
    private String contactTelephoneNumber = null;

    /**
     * @return the contactEmail
     */
    public String getContactEmail() {
        return contactEmail;
    }

    /**
     * @param contactEmail the contactEmail to set
     */
    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }

    /**
     * @return the contactTelephoneNumber
     */
    public String getContactTelephoneNumber() {
        return contactTelephoneNumber;
    }

    /**
     * @param contactTelephoneNumber the contactTelephoneNumber to set
     */
    public void setContactTelephoneNumber(String contactTelephoneNumber) {
        this.contactTelephoneNumber = contactTelephoneNumber;
    }


}

Upvotes: 0

Views: 2978

Answers (1)

karen
karen

Reputation: 943

Unfortunately, I ended up having to manipulate(hack) the xml as follows:

        StringWriter stringWriter = new StringWriter();
        marshaller.marshal(programme, stringWriter);

        String xmlContent = stringWriter.toString();
        xmlContent = xmlContent.replaceAll("xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
        xmlContent = xmlContent.replaceAll("</ns2:", "</");
        xmlContent = xmlContent.replaceAll(":ns2=", "=");
        xmlContent = xmlContent.replaceAll("<ns2:", "<");

        result = xmlContent;

Upvotes: 1

Related Questions