JDOM:从字符串创建元素,其中包含 xml 代码
我正在使用 jdom 将一些文本输入解析为 xml。它的结构如下:
<item>
<text>Some description</text>
<options .... />
</item>
<item>
<text>Some description 2</text>
<options .... />
</item>
然后,我需要一个常规的 w3c.dom 对象。我正在使用以下代码:
DOMOutputter domOutputter = new DOMOutputter();
org.w3c.dom.Document w3cDoc = domOutputter.output( doc );
我正在使用以下代码创建元素:
Element desc = new Element("tekst");
String description = //some function returning string
desc.addContent(description);
e.setContent(desc);
其中 e 是 new Element("item")
它工作正常,但问题是在“描述”中用户插入一些 xml 标签时。我愿意处理它。我的输出如下:
<text><bold>description</bold></text>
有没有办法在创建元素时自动解析这些标签? 从 w3cDoc 中,我使用 DOMSource 创建源,并且需要它进行进一步转换。也许我应该更换转义括号?
__
dce
I'm parsing some text input to xml using jdom. It have structure like:
<item>
<text>Some description</text>
<options .... />
</item>
<item>
<text>Some description 2</text>
<options .... />
</item>
Then, I need a regular w3c.dom object. I'm using this code:
DOMOutputter domOutputter = new DOMOutputter();
org.w3c.dom.Document w3cDoc = domOutputter.output( doc );
I'm creating element with this code:
Element desc = new Element("tekst");
String description = //some function returning string
desc.addContent(description);
e.setContent(desc);
Where e is new Element("item")
It works fine, but problem is when in "description" user inserts some xml tags. I'd like to handle it. I have output like:
<text><bold>description</bold></text>
Is there any way to automaticaly parse those tags when creating Element?
From the w3cDoc I'm creating Source with DOMSource and it's needed for further transformation. Maybe there should I replace escaped brackets?
__
dce
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过 http://www.jdom.org/docs /apidocs/org/jdom/output/XMLOutputter.html
特别位于
http://www.jdom.org /docs/apidocs/org/jdom/output/XMLOutputter.html#escapeAttributeEntities(java.lang.String)
http://www.jdom.org /docs/apidocs/org/jdom/output/XMLOutputter.html#escapeElementEntities(java.lang.String)
Have you looked at http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html
specifically at
http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html#escapeAttributeEntities(java.lang.String)
http://www.jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html#escapeElementEntities(java.lang.String)