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.
Let’s take a look at how to use the JTree Swing component.
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.
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.
TreeNode method, used to access tree node information
MutableTreeNode method is used on a mutable tree (can add or delete child nodes)
TreeModel method is used to create and manage tree-related data models.
Next, we will create a class that continues JTree, providing functions for analyzing XML documents and displaying nodes using visual JTree components.
Create XTree component
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.
Field:
PRivate DefaultMutableTreeNode treeNode This member variable stores the TreeNode object used to store the JTree model.
The DefaultMutableTreeNode class is defined in javax.swing.tree and provides an implementation of the MutableTreeNode interface by default.
private DocumentBuilderFactory dbf
private DocumentBuilder db
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.
Constructor
public XTree(String text)
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.
Method
private DefaultMutableTreeNode createTreeNode(Node root)
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.
private String getNodeType(Node node)
This method is used by createTreeNode() to associate a string with a certain type of node.
private Node parseXml()
This method is used to parse XML text strings. It returns an object of Node type and can be passed to the createTreeNode() method.
I have given the java code below for everyone to analyze and study.
// Enter W3C’s DOM classes
import org.w3c.dom.*;
// JAXP’s classes for DOM I/O
import javax.xml.parsers.*;
// Standard Java classes
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io .*;
public class
/**
* This member variable stores the TreeNode object used to store the JTree model.
*DefaultMutableTreeNode class is defined in javax.swing.tree
*An implementation of the MutableTreeNode interface is provided by default.
*/
public ( true );
setEditable( false ); // Allow the tree to be editable
// Analyze the object by initializing its DOM
dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating( false );
db = dbf. newDocumentBuilder();
// Take the DOM root node and convert it into a JTree tree model
treeNode = createTreeNode( parseXml( text ) );
setModel( new DefaultTreeModel( treeNode ) );
} file://Abort XTree ()
/**
* These three member variables are part of JAXP and are used to analyze XML text and convert it into DOM (Document Object Model) objects.
*/
private DefaultMutableTreeNode createTreeNode( Node root )
{
DefaultMutableTreeNode treeNode = null;
String type, name, value;
NamedNodeMap attribs;
Node attribNode;
// from Get data from the root node
type = getNodeType( root );
name = root.getNodeName();
value = root.getNodeValue();
treeNode = new DefaultMutableTreeNode( root.getNodeType() == Node.TEXT_NODE ? value : name );
//Display attributes
attribs = root.getAttributes();
if( attribs != null )
{
for( int i = 0; i < attribs.getLength(); i++ )
{
attribNode = attribs.item(i);
name = attribNode.getNodeName().trim();
value = attribNode.getNodeValue().trim();
if ( value != null )
{
if ( value .length() > 0 )
{
treeNode.add( new DefaultMutableTreeNode( "[Attribute] --> " + name + "="" + value + """ ) );
} file://end if ( value.length() > 0 )
} file://end if ( value != null )
} file://end for( int i = 0; i } file://end if( attribs != null )
// If there are child nodes, recurse
if( root.hasChildNodes() )
{
NodeList children;
int numChildren;
Node node;
String data;
children = root.getChildNodes();
// If the child node is not empty, only recurse
if( children != null )
{
numChildren = children.getLength();
for (int i=0; i < numChildren; i++)
{
node = children. item(i);
if( node != null )
{
if( node.getNodeType() == Node.ELEMENT_NODE )
{
treeNode.add( createTreeNode(node) );
} file://end if ( node.getNodeType() == Node.ELEMENT_NODE )
data = node.getNodeValue();
if( data != null )
{
data = data.trim();
if ( !data.equals( " ") && !data.equals("