XML中的不需要的元素通过XSTREAM

我是XStream的新手

我跟随DTO

@XStreamAlias("outline") public class OutlineItem implements java.io.Serializable { private static final long serialVersionUID = -2321669186524783800L; @XStreamAlias("text") @XStreamAsAttribute private String text; @XStreamAlias("removeMe") private List childItems; } 

我曾经做过

 XStream stream = new XStream(); stream.processAnnotations(OutlineItem.class); stream.toXML(outlineItem); 

我得到这个作为我的输出文本

                      

而我希望输出为:

             

任何帮助将不胜感激! 不确定是否需要某种XSLT ……

  • 沙阿

注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB( JSR-222 )专家组的成员。


我相信答案是:

 @XStreamImplicit(itemFieldName="outline") private List childItems; 

您是否考虑过使用JAXB实现( Metro , MOXy , JaxMe ,…)?

OutlineItem

 import javax.xml.bind.annotation.*; @XmlRootElement(name="outline") @XmlAccessorType(XmlAccessType.FIELD) public class OutlineItem implements java.io.Serializable { private static final long serialVersionUID = -2321669186524783800L; @XmlAttribute private String text; @XmlElement("outline") private List childItems; } 

演示

 import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(OutlineItem.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(outlineItem, System.out); } }