我正在从 XML 格式的数据库中检索一个字符串值 字符串的示例:
<TXNURN>3505</TXNURN><CH></CH><REQ>N</REQ><DOB></DOB><QT>2</QT><DR>TAGER00</DR><NUMBER>N</NUMBER>
在网页中,它显示如下(当然,因为 XML 标签在 HTML 页面中不可见):
3505N2TAGER00N
我想做的是创建一个 java 类/函数,它将接受字符串,并对其进行格式化,以便 HTML 页面上的输出看起来像这样:
TXNURN: 3505
CH:
REQ: N
DOB:
QT: 2
DR: TAGER00
NUMBER: N
我知道有很多方法可以格式化字符串和字符,但我就是看不到让我思考如何有效地使用它们来获得我想要的结果。我不需要格式化你看到的字符串...
任何帮助将不胜感激!
提前致谢。
-------------------------------------------------- - - - - 更新 - - - - - - - - - - - - - - - - - - - - - ----------------------
我现在明白了,我真正要做的就是在每个结束标记后添加一个换行符。
但问题仍然是……如何?
I'm retrieving a String value from a database that is in XML format an example of the String:
<TXNURN>3505</TXNURN><CH></CH><REQ>N</REQ><DOB></DOB><QT>2</QT><DR>TAGER00</DR><NUMBER>N</NUMBER>
In the webpage it is displayed like this(since of course XML tags are invisible in HTML page):
3505N2TAGER00N
What I would like to do, is create a java class/function that will take the String, and format it so that the output on the HTML page would look something like this:
TXNURN: 3505
CH:
REQ: N
DOB:
QT: 2
DR: TAGER00
NUMBER: N
I know there are plenty of methods that can format Strings and characters, but I just can't seem to wrap my head around on how to use them effectively in getting the result that I want. I don't ever have to format Strings you see...
Any help would greatly be appreciated!
Thanks in advance.
---------------------------------------------------------- Update ---------------------------------------------------------------
I see now that all I realy have to do is put a line feed after every closing tag .
But the question remains... How?
发布评论
评论(1)
有 2 个通用 API 可用于处理 JRE 中内置的 XML - DOM 和 SAX。
这里有一个快速而肮脏的技巧,可以做类似您使用 SAX 请求的事情:
为了简洁起见,我使用了一种已弃用的(并且有充分理由!)方法从字符串中获取输入流,您可能想要生成HTML(否则这将在一行中呈现为 HTML 忽略格式),但我希望您能进行一般的图片
编辑:可能有一种更短更奇特的方法来执行此操作,使用 XSLT 转换将 XML 转换为 HTML(这是 xml for 你想要什么)。太糟糕了,我不知道如何足够快地制作出这样的东西
there're 2 common APIs for working with XML built-in to the JRE - DOM and SAX.
here's a quick and dirty hack to do something like what you requested using SAX:
i've used a deprecated (and for a good reason!) method for getting an input stream from a string for brevity, and you'll probably want to produce HTML (otherwise this would be rendered in one line as HTML ignored formatting), but i hope you get the general picture
edit: there's probably a shorter fancier way to do this using an XSLT transform to convert your XML into HTML (which is xml for what you want). too bad i dont know how to whip up something like that quick enough