用Java解析XML多行字符串 [英] Parse XML multi line string in Java

查看:227
本文介绍了用Java解析XML多行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用经典DOM解析Java中的多行XML属性.解析工作正常.但是,它破坏了换行符,因此,当我渲染解析的字符串时,换行符将被简单的空格代替.

I'm trying to parse a multi line XML attribute in Java using the classic DOM. The parsing is working just fine. However, it's destroying the line breaks so, when I render my parsed string, line breaks get replaced by simple spaces.

<string key="help_text" value="This is a multi line long
                               text. This should be parsed
                               and rendered in multiple lines" />

要获取我正在使用的属性:

To get the attribute I'm using:

attributes.getNamedItem("value").getTextContent()

如果我只是使用"\ n"将手动键入的字符串传递给render方法,则文本将按预期绘制.

If I just pass a manually typed string to the render method using "\n", the text gets drawn as intended.

有什么想法吗?

推荐答案

我过去曾经为此使用过JDom.它在解码多行属性时为您省去了很多麻烦,并真正增强了Java上的XML解析/编写. JDom还与Android开发兼容,并且非常小(只有一个jar文件).

I've used JDom for this on the past. It saves you a lot of trouble when decoding multilined attributes and really enhances XML parsing/writing on Java. JDom is also compatible with Android development and it's really tiny (only one jar file).

https://github.com/hunterhacker/jdom

这篇关于用Java解析XML多行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆