Jax-b 文字 xml 序列化

发布于 2025-01-05 22:12:50 字数 1385 浏览 1 评论 0原文

我有一个 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);

编辑-->这就是我完成此操作的方法

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

I have a pojo, which is annotated with JAX-B annotations. I use the setters and getters to populate the object. I use the marshaller to write out the xml to a document, which eventually is written by another api to an outputstream.

Object o = new Object('blah','blah','blah');
Document doc = db.newDocument();
marshaller.marshal(o, doc);

however, I have a string of xml that I need to set as an attribute on one of my pojo fields, but I need it to be marshalled as xml, not as a string. It is xhtml, so I know the format. How can I go about doing this? I do have an xsd, but obviously there is no "type" for 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--> this is how I accomplished this

 <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>

And the generated java code is used like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2025-01-12 22:12:50

我可能会修改用于描述内容模型的 XML 架构,以允许 xsd:any 您希望在其中注入 xhtml 内容。如果您希望进一步限制该位置允许的元素,您可以将 xsd:any 限制为仅匹配 XHTML 命名空间。

然后,您只需遵循 xsd:any on JAXB 实现规则,输出将以 XML 形式发送。

如果您不是从 XSD 开始,那么上面的链接还显示了您需要用来描述 xsd:any 的等效项的注释。

@XmlAnyElement
public List<Element> getAny();

其中 Element 是 org.w3c.dom.Element。

I would probably modify the XML Schema used to describe your content model to allow for an xsd:any where you wish to inject your xhtml content. You can restrict the xsd:any to match only the XHTML namespace, if you wish to further restrict the allowed elements at that location.

You would then simply have to follow the xsd:any on JAXB implementation rules and the output will be sent as XML.

If you don't start from an XSD, then the above link also shows the annotation you need to use to describe the equivalent of the xsd:any.

@XmlAnyElement
public List<Element> getAny();

where Element is org.w3c.dom.Element.

北凤男飞 2025-01-12 22:12:50

最好的选择可能是将 xml 标记的内容包含在 CDATA 块中。您可以通过配置 JaxB 的 OutputFormat 来做到这一点:

OutputFormat of = new OutputFormat();
of.setCDataElements(new String[] { "thisXmlField"});
XMLSerializer serializer = new XMLSerializer(of);
marshaller.marshal(o, serializer.asContentHandler());

Your best bet is probably to enclose the content of your xml tag in a CDATA block. You can do that by configuring the OutputFormat for JaxB:

OutputFormat of = new OutputFormat();
of.setCDataElements(new String[] { "thisXmlField"});
XMLSerializer serializer = new XMLSerializer(of);
marshaller.marshal(o, serializer.asContentHandler());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文