Custom XML-friendly printer for java

I need to format an xhtml file that contains some elements included in another namespace.

I searched all day for a really good mechanism for beautifully printing an XML file in java. I tested

  • dom4j
  • jdk (xerxes)
  • my own xslt file
  • jtidy

I am missing a really good formatting with the following options:

  • setting line width
  • wrapping attributes if maximum line width is reached
  • alternative: align all attributes
  • for empty elements, to include a space before closing the <br/>vs tag<br />
  • indicate the names of the elements where empty space does not matter

After my research, I came to the conclusion that to run my own xslt stylesheet, but I think that you cannot do 2. or 3. And I also did not succeed with 4 ..

Of course, the line width can be forced when white space is significant and so on. But is there any library that can do what I want?

Do you have any suggestions for me?

UPDATE: I am really looking for a network for a good xml beautifier / pretty printer. Nothing found. Eclipse does a good job, but I don't know how to extract a component from its core. There should be something like that. At the moment I am writing my own: - (

Eclipse has many options:

enter image description here

Something like this as a library would be nice.

+5
3
+2

Java SAX . ​​

XML.

    SAXReader reader = new SAXReader();
        Document doc = reader.read(new ByteArrayInputStream(XMLSTRING.getBytes()));


        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        OutputFormat outputFormat = OutputFormat.createCompactFormat();

        // Out formatted XML
        outputFormat = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(baos, outputFormat);
        writer.write(doc);
        writer.close();

        String formatedXML = new String(baos.toByteArray());
+3

I always knew ' Tidy ' to be a good tool for these situations.

0
source

All Articles