0
我正在編寫一個實用程序來集成內部系統和第三方產品。我正在嘗試生成一個可以由第三方產品加載的xml文件,但我無法完全按照需要生成xml。我創建了一個僅用於測試的簡化版本。JAXB提供空的XML元素爲<xmlelement/>並刪除命名空間名稱
預期的輸出應該如下:
<?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個區,我不能得到正確的是具有命名空間一個賦值給它的值,並且空標籤在不添加以下情況下自關閉:xmlns:xsi =「http://www.w3.org/2001/XMLSchema-instance」xsi:nil =「true」。我試過空標籤,但他們被拒絕爲無效的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類:
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;
}
}
你嘗試從''刪除=的nillable TRUE' @ XmlElement'註解? – Vadim
我嘗試過以及各種組合,但通過在編輯類中刪除nillable = true,xml中缺少空值,如下所示:<?xml version =「1.0」encoding =「UTF-8」standalone =「是 「> 系列1 SeriesTitle> 測試程序 ProgrammeTitle> 第1集 EpisodeTitleNumber> ... –
karen
有誰知道另一個XML庫,我可以使用,而不是JAXB,可以提供我需要的結果嗎? – karen