使用 Java 方法格式化 HTML 页面上的文本/XML 输出

发布于 2024-12-13 08:33:27 字数 829 浏览 2 评论 0 原文

我正在从 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?

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

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

发布评论

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

评论(1

小猫一只 2024-12-20 08:33:28

有 2 个通用 API 可用于处理 JRE 中内置的 XML - DOM 和 SAX。

这里有一个快速而肮脏的技巧,可以做类似您使用 SAX 请求的事情:

public class Main {
private final static String input = "<TXNURN>3505</TXNURN><CH></CH><REQ>N</REQ><DOB></DOB><QT>2</QT><DR>TAGER00</DR><NUMBER>N</NUMBER>";

public static void main(String[] args) throws Exception {
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final StringBuilder output = new StringBuilder();
    parser.parse(new StringBufferInputStream("<root>"+input+"</root>"), new DefaultHandler() {
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if ("root".equals(qName)) return;
            output.append(qName).append(":");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("root".equals(qName)) return;
            output.append("\n");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            output.append(" ").append(ch, start, length);
        }
    });

    System.out.println(output.toString());
}

为了简洁起见,我使用了一种已弃用的(并且有充分理由!)方法从字符串中获取输入流,您可能想要生成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:

public class Main {
private final static String input = "<TXNURN>3505</TXNURN><CH></CH><REQ>N</REQ><DOB></DOB><QT>2</QT><DR>TAGER00</DR><NUMBER>N</NUMBER>";

public static void main(String[] args) throws Exception {
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final StringBuilder output = new StringBuilder();
    parser.parse(new StringBufferInputStream("<root>"+input+"</root>"), new DefaultHandler() {
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            if ("root".equals(qName)) return;
            output.append(qName).append(":");
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("root".equals(qName)) return;
            output.append("\n");
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            output.append(" ").append(ch, start, length);
        }
    });

    System.out.println(output.toString());
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文