JAXB提供空XML元素作为< xmlelement/>并删除命名空间名称 [英] JAXB Provide Empty XML Element as <xmlelement/> And Remove Namespace Name

查看:79
本文介绍了JAXB提供空XML元素作为< xmlelement/>并删除命名空间名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实用程序,用于在内部系统和第三方产品之间进行集成.我正在尝试生成一个可以由第三方产品加载的xml文件,但是我很难完全按照他们的要求来生成xml.我创建了一个简化版本,仅供测试.

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.

预期输出应如下:

<?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>

我得到的输出如下:

<?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>

我无法理解的2个方面是名称空间分配了一个值,而null标签在不添加以下内容的情况下会自动关闭:xmlns:xsi ="http://www.w3.org/2001 /XMLSchema-instance"xsi:nil =" true.我已经尝试过空标签,但是它们被视为无效的xml而被拒绝.

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.

我的代码如下:

主要:

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

    }
}

程序类:

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

}

编辑类:

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


}

技术等级:

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类:

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


}

推荐答案

不幸的是,我最终不得不按如下所述操作(破解)xml:

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;

这篇关于JAXB提供空XML元素作为&lt; xmlelement/&gt;并删除命名空间名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆