Display XML Source View with syntax Color highlighting using Java

This article will take you through how one can display an XML document or file contents as a source view with syntax color highlighting in a Java program. There are some open source products available to solve this purpose. This article will focus on one particular open source product that is new and sounds promising.

Display XML Source View with syntax Color highlighting using Java

How to display raw XML source in a text area view with syntax color highlighting in Java?

Introduction

XML Documents can be displayed in a Java program in two main representations:

  • Tree view
  • Source view

Tree view is one of the most common ways used with node expansion mechanism. Source view is purely displaying the entire XML file contents as it is in the file in the GUI.

Displaying XML in Source View

The simplest and straightforward way to display XML in source view is to use a JTextArea. The steps are as simple as listed below:

  1. Read contents of the XML file
  2. Store the contents of the file as a String object
  3. set the String as the JTextArea's text

How to read contents of a file?

see this article"How to read from a character based file using File Reader?"

How to display the contents in a TextArea?

The contents read from a file should be stored in a String object and simply set the resulting object as the JTextArea's text.

     JTextArea textArea = new JTextArea();    
     textArea.setText(String); 

XML Syntax color highlighting in Source View

Adding the contents of an XML file to a textarea will display the XML syntax as plain black text. To color highlight different tokens (Node elements, attributes, attribute values etc)

There are two opensource products I found that fulfill this requirement:

  • IBM XMLSourceView
  • Fifesoft - RSyntaxTextArea

Some differences between XMLSourceView & RSyntaxTextArea

XMLSourceView RSyntaxTextArea
IBM product Fifesoft product
Open source product Open source product
inacitve- project is now closed
Very much active
provides Syntax Color highlighting for XML provides Syntax Color highlighting for over 20 languages
Main disadvantage is that the project is closed, further to that it is limited to small sized documents. struggles to display for larger ones produces optimum results even for large files.

RSyntaxTextArea

"RSyntaxTextArea is a syntax highlighting text component for Java Swing. It extends JTextComponent so it integrates completely with the standard javax.swing.text package. It is fast and efficient, and can be used in any application that needs to edit or view source code. " - www.fifesoft.com

Apart from color coding, it also provides the following options with the xml syntax displayed in the text area:

  • cut
  • copy
  • paste
  • delete
  • undo
  • redo
  • select all

How to use RSyntaxTextArea for XML syntax highlighting?

Steps:

  • Download the "RSyntaxTextArea.jar" to your java project and add to build path.
  • RSyntaxTextArea extends JTextArea, so just as explained earlier, read the XML file and set the contents String object as the text area's text.
// Syntax highlighting text component for Java Swing
RSyntaxTextArea rsyntaxtextarea = new RSyntaxTextArea();
 
/** As this product can highlight over 20 languages, 
we need to specify that we want to highlight XML source*/  
rsyntaxtextarea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
 
 /** read contents of your XML file - readFile() is a custom written method. 
 you can write your own method*/
 String contents = readFile("c:\projects\test.xml");
 
 rsyntaxtextarea.setText(contents);

If you want to know how to read a file, see article "How to read from a character based file using File Reader?"

  • This will automatically display the XML source color highlighted with the default colors. We can change the fonts, colors and styles according to our needs with their provided methods.

To download RSyntaxTextArea or get more information about this open source project, visit http://fifesoft.com/rsyntaxtextarea/

Hope this article was useful to you. We will update you with more articles on how to change styles and full code listing soon.

Feedback or Questions?

We welcome feedback and questions and will try our best to attend to it as quickly as possible!

Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.