【问题标题】:Xstream - Remove value tagXstream - 删除值标签
【发布时间】:2015-02-26 13:55:52
【问题描述】:

我有以下 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标签的代码(这只是一个sn-p..):

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);
}

【问题讨论】:

    标签: java xml xstream


    【解决方案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 显式提供)。

    【讨论】:

    • 类已生成,我无法更改它。
    • @GregD 很公平,在这种情况下,只需使用registerConverter 的第二个注册机制,则无需对类进行任何更改。
    【解决方案2】:

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

    看看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;
    }
    
    }
    

    完成任务

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2012-05-30
      相关资源
      最近更新 更多