Xstream - 删除值标记

时间:2015-02-26 13:55:53

标签: java xml xstream

我有以下xsd标记:

<xs:complexType name="documentation">
<xs:simpleContent>
  <xs:extension base="xs:string">
    <xs:attribute type="xs:string" name="language" use="required"/>
  </xs:extension>
</xs:simpleContent>

生成(使用jax-b):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "documentation", propOrder = {
"value"
})
public class Documentation {

  @XmlValue
  protected String value;
  @XmlAttribute(name = "language", required = true)
  protected String language;

我想要一些输出:

<documentation language="NL">SomeValue</documentation>

但是Xstream生成:

<documentation language="NL">
  <value>SomeValue</value>
</documentation>

我该如何删除值标签?我不想要他们..

生成xml标签的代码(这只是一个片段..):

private void createDocumentation(Description description, String docNL) {
    List<Documentation> documentations = description.getDocumentation();
    Documentation documentationNL = new Documentation();
    documentationNL.setLanguage("NL");
    documentationNL.setValue(docNL);
    documentations.add(documentationNL);
}

private void createXmlFile(Description description) {

    XStream xstream = new XStream(new DomDriver());
    xstream.alias("description", Description.class);
    xstream.alias("documentation", Documentation.class);

    xstream.addImplicitCollection(Description.class, "documentation");  
    xstream.useAttributeFor(Documentation.class, "language");

    String xml = xstream.toXML(description);
}

2 个答案:

答案 0 :(得分:1)

XStream提供了一个名为ToAttributedValueConverter的标准转换器实现,您可以为任何简单内容加属性类型连接,如下所示:

@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "value" })
public class Documentation {

  protected String value;
  protected String language;
}

strings注释元素命名与元素内容对应的属性,所有其他属性将成为属性。如果您想使用xstream.registerConverter声明转换器而不是使用XStream注释,那么您可以使用

xstream.registerConverter(new ToAttributedValueConverter(Documentation.class,
  xstream.getMapper(), xstream.getReflectionProvider(), xstream.getConverterLookup(),
  "value"));

MapperReflectionProviderConverterLookup对象在您使用注释注册时会自动提供给转换器,但必须明确提供给registerConverter)。< / p>

答案 1 :(得分:0)

一种选择是为您的Documentation对象创建自定义转换器。

查看XStream Converter tutorial

编辑TS:

中加入:

        xstream.registerConverter(new DocumentationConverter());

public class DocumentationConverter implements Converter {

public boolean canConvert(Class clazz) {
    return clazz.equals(Documentation.class);
}

public void marshal(Object value, HierarchicalStreamWriter writer,
        MarshallingContext context) {
    Documentation documentation = (Documentation) value;
    writer.addAttribute("language", documentation.getLanguage());
    writer.setValue(documentation.getValue());
}

public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {
    Documentation documentation = new Documentation();
    reader.moveDown();
    documentation.setLanguage(reader.getAttribute("language"));
    documentation.setValue(reader.getValue());
    reader.moveUp();
    return documentation;
}

}

完成了这项工作