How to display XML source in a text area view with syntax color highlighting in 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.
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 dispalying the entire XML file contents as it is in the file in the GUIGraphical User Interface - The visual front-end of your application through which the user interacts with your application. Check out this Swing Java tutorial for basic GUI programming..
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:
- Read contents of the XML file
- Store the contents of the file as a StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String. object
- set the String as the JTextArea's text
How to read contents of a file?
see this article "Reading from a character based file using the FileReader"
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.
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 and attach the "RSyntaxTextArea.jar" to your java project.
- RSyntaxTextArea extends JTextArea, so just as explained earlier, read the XML file and set the contents String object as the text area's text.
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.
* see article <a href="/drupal/ReadingACharacterFileUsingFileReader" target="_blank">"Reading from a character based file using the FileReader"</a> or
* you can write your own method
*/
String contents = readFile("c:\projects\test.xml");
rsyntaxtextarea.setText(contents);
- 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 u. We will update you with more articles on how to change styles and full code listing soon.








Comments
thank you
very usefull explanation. Thank you.
bnatey
توبيكات
توبيكات توبيكات
توبيكات اسلاميه
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه
توبيكات كريسماس
توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك
توبيكات
توبيكات
توبيكات اسلاميه
توبيكات حزينه
توبيكات حب
توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني
توبيكات منوعه
توبيكات مضحكه
توبيكات عتاب
توبيكات ساخره
توبيكات شعريه
توبيكات اجنبيه
توبيكات نكت
توبيكات جوال
توبيكات سعوديه
توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات
توبيكات
توبيكات فلاش
توبيكات روعه
توبيكات رائعه
منتديات
دردشة
العاب
العاب بنات
شات
توبيكات مصريه
توبيكات قطريه
دليل مواقع
تحميل صور
مركز تحميل
برامج
كأس العالم
كأس العالم 2010
العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات
Post new comment