1

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.