私は内部システムとサードパーティ製品を統合するユーティリティを作成しています。私は、サードパーティの製品で読み込むことができる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;
}
}
あなたは 'から' nil可能= true'をを削除しようとしませんでした@ XmlElement'アノテーション? – Vadim
私はこれとさまざまな組み合わせを試しましたが、編集クラスのnillable = trueを削除すると、ここに示すようにxmlにnull値がありません:<?xml version = "1.0" encoding = "UTF-8" standalone = "はい "> シリーズ1 SeriesTitle> テストプログラム ProgrammeTitle> エピソード1 EpisodeTitleNumber> ... –
karen
誰もが私は必要な結果を提供する可能性が代わりにJAXBの私が使用することができ、他のXMLライブラリを知っていますか? – karen