}
case Node.CDATA_SECTION_NODE:
{
type = "CData section";
break;
}
case Node.ENTITY_REFERENCE_NODE:
{
type = "Entity reference";
break;
}
case Node.ENTITY_NODE:
{
type = "Entity";
break;
}
case Node.PROCESSING_INSTRUCTION_NODE:
{
type = "Processing instruction";
break;
}
case Node.COMMENT_NODE:
{
type = "Comment";
break;
}
case Node.DOCUMENT_NODE:
{
type = "Document";
break;
}
case Node.DOCUMENT_TYPE_NODE:
{
type = "Document type";
break;
}
case Node.DOCUMENT_FRAGMENT_NODE:
{
type = "Document fragment";
break;
}
case Node.NOTATION_NODE:
{
type = "Notation";
break;
}
default:
{
type = "???";
break;
}
}// 结束 switch( node.getNodeType() )
return type;
} file://结束 getNodeType()
使用Java Swing创建一个XML编辑器(5)
时间:2010-12-16
/**
* 这个方法,用来分析XML文本字符串,它返回Node类型的对象,能够被传送到createTreeNode()方法中。
*
* @参数 text 一个显示XML文档的字符串
* @返回值 返回一个org.w3c.Node.Node对象
*/
private Node parseXml( String text )
{
ByteArrayInputStream byteStream;
byteStream = new ByteArrayInputStream( text.getBytes() );
try
{
doc = db.parse( byteStream );
}
catch ( Exception e )
{
e.printStackTrace();
System.exit(0);
}
return ( Node )doc.getDocumentElement();
} file://结束 parseXml()
} file://结束 class XTree
代码2 XTreeTester.java
import javax.xml.parsers.*;
// GUI类
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
file://标准 Java类
import java.io.*;
public class XTreeTester extends JFrame
{
// XTree对象,用来在JTree中显示XML
private XTree xTree;
// JScrollPane是JTree的容器
private JScrollPane jScroll;
private WindowListener winClosing;
// 设置框架的宽和高
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 300;
使用Java Swing创建一个XML编辑器(6)
时间:2010-12-16
/*
* 构造器构造一个框架包含JScrollPane,
* 把一个基于XML字符串的XTree对象传到构造函数中
*/
public XTreeTester( String title, String xml ) throws ParserConfigurationException
{
super( title );
Toolkit toolkit;
Dimension dim, minimumSize;
int screenHeight, screenWidth;
// 初始化基本的布局属性
setBackground( Color.lightGray );
getContentPane().setLayout( new BorderLayout() );
toolkit = Toolkit.getDefaultToolkit();
dim = toolkit.getScreenSize();
screenHeight = dim.height;
screenWidth = dim.width;
setBounds( (screenWidth-FRAME_WIDTH)/2, (screenHeight-FRAME_HEIGHT)/2, FRAME_WIDTH, FRAME_HEIGHT );
// 构建XTree对象
xTree = new XTree( xml );
file://把XTree封装到JScroll中,以便在JFrame可以使它在屏幕中上下滚动.
jScroll = new JScrollPane();
jScroll.getViewport().add( xTree );
// 添加滚动条到框架中
getContentPane().add( jScroll, BorderLayout.CENTER );
validate();
setVisible(true);
// 添加WindowListener用来关闭窗口
winClosing = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exit();
}
};
addWindowListener(winClosing);
}
使用Java Swing创建一个XML编辑器(7)
时间:2010-12-16
// 程序从 |