【发布时间】:2012-02-14 17:15:40
【问题描述】:
我有一个 pojo,它使用 JAX-B 注释进行了注释。我使用 setter 和 getter 来填充对象。我使用编组器将 xml 写入文档,最终由另一个 api 写入输出流。
Object o = new Object('blah','blah','blah');
Document doc = db.newDocument();
marshaller.marshal(o, doc);
但是,我有一个 xml 字符串,我需要将其设置为我的一个 pojo 字段的属性,但我需要将其编组为 xml,而不是字符串。它是 xhtml,所以我知道格式。我该怎么做呢?我确实有一个 xsd,但显然 xml 没有“类型”。
//need to do this
String xml = <tag>hello</tag>;
Object o = new Object('blah','blah','blah');
o.setThisXmlField(xml);
marshaller.marshal(o, doc);
edit-->我就是这样完成的
<xs:element name="course">
<xs:complexType>
<xs:sequence>
<xs:element name="courseSummary" type="courseSummary"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="courseSummary">
<xs:sequence>
<xs:any/>
</xs:sequence>
</xs:complexType>
生成的java代码使用如下:
Tidy tidy = new Tidy();
tidy.setXHTML(true);
String courseSummary = "some turruble xml <b>REALLY RUBBISH</li>";
Course c = new Course();
Document courseSummaryDoc = tidy.parseDOM(IOUtils.toInputStream(courseSummary),null);
CourseSummary summary = new CourseSummary();
summary.setAny(courseSummaryDoc.getDocumentElement());
c.setCourseSummary(summary);
【问题讨论】:
标签: java xml xsd xml-serialization jaxb