我正在写一个实用程序之间的集成内部系统和第三方产品。我试图生成一个可以由第三方产品加载的xml文件,但我很难按照他们的要求生成xml。我已经创建了一个简化的版本,只是为了测试。
预期产出应如下:
<?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>
我的代码如下:
主要:
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();
}
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:
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;
我使用CXF从WSDL/XSD生成java类,然后返回XML(用于JMS)。 在生成的一个类中,它表示:
供应商提供的XML如下: 请注意,没有声明,供应商也没有提供模式。这不能更改,供应商将来会继续这样发布XML。 为了生成JAXB绑定,我创建了如下模式: 请注意,我已经声明了一个或多或少有意义的命名空间(“http://acme.com/schema”),以便它可以用于元素引用等。XJC 生成以下: 然后,我尝试解组XML文档: 我得到的例外是: 显然,这是因为XML元素属于一个空的名称空间,而J
我有几个关于JAXB编组的简单问题。我正在尝试封送包含以下字段的类: 只需使用以下序列化代码: 我得到的输出是: 现在,我面临的问题如下: > 我想要名称空间xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“出现在根元素中,即TokenRestrictionTemplate中,而不是单个子元素中。如何实现这一点? 我有一些元素,例如带有@Xm
问题内容: 我喜欢ElementTree解析xml的方式,特别是Xpath功能。我从带有嵌套标签的应用程序以xml输出。 我想按名称访问此标签,而不指定名称空间,这可能吗?例如: 代替: 问题答案: 至少使用lxml2,可以稍微减少此开销:
主要内容:命名冲突,使用前缀来避免命名冲突,XML 命名空间 - xmlns 属性,统一资源标识符(URI,全称 Uniform Resource Identifier),默认的命名空间,实际使用中的命名空间XML 命名空间提供避免元素命名冲突的方法。 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。 这个 XML 携带 HTML 表格的信息: <table> <tr> <td>Apples</td> <td>Bananas</td> <
我试图从通过JAXB生成的类序列化XML。 班级: 文件“package-info.java”: 编组器(简化,无需错误处理): 此代码生成: 我期待这样的事情: 我有一个类似的代码并且工作正常,但是我不明白为什么这个代码不显示命名空间。有线索吗?谢谢!