java DOM解析XML(2) 树

//=======a.xml===================

<?xml version="1.0" encoding="GB2312"?>
<people>
    
<person personID="1">
       
<name>CY</name>
       
<age>22</age>
       
<address>武汉</address>
    
</person>
    
<person personID="2">
       
<name>SUN</name>
       
<age>18</age>
       
<address>襄樊</address>
    
</person>
    
<person personID="3">
       
<name>LiGuo</name>
       
<age>55</age>
       
<address>武汉</address>
    
</person>
    
<person personID="4">
       
<name>wj</name>
       
<age>55</age>
       
<address>应城</address>
    
</person>
</people>

//====java文件===treeTest.java=================

/*
 * treeTest.java
 *
 * Created on 2008年2月17日, 下午9:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/


package javaapplication2;
/**
 *
 * 
@author Administrator
 
*/

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
class treeTest extends JFrame {
    
private JTree tree=null;
    
private DefaultTreeModel treeModel=null;
    
private static String Na=null;   //定义Name元素
    private static String Ag=null//定义Age元素
    private static String Address=null;  //定义Address元素
    private static String sqlStr=null;
    
private static String person=null;
    DefaultMutableTreeNode root
=null;
    DefaultMutableTreeNode aaa
=null;
    DefaultMutableTreeNode ccc1
=null;
    DefaultMutableTreeNode ccc2
=null;
    DefaultMutableTreeNode ccc3
=null;
    
    
public treeTest(String tit) throws Exception {
        
super(tit);
        javax.xml.parsers.DocumentBuilderFactory factory
=DocumentBuilderFactory.newInstance();
        
//根据解析器工厂来获得具体的加载文档对象
        javax.xml.parsers.DocumentBuilder builder=factory.newDocumentBuilder();
        
//加载XML文件  路径不能是中文
        Document doc=builder.parse("a.xml");
        NodeList n1
=doc.getElementsByTagName("person");
        root
=new DefaultMutableTreeNode("People");
        treeModel
=new DefaultTreeModel(root);
        tree
=new JTree(treeModel);
        
for(int i=0;i<n1.getLength();i++{
            
//生成一个Element对象来存放具体节点的值
            Element node=(Element)n1.item(i);
            person
=node.getNodeName().toString()+i;
            Na
=node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
            Ag
=node.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
            Address
=node.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();
            
            aaa
=new DefaultMutableTreeNode(person);
            ccc1
=new DefaultMutableTreeNode(Na);
            ccc2
=new DefaultMutableTreeNode(Ag);
            ccc3
=new DefaultMutableTreeNode(Address);
            aaa.add(ccc1);
            aaa.add(ccc2);
            aaa.add(ccc3);
            treeModel.insertNodeInto(aaa,root,i);     
//将每个aaa加到根节点root中
        }

        getContentPane().add(tree,BorderLayout.CENTER);
        
final JLabel messageLabel=new JLabel("用户还没有选择!");
        add(messageLabel,BorderLayout.SOUTH);
        tree.addTreeSelectionListener(
new TreeSelectionListener() {
            
public void valueChanged(TreeSelectionEvent t) {
                TreePath tp
=t.getNewLeadSelectionPath();
                messageLabel.setText(
"您选择了:"+tp.getLastPathComponent());
            }

        }
);
        setSize(
500,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(
true);
    }

    
public static void main(String args[]) throws Exception {
        JFrame.setDefaultLookAndFeelDecorated(
true);
        
new treeTest("JTree-XML-Test");
    }

}
posted @ 2008-02-17 17:10  wj-conquer  阅读(521)  评论(0编辑  收藏  举报