首页 > 其他 > 详细

Dom解析xml源代码

时间:2014-06-07 11:12:39      阅读:358      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;
import org.xml.sax.SAXException;


public class DomXmlTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db=dbf.newDocumentBuilder();
            File f=new File("c:/bb.xml");
            Document doc=db.parse(f);
            NodeList nl=doc.getElementsByTagName("student");
            int len=nl.getLength();
            for(int i=0;i<len;i++){
                Element elt=(Element) nl.item(i);
                Node eltName=(Node) elt.getElementsByTagName("name").item(0);
                Node eltAge=(Node) elt.getElementsByTagName("age").item(0);
                System.out.println("name:"+eltName.getFirstChild().getNodeValue());
                System.out.println("age:"+eltAge.getFirstChild().getNodeValue());
            }
        
        
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}
bubuko.com,布布扣

 

xml文件

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student sn="01">
        <name>contextConfigLocation</name>
        <age>classpath:spring_config/spring*.xml</age>
    </student>
    <student sn="02">
        <name>AREACODE</name>
        <age>410526</age>
    </student>
</students>
bubuko.com,布布扣

 

Dom解析xml源代码,布布扣,bubuko.com

Dom解析xml源代码

原文:http://www.cnblogs.com/LT0314/p/3770197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!