Home > Java > javaTutorial > Create an XML editor using Java Swing (1)

Create an XML editor using Java Swing (1)

黄舟
Release: 2016-12-20 13:39:45
Original
2209 people have browsed it

I think you must have some understanding of xml. Maybe you are still eager to write an XML text, but there are too few cross-platform, free XML editors available now. So in this article, I want to introduce or take you step by step to develop a simple XML editor. Of course, we will use some of the most common Java 2 Swing components, but these are all free, and some are in the JDK , some can be downloaded from the Internet. I think through this article, you can create an XML editor of your own.
 

  Let me first introduce the idea of ​​​​writing this article. First I want to briefly discuss XML and why tree structures are suitable for displaying XML. Then we will look at how the JAXP API sets up the required XML class environment. Then we will look at JTree for displaying a graphical tree. Swing component; finally, we will create a reusable class that continues the JTree component and can be used to parse an XML document and display the data in a Jtree.

  When it comes to XML (eXtensible Markup Languge), people often think of it as a new markup language used in web browsers, just like Html or CSS. In fact, XML is a data representation language that allows you to use a very efficient way to describe your data. XML enables you to define statements such as "these three Words constitutes a heading." XML allows you to declare any type of data, rather than displaying the data in a web page.

  Please take a look at the following XML example:

<article>
<header>
<title> Create an XML editor using Java Swing
<suBTitle> Part 1</subtitle>
</title>
<author > Wayne </author>
<header>
<content> This is the text </content>
</article>

Please note that these elements are different from standard HTML statements, but they look more like HTML. This is because both XML and HTML are derived from the SGML language. The difference is that HTML has a predefined set of tags, while XML's syntax has a lot of flexibility, allowing you to use expressive tags such as to surround the data. You should also note that all elements are subordinate to the root element (

in the above example), and some elements have their own sub-elements, such as which is a sub-element of . This way of organizing data has three benefits: the data can be more expressive, the data is easier to maintain, and the data is easier to display as a tree structure. This is why we use JTree objects to display XML data. If you want to have a deeper understanding of XML, please refer to the relevant tutorials on Tianji.com. <br><br> JAXP is a Java API for processing XML. It enables applications to analyze and transform XML documents. Its function is a bit like the JDBC API, which abstracts functions into methods. You can go to the Apache website to find the latest Xerces analyzer, which contains the latest JAXP. After downloading it, put it in your class directory. <br><br>Let’s take a look at how to use the JTree Swing component. <br><br> We all know that in nature, a tree usually has a very thick trunk with many branching branches. There is a certain connection between each tree branch and the branch, because they all have the same source: the trunk. This continuing relationship is not limited to branches; the human genealogy follows the same pattern. From parents, to children, to children’s children, and so on until we lose count. Likewise, in data storage, the concept of a tree is a way to store data in the same way as a human family tree. Each branch of the tree is called a node, each node with child nodes is called a parent node, and the common parent node of all child nodes is called the root node. A JTree component is a simple visual representation of a tree data structure. <br><br> Almost all XML editors include a visual tree structure that allows you to edit elements in an XML document. We'll build an editor soon, but before that, let's take a look at the JTree component. A node stores data at a certain location in a tree. In order to store data, any parent node and their child nodes must be known. The javax.swing.tree package defines some very useful interfaces, providing a common method to build and operate a tree structure. <br><br> TreeNode method, used to access tree node information<br><br> MutableTreeNode method is used on a mutable tree (can add or delete child nodes)<br><br> TreeModel method is used to create and manage tree-related data models. <br><br> Next, we will create a class that continues JTree, providing functions for analyzing XML documents and displaying nodes using visual JTree components. <br><br> Create XTree component<br><br> The XTree class consists of a constructor and three methods. For the sake of simplicity, our component can only build an Xtree, and its nodes cannot be processed after the tree is created. Let's take a look at this class. <br><br> Field:<br><br> PRivate DefaultMutableTreeNode treeNode This member variable stores the TreeNode object used to store the JTree model. <br><br>The DefaultMutableTreeNode class is defined in javax.swing.tree and provides an implementation of the MutableTreeNode interface by default. <br><br> private DocumentBuilderFactory dbf<br><br> private DocumentBuilder db<br><br> private Document doc These three member variables are part of JAXP and are used to analyze XML text and convert it into DOM (Document Object Model) objects. <br><br> Constructor<br><br> public XTree(String text)<br><br> This constructor creates an XTree object by using the XML text passed to the constructor. After initializing some basic display properties related to the JTree superclass and the DOM analysis object, the constructor generates a TreeModel object used to create an actual visual tree. Create a root node by passing the DOM object to the createTreeNode() method. The createTreeNode() method returns an object of type DefaultMutableTreeNode. This object is then used to create the tree's TreeModel. <br><br> Method<br><br> private DefaultMutableTreeNode createTreeNode(Node root)<br><br> This method takes a DOM node and then recurses through the child nodes until all nodes are added to the DefaultMutableTreeNode. This is a recursive method. In order to find every child node under the root node, it calls itself every time. JTree can then use the DefaultMutableTreeNode object since it is already a tree. <br><br>  private String getNodeType(Node node)<br><br> This method is used by createTreeNode() to associate a string with a certain type of node. <br><br>  private Node parseXml()<br><br> This method is used to parse XML text strings. It returns an object of Node type and can be passed to the createTreeNode() method. <br><br>I have given the java code below for everyone to analyze and study. <br><br>// Enter W3C’s DOM classes<br>import org.w3c.dom.*;<br>// JAXP’s classes for DOM I/O<br>import javax.xml.parsers.*;<br>// Standard Java classes<br> import javax.swing.*;<br>import javax.swing.tree.*;<br>import javax.swing.event.*;<br>import java.awt.*;<br>import java.awt.event.*;<br>import java.io .*;<br>public class <br> /**<br>* This member variable stores the TreeNode object used to store the JTree model. <br>*DefaultMutableTreeNode class is defined in javax.swing.tree<br>*An implementation of the MutableTreeNode interface is provided by default. <br>*/<br><br>public ( true );<br>setEditable( false ); // Allow the tree to be editable<br><br>// Analyze the object by initializing its DOM <br>dbf = DocumentBuilderFactory.newInstance();<br>dbf.setValidating( false );<br>db = dbf. newDocumentBuilder();<br><br>// Take the DOM root node and convert it into a JTree tree model<br>treeNode = createTreeNode( parseXml( text ) );<br>setModel( new DefaultTreeModel( treeNode ) );<br>} file://Abort XTree ()<br><br>  /**<br>* These three member variables are part of JAXP and are used to analyze XML text and convert it into DOM (Document Object Model) objects. <br>*/<br><br>private DefaultMutableTreeNode createTreeNode( Node root )<br>{<br>DefaultMutableTreeNode treeNode = null;<br>String type, name, value;<br>NamedNodeMap attribs;<br>Node attribNode;<br><br>// from Get data from the root node <br>type = getNodeType( root ); <br>name = root.getNodeName(); <br>value = root.getNodeValue(); <br><br>treeNode = new DefaultMutableTreeNode( root.getNodeType() == Node.TEXT_NODE ? value : name );<br><br>//Display attributes<br>attribs = root.getAttributes();<br>if( attribs != null )<br>{<br>for( int i = 0; i < attribs.getLength(); i++ )<br>{<br> attribNode = attribs.item(i);<br>name = attribNode.getNodeName().trim();<br>value = attribNode.getNodeValue().trim();<br><br>if ( value != null )<br>{<br>if ( value .length() > 0 )<br>{<br>treeNode.add( new DefaultMutableTreeNode( "[Attribute] --> " + name + "="" + value + """ ) );<br>} file://end if ( value.length() > 0 )<br>} file://end if ( value != null )<br>} file://end for( int i = 0; i } file://end if( attribs != null ) <br><br>// If there are child nodes, recurse <br>if( root.hasChildNodes() )<br>{<br>NodeList children;<br>int numChildren;<br>Node node;<br>String data;<br><br>children = root.getChildNodes();<br>// If the child node is not empty, only recurse <br>if( children != null )<br>{<br>numChildren = children.getLength();<br><br>for (int i=0; i < numChildren; i++)<br>{<br>node = children. item(i);<br>if( node != null )<br>{<br>if( node.getNodeType() == Node.ELEMENT_NODE )<br>{<br>treeNode.add( createTreeNode(node) );<br>} file://end if ( node.getNodeType() == Node.ELEMENT_NODE )<br><br>data = node.getNodeValue();<br><br>if( data != null )<br>{<br>data = data.trim();<br>if ( !data.equals( " ") && !data.equals("

The above is the content of using Java Swing to create an XML editor (1). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template