xml解析

DOCTYPE disks[
    
]>

    
        10G
        100
        200
    
    
    
        20G
        100
        3000
    


sax解析整个xml文档内容
package com.briup.xml.chap01;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import jdk.nashorn.internal.runtime.ListAdapter;

/* sax 解析 xml 原样输出
 * 
 */
public class SaxDisk {

    public static void main(String[] args) {

        // 1. 创建 sax解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();

        try {
            // 2.获取sax解析器
            SAXParser parser = factory.newSAXParser();
            File xmlFile = new File("src/com/briup/xml/chap01/disk.xml");

            // 3.解析
            MyHandler hanlder = new MyHandler();
            /*
             * 解析谁 谁来解析(hanlder) 1.第一个参数表示需要解析的文件 2.第二个参数表示真正执行解析的类
             * 
             */
            parser.parse(xmlFile, hanlder);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MyHandler extends DefaultHandler {

    // 前两个参数跟网络有关 这里不需要关注
    // 解析所有的开始标签

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        // qname是标签名
        System.out.print("<" + qName);

        // 通过属性的长度 获取属性的name和value
        for (int i = 0; i < attributes.getLength(); i++) {
            // 属性的名字
            String name = attributes.getQName(i);
            // 属性的值
            String value = attributes.getValue(i);
            System.out.print(" " + name + "='" + value + "'");
        }
        System.out.print(">");

    }

    // 解析所有的结束标签
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.print("");
    }

    // 解析xml中出现的文档内容,包括空格,文本以及换行
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        // 把读到的内容变成字符串 这个字符串包含了空格 换行
        String str = new String(ch, start, length);
        System.out.print(str);

    }

}

// 创建dom解析工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 创建dom解析器
		DocumentBuilder builder = factory.newDocumentBuilder();

		File file = new File("src/com/briup/xml/chap02/disk.xml");
		// 解析返回documnet对象
		Document document = builder.parse(file);
		// 元素节点 值为null
		NodeList nl = document.getElementsByTagName("disk");
		// 第一个disk元素节点
		Node node1 = nl.item(0);
		System.out.println(node1);
		System.out.println(node1.getNodeType()); // 1 代表元素节点
		System.out.println(node1.getNodeName()); // disk
		System.out.println(node1.getNodeValue()); // null

		// 属性节点 属性节点肯能会有多个 item(0) 是取第几个
		Node attributeNode = nl.item(0).getAttributes().item(0);
		System.out.println(attributeNode); // name="c盘"

		// 第一个子节点 是空文本
		Node firstChild = node1.getFirstChild();
		System.out.println(firstChild);
		// 空文本的下一个节点 是size
		Node nextSibling = firstChild.getNextSibling();
		System.out.println(nextSibling);
		
		NodeList n2 = document.getElementsByTagName("file");
		Node item1 = n2.item(0);
		System.out.println(item1);
		Node firstChild2 = item1.getFirstChild();
		System.out.println(firstChild2.getNodeValue());

  

 

 

 

你可能感兴趣的:(xml解析)