Java 通过XSTREAM在XML中添加不需要的元素
我是XStream的新手 我有以下的DTOJava 通过XSTREAM在XML中添加不需要的元素,java,xml,xslt,xstream,Java,Xml,Xslt,Xstream,我是XStream的新手 我有以下的DTO @XStreamAlias("outline") public class OutlineItem implements java.io.Serializable { private static final long serialVersionUID = -2321669186524783800L; @XStreamAlias("text") @XStreamAsAttribute private String t
@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<OutlineItem> childItems;
}
我把它作为我的输出文本
<outline text="Test">
<removeMe>
<outline text="Test Section1">
<removeMe>
<outline text="Sub Section1 1">
<removeMe/>
</outline>
<outline text="Sub Section1 2">
<removeMe/>
</outline>
</removeMe>
</outline>
<outline text="Test Section 2">
<removeMe>
<outline text="Test Section2 1">
<removeMe/>
</outline>
</removeMe>
</outline>
</removeMe>
</outline>
鉴于我希望输出为:
<outline text="Test">
<outline text="Test Section1">
<outline text="Sub Section1 1">
</outline>
<outline text="Sub Section1 2">
</outline>
</outline>
<outline text="Test Section 2">
<outline text="Test Section2 1">
</outline>
</outline>
</outline>
任何帮助都将不胜感激!不确定是否需要某种XSLT
- 沙阿
我相信答案是:
@XStreamImplicit(itemFieldName="outline")
private List<OutlineItem> childItems;
谢谢你的解决方案。它工作得很好。我还将研究JAXB。
@XStreamImplicit(itemFieldName="outline")
private List<OutlineItem> childItems;
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<OutlineItem> 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);
}
}