Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse XML formatter

What is a free alternative to the built-in Eclipse XML editor?

The one packaged with the IDE has two major problems with formatting:

  1. The formatter tries to parse escaped char's as though they were unescaped. E.g. '&lt;' is treated like '<' which causes the formatter to "stop".
  2. White space between elements is not honored:

    <foo>   text </foo>
    

    will be formatted to:

    <foo>text</foo>
    

We are using Eclipse 3.4.

Updates

Issue #1 is a known bug: Formatting issues with entities in XML files.

Apparently the white space formatting is intended functionality. I have requested this be provided as an option or advise on fixing this in a plugin of my own, but as of yet, no answer.

like image 970
javamonkey79 Avatar asked Jan 23 '09 03:01

javamonkey79


2 Answers

This is way less than ideal, and I hope someone has a better solution, but it works:

Build a simple App using dom4j:

public static void main( final String[] args ) throws Exception {
    String text = FileUtils.readFileToString( new File( args[ 0 ] ) );
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText( false );

    XMLWriter writer = new XMLWriter( System.out, format );
    writer.write( DocumentHelper.parseText( text ) );
}

Create a runnable jar, (optional) batch script, and add as an external tool. Of course, you could try XMLTidy or some other command line XML formatter, but I have had better results with dom4j.

like image 196
javamonkey79 Avatar answered Sep 21 '22 01:09

javamonkey79


I like Notepad++ with the XML Tools plugin. It does XSLT, has an XPATH expression evaluator, and does DTD and schema validation. Toolchain download links:

  • Notepad++: http://sourceforge.net/project/downloading.php?group_id=95717&use_mirror=internap&filename=npp.5.0.Installer.exe&24657954
  • XML Plugins: http://sourceforge.net/project/downloading.php?group_id=189927&use_mirror=voxel&filename=xmltools_2.3.1_r639.zip&44936797
  • External Libraries: http://sourceforge.net/project/downloading.php?group_id=189927&use_mirror=internap&filename=ext_libs.zip&52274848
like image 26
Stephen Friederichs Avatar answered Sep 21 '22 01:09

Stephen Friederichs