JAVA中用SAX和DOM技术解析XML的应用

1.SAX技术:
package sax;

import java.io.File;
import java.io.IOException;

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

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * SAX解析技术
 * @author Daniel Cheng
 *
 */
public class SAXParserTest {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory factory=SAXParserFactory.newInstance();
        factory.setValidating(true);
        SAXParser parser=factory.newSAXParser();
        System.out.println(parser.isValidating());//DTD校验
        parser.parse(new File("file/dong.xml"), new DefaultHandler(){
           
            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                System.out.println(new String(ch,start,length));
               
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                System.out.println("</"+qName+">");
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                System.out.print("<"+qName);
                for(int i=0;i<attributes.getLength();i++){
                   
                    System.out.print(" " + attributes.getQName(i) + "=\"" + attributes.getValue(i)+ "\"");
                }
                System.out.println(">");
            }
           
           
            @Override
            public void error(SAXParseException e) throws SAXException {
                System.out.println("一般错误!"+e.getMessage());
            }

            @Override
            public void fatalError(SAXParseException e) throws SAXException {
                System.out.println("严重错误!"+e.getMessage());
            }
           
           
        });

    }

}

2.DOM技术:
package dom;

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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * DOM解析技术
 * @author Daniel Cheng
 *
 */
public class DompraserTest {

    public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new File("file/dong.xml"));
        Element root = doc.getDocumentElement();
        printElement(root);
    }

    public static void printElement(Element e) {
        System.out.print("<" + e.getTagName());
        NamedNodeMap map = e.getAttributes();
        if(map!=null){
        for (int i = 0; i < map.getLength(); i++) {
            Attr attr = (Attr) map.item(i);
            System.out.print(" " + attr.getName() + "=\"" + attr.getValue()+ "\"");
        }
        }
        System.out.print(">");
       
        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element en = (Element) n;
                printElement(en);
            } else {
                System.out.print(n.getTextContent());
            }
        }
        System.out.print("</" + e.getTagName() + ">");
    }

}

 

你可能感兴趣的:(java,xml)