unknown
unknown

Reputation: 5017

Format attributes for XML in Pretty format in java

I am trying to format XML string to pretty. I want all the attributes to be printed in single line. XML input:

<root><feeds attribute1="a" attribute2="b" attribute3="c" attribute4="d" attribute5="e" attribute6="f"> <id>2140</id><title>gj</title><description>ghj</description>
<msg/>

Expected output:

<root>
<feeds attribute1="a" attribute2="b" attribute3="c" attribute4="d" attribute5="e" attribute6="f">
    <id>2140</id>
    <title>gj</title>
    <description>ghj</description>
    <msg/>
</feeds>

Actual Output:

<root>
<feeds attribute1="a" attribute2="b" attribute3="c" attribute4="d"
    attribute5="e" attribute6="f">
    <id>2140</id>
    <title>gj</title>
    <description>ghj</description>
    <msg/>
</feeds>

Here is my code to format xml. I have also tried SAX parser. I don't want to use DOM4J.

public static String formatXml(String xml) {
  DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
  DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
  LSSerializer writer = impl.createLSSerializer();
  writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
  writer.getDomConfig().setParameter("xml-declaration", false);
  writer.getDomConfig().setParameter("well-formed", true);

  LSOutput output = impl.createLSOutput();
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  output.setByteStream(out);

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  InputSource is = new InputSource(new StringReader(xml));

  writer.write(db.parse(is), output);
  return  new String(out.toByteArray());
}

Is there any way to keep attributes in one line with SAX or DOM parser? I am not looking for any additional library. I am looking for solution with java library only.

Upvotes: 1

Views: 518

Answers (1)

queeg
queeg

Reputation: 9463

A SAX or DOM parser will read your input string and allow your application to understand what was passed in. At some point in time your application then writes out that data, and that is the moment where you decide to insert additional whitespace (like linefeeds and tab characters) to pretty-print the document.

If you really want to use SAX and make the parser efficient the best you could do is write the document while it is being parsed. So you would implement the ContentHandler interface (https://docs.oracle.com/en/java/javase/11/docs/api/java.xml/org/xml/sax/ContentHandler.html) such that it directly writes out the data while adding linefeeds where you feel they belong to.

Check this tutorial to see how the ContentHandler can then be applied in a SAX parser: https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

Upvotes: 1

Related Questions