如何使用 JAXB 为 XML 中的空元素生成结束标记
How to generate end tag for empty element in XML using JAXB
我正在使用 JAXB 生成 XML。但是 JAXB 正在生成一个空标签,将其自行关闭。但我的客户想要单独的空标签。我知道两者是平等的,但他不同意我的看法。请任何人提出解决方案。谢谢。
示例代码:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"currencyCode",
"discountValue",
"setPrice",
"spendLowerThreshold",
"spendUpperThreshold",
"discountApportionmentPercent",
"discountApportionmentValue"
})
@XmlRootElement(name = "countryData")
public class CountryData {
protected String currencyCode;
protected String discountValue = "";
protected String setPrice = "";
protected String spendLowerThreshold = "";
protected String spendUpperThreshold = "";
protected String discountApportionmentPercent = "";
protected String discountApportionmentValue = "";
// Setters and Gettres
}
实际输出:
<currencyCode>GBP</currencyCode>
<discountValue/>
<setPrice/>
<spendLowerThreshold/>
<spendUpperThreshold/>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue/>
预期输出:
<currencyCode>GBP</currencyCode>
<discountValue></discountValue>
<setPrice></setPrice>
<spendLowerThreshold></spendLowerThreshold>
<spendUpperThreshold></spendUpperThreshold>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue></discountApportionmentValue>
编组代码:
try {
Marshaller marshaller = JAXBContext.newInstance(CountryData.class).createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshaller.marshal(countryData , os);
log.debug("The PPV request raw XML -> " + os.toString());
} catch (JAXBException e) {
// nothing to do
}
我正在使用 JDK 6.0
如果你已经从XSD then you would also generated ObjectFactory class. If don't please refer here生成了类关于如何生成ObjectFactoryclass。
在那之后,你的代码就像--
JAXBContext context;
context = JAXBContext.newInstance(*yourClass*.class);
final ObjectFactory objFactory = new ObjectFactory();
final JAXBElement<YourClass> element = objFactory
.*autoGeneratedmethodfromObjectFactorytogetelement*;
Marshaller marshaller;
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
final StringWriter stringWriter = new StringWriter();
marshaller.marshal(element, stringWriter);
String message = stringWriter.toString();
这将为您提供所需的输出。
此行为取决于实现。我无意中遇到了相反的问题(我需要自闭标签),我在 this way.
You can try using this version of marshal()
中解决了问题或修改了链接答案中的代码。
问题已解决,
要强制关闭标签,只需添加一个空字符串作为该标签的值!
您可以使用 @XmlValue 来做到这一点:
public class closedTag {
@XmlValue
private String content;
public closedTag() {
this.content = "";
}
}
我正在使用 JAXB 生成 XML。但是 JAXB 正在生成一个空标签,将其自行关闭。但我的客户想要单独的空标签。我知道两者是平等的,但他不同意我的看法。请任何人提出解决方案。谢谢。
示例代码:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"currencyCode",
"discountValue",
"setPrice",
"spendLowerThreshold",
"spendUpperThreshold",
"discountApportionmentPercent",
"discountApportionmentValue"
})
@XmlRootElement(name = "countryData")
public class CountryData {
protected String currencyCode;
protected String discountValue = "";
protected String setPrice = "";
protected String spendLowerThreshold = "";
protected String spendUpperThreshold = "";
protected String discountApportionmentPercent = "";
protected String discountApportionmentValue = "";
// Setters and Gettres
}
实际输出:
<currencyCode>GBP</currencyCode>
<discountValue/>
<setPrice/>
<spendLowerThreshold/>
<spendUpperThreshold/>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue/>
预期输出:
<currencyCode>GBP</currencyCode>
<discountValue></discountValue>
<setPrice></setPrice>
<spendLowerThreshold></spendLowerThreshold>
<spendUpperThreshold></spendUpperThreshold>
<discountApportionmentPercent>0.0</discountApportionmentPercent>
<discountApportionmentValue></discountApportionmentValue>
编组代码:
try {
Marshaller marshaller = JAXBContext.newInstance(CountryData.class).createMarshaller();
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshaller.marshal(countryData , os);
log.debug("The PPV request raw XML -> " + os.toString());
} catch (JAXBException e) {
// nothing to do
}
我正在使用 JDK 6.0
如果你已经从XSD then you would also generated ObjectFactory class. If don't please refer here生成了类关于如何生成ObjectFactoryclass。
在那之后,你的代码就像--
JAXBContext context;
context = JAXBContext.newInstance(*yourClass*.class);
final ObjectFactory objFactory = new ObjectFactory();
final JAXBElement<YourClass> element = objFactory
.*autoGeneratedmethodfromObjectFactorytogetelement*;
Marshaller marshaller;
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
final StringWriter stringWriter = new StringWriter();
marshaller.marshal(element, stringWriter);
String message = stringWriter.toString();
这将为您提供所需的输出。
此行为取决于实现。我无意中遇到了相反的问题(我需要自闭标签),我在 this way.
You can try using this version of marshal()
中解决了问题或修改了链接答案中的代码。
问题已解决, 要强制关闭标签,只需添加一个空字符串作为该标签的值! 您可以使用 @XmlValue 来做到这一点:
public class closedTag {
@XmlValue
private String content;
public closedTag() {
this.content = "";
}
}