Dom4j解析XML

package com.test;

import java.io.InputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;

import org.dom4j.io.SAXReader;

/**
 * 
 * DOM4j解析XML
 */
public class Dom4jParseXml
{
    /**
     * 获取指定xml文档的Document对象,xml文件必须在classpath中可以找到
     * @param xmlFilePath xml文件路径
     * @return Document对象
     */
    public static Document parse2Document(String xmlFilePath)
    {
        SAXReader reader = new SAXReader();
        Document document= null;
        try
        {
            InputStream in = Dom4jParseXml.class.getResourceAsStream(xmlFilePath);
            document = reader.read(in);
            
        }
        catch (Exception e) {
            // TODO: handle exception
            System.out.println(e.getMessage());
            System.out.println("读取classpath下xmlFileName文件发生异常,请检查CLASSPATH和文件名是否存在!"); 
            e.printStackTrace(); 

        }
        return document; 
    }
    
    /**
     * 
     * @param xmlFileName
     */
    public static void parseXmlData(String xmlFileName)
    {
        //产生一个解析器对象 
        SAXReader reader = new SAXReader();
        //将xml文档转换为Document的对象
        Document document = parse2Document(xmlFileName);
        //获取文档的根元素
        Element root = document.getRootElement();
        StringBuffer bf = new StringBuffer();
        bf.append("通过Dom4j解析XML,并输出数据:\n"); 
        bf.append(xmlFileName + "\n"); 
        bf.append("----------------开始遍历----------------\n"); 
      //遍历当前元素(在此是根元素)的子元素 
        for(Iterator it_one = root.elementIterator();it_one.hasNext();)
        {
            Element ele_one = (Element) it_one.next();
            //获取当前元素的名字
            String student = ele_one.getName();
            //获取当前元素的id和sex属性的值分别赋给id,sex变量
            String id = ele_one.attributeValue("id");
            String sex = ele_one.attributeValue("sex");
            String name = ele_one.element("name").getText();
            String age = ele_one.element("age").getText();
            //将数据存放到缓冲区字符串对象中
            bf.append(student + ":\n");
            bf.append("\t"+"id=" + id + " sex=" + sex + "\n");
            bf.append("\t"+"name=" + name + " age=" + age + "\n");
            
            //获取当前元素ele_one(在此是student元素)下的子元素subjects 
            
            Element ele_subjects = ele_one.element("subjects");
            bf.append("\t" + ele_subjects.getName() + "\n");
            
            //遍历当前元素ele_subjects(在此是subjects元素)的子元素 
            for(Iterator it_two = ele_subjects.elementIterator();it_two.hasNext();)
            {
                Element ele_two = (Element)it_two.next();
                String subname = ele_two.attributeValue("subname");
                String subject = ele_two.getTextTrim();
                bf.append("\t\t" + ele_two.getName() + ":" + "subname=" + subname + " value=\"" + subject + "\"\n");
            }
            bf.append("\n");
        }
        bf.append("-----------------------遍历结束---------------------\n");
        System.out.println(bf.toString());
        

    }
    
    public static void main(String args [])
    {
        parseXmlData("../test/student.xml");
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<student id="1" sex="m">
<name>Aaron</name>
<age>14</age>
<subjects>
<subject id="1000" subname="语文">80</subject>
<subject id="1001" subname="数学">89</subject>
<subject id="1003" subname="历史">70</subject>
</subjects>
</student>
<student id="2" sex="w">
<name>selina </name>
<age>15</age>
<subjects>
<subject id="1000" subname="语文">90</subject>
<subject id="1001" subname="数学">80</subject>
<subject id="1003" subname="历史">80</subject>
</subjects>
</student>
</xml-body>

你可能感兴趣的:(xml)