使用 xstream 将 XML 的一部分解组为字符串
我正在使用 xstream 来编组/解组 xml。我有以下 xml 片段,我想将节点“rawText”内容作为字符串存储在 Form.java bean 上。
<FormData>
<form id="1">
<rawText>
<h1>All form submitted data goes here</h1>.
<clob> This can contain more 'xml' like data with more nodes </clob>
</rawText>
</form>
</FormData>
Form.java
class Form{
private int id;
private String rawText;
//getters + setters
}
因此,在上面的示例中,我希望将以下内容填充到 Form bean 上的 rawText 字段中。我该如何实现这一目标?
<h1>All form submitted data goes here</h1>.
<clob> This can contain more 'xml' like data with more nodes </clob>
I am using xstream to marshal/unmarshal xml. I have following xml fragment for which I would like to store the node 'rawText' contents as a string on Form.java bean.
<FormData>
<form id="1">
<rawText>
<h1>All form submitted data goes here</h1>.
<clob> This can contain more 'xml' like data with more nodes </clob>
</rawText>
</form>
</FormData>
Form.java
class Form{
private int id;
private String rawText;
//getters + setters
}
So in above example, I would like to get following content populated into rawText field on Form bean. How do I achieve this?
<h1>All form submitted data goes here</h1>.
<clob> This can contain more 'xml' like data with more nodes </clob>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您应该构建一个自定义转换器并处理 reader.getValue() 来连接您的文本,因为它不会按您想要的方式返回整个文本。
检查以下代码,大部分借自 这个问题。
转换器:
表单类:
仅管理根节点:
测试:
输出:
再见!
As far as I know you should build up a custom converter and handle the reader.getValue() to concatenate your text, because it does not return the whole text as you want.
Check the following code most borrowed from this question.
Converter:
Form class:
Just to manage the root node:
To test:
Output:
See you!