DOM解析xml文件

首先需要下载插件jdom.jar进入项目

导入jdom.jar插件之后一切就简单了,这里测试给一个简单xml文件

<?xml version="1.0" encoding="UTF-8"?>
<student-info>
    <student id="10031" name="admin">
        <name id="王明">
            <height>165</height>
            <weight>100</weight>
        </name>
        <age>24</age>
        <address>上海</address>
    </student>
    <student id="10032" name="user">
        <name id="李珊" age="23">
            <height>165</height>
            <weight>100</weight>
        </name>
        <age>25</age>
        <address>北京</address>
    </student>
</student-info>
简单的xml文件

创建针对此xml文件的解析器

package one;

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 DOMPrinter {
    
    public static void printNode(Node node){
        short nodetype=node.getNodeType();
        switch(nodetype){
            case Node.PROCESSING_INSTRUCTION_NODE:
                System.out.println("指令节点:<"+node.getNodeName()+""+node.getNodeValue()+">");
                break;
            case Node.ELEMENT_NODE:
                System.out.println("元素节点:<"+node.getNodeName()+">");
                NamedNodeMap attrs=node.getAttributes();
                int attrNum=attrs.getLength();;
                for(int i=0;i<attrNum;i++){
                    Node attr=attrs.item(i);
                    System.out.println(""+attr.getNodeName()+"="+attr.getNodeValue()+"");
                }
                break;
            case Node.TEXT_NODE:
                if(node.getNodeValue()!=null&&node.getNodeValue().intern().length()!=0){
                    System.out.println("文本节点:"+node.getNodeValue());
                    break;
                }
            case Node.DOCUMENT_NODE:
                System.out.println("文档节点");
                break;
            default:
                System.out.println("位置节点");
                break;
        } 
        Node child=node.getFirstChild();
        while(child!=null){
            printNode(child);
            child=child.getNextSibling();
        }
        if(node.getNodeType()==Node.ELEMENT_NODE){
            System.out.println("节点结束:"+"</"+node.getNodeName()+">");
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//建立dom解析的工厂实例
        
        DocumentBuilder builder;
        try {
            //从工厂实例中获得解析器
            builder = factory.newDocumentBuilder();
            Document doc=builder.parse("D:\workspqce2013\DOMJX-XML\src\Student.xml");//将需要解析的xml文件导入dom解析器
            //获取需要解析的xml文件元素
            printNode(doc);
        } 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();
        }
        
        
    }

}
xml解析器

另给出一个针对各种xml都适用的解析器

package one;

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

import org.w3c.dom.*;


public class TraverseXML {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            
            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//建立dom解析的工厂实例
            
            DocumentBuilder builder=factory.newDocumentBuilder();//从工厂实例中获得解析器
            
            Document doc=builder.parse("D:\workspqce2013\DOMJX-XML\src\Student.xml");//将需要解析的xml文件导入dom解析器
            //获取需要解析的xml文件元素
            NodeList nl=doc.getElementsByTagName("student");
            for(int i=0;i<nl.getLength();i++){
                Element nod=(Element)nl.item(i);
                //System.out.println("id:"+ nod.getAttribute("id"));
                //System.out.println("name:"+nod.getAttribute("name"));
                String name=nod.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
                String age=nod.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
                String address=nod.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();        
                //System.out.println(name);
                //System.out.println(age);
                //System.out.println(address);
                Element nod2=(Element)nod.getElementsByTagName("name").item(0);
                String id=nod2.getAttribute("id");
                String age2=nod2.getAttribute("age");
                if(nod2.getElementsByTagName("height").item(0)!=null){
                    String height=nod2.getElementsByTagName("height").item(0).getFirstChild().getNodeValue();
                    System.out.println(height);
                }else{
                    System.out.println("dddddddddddd");
                }
                if(age2!=""&&age2!=null){
                    System.out.println(id);
                    System.out.println(age2);
                }else{
                    System.out.println(id);
                    System.out.println("没有此属性");
                }
                
                
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
解析器
原文地址:https://www.cnblogs.com/feitianshaoxai/p/6400395.html