大家好,我在写一个java读xml得程序,
一个book.xml得文件
<?xml version="1.0" encoding="UTF-8"?>
<book>
 <person>
    <first>Kiran</first>
    <last>Pai</last>
    <age>22</age>
 </person>
 <person>
   <first>Bill</first>
   <last>Gates</last>
   <age>46</age>
 </person>
</book>
---------------------------
意图:我想动态获取节点名放到数组里;
问题:怎样获取节点名;
部分代码:
    public Vector getXml() throws IOException, SAXException,
            ParserConfigurationException {
        Vector head_Vector=new Vector();//Table's Title数组
         DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new File("book.xml"));//选中要读的xml
        .......原来我是把读xml得代码写死了得,现在想写活,怎么遍历
         ...........................................
         return head_Vector;
}
谢谢大家,我急用

解决方案 »

  1.   

    public class BookTest { 
    private String show; 
    private String title; 
    private String author; 
    public String getAuthor() { 
    return author; 

    public void setAuthor(String author) { 
    this.author = author; 

    public String getShow() { 
    return show; 

    public void setShow(String show) { 
    this.show = show; 

    public String getTitle() { 
    return title; 

    public void setTitle(String title) { 
    this.title = title; 


    <?xml version="1.0" encoding="GBK"?> <books> 
      <!--第一次写xml文件--> 
      <book show="yes"> 
        <title>java </title> 
        <author>张三 </author> 
      </book> 
      <book show="no"> 
        <title>c# </title> 
        <author>李四 </author> 
      </book> 
      <book show="yes"> 
        <title>sql        bb </title> 
        <author>王五 </author> 
      </book> 
      <owner>beibei </owner> 
    </books> 
    //解析XML 文件,获取里面的值 
    public List getInfo() 

    List booklist=new ArrayList(); 
    try { 
    SAXReader saxreader=new SAXReader(); 
    Document document=saxreader.read(new File("f:\\bookxml.xml")); 
    List list=document.selectNodes("/books/book"); 
    Iterator iter=list.iterator(); 
    while(iter.hasNext()) 

    BookTest book=new BookTest(); 
    Element bookelement=(Element) iter.next(); 
    book.setShow(bookelement.attributeValue("show")); 
        Element title=bookelement.element("title"); 
        book.setTitle(title.getText()); 
        Element author=bookelement.element("author"); 
        book.setAuthor(author.getText()); 
        booklist.add(book); 
                    

    } catch (DocumentException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 

    return booklist; 
    }
      

  2.   

    //读取所有元素信息
                    List items = null;
    FileInputStream fis = null;
    try {
    items = new ArrayList();
    fis = new FileInputStream();
    SAXBuilder sb = new SAXBuilder();
    Document doc = sb.build(fis);
    Element root = doc.getRootElement(); // 得到根元素
    items = root.getChildren("book"); // 得到根元素所有子元素的集合
    }
      

  3.   

    1. 如果你的xml的结构经常变,而你仅仅是为了得到xml中的节点名称,那么用DOM遍历。 Node.getNodeName()可以得到当前节点的名称的;
    2. 如果你的xml结构不变,而你是为了拿到每个节点的值,建议用Castor
      

  4.   

    谢谢你,我大概知道哦了,我得xml得结构就是那样得,不会变,我就是把这个book.xml得数据取出来,实现一种这样得效果JTable
    ----------------
    fist  last   age  ----表头..................----值
      

  5.   

    不对啊,我还是有点问题。请给个例子,我把节点名和值存到了两个Vector中,