Javaがxmlファイルを読み書きする方法


Javaプログラムでxmlファイルを解析する方法
DOM解析
//      DocumentBuilderFactory    
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
    //      DocumentBuilder    
    DocumentBuilder db = dbf.newDocumentBuilder();
    //    DocumentBuilder     parse      xml   
    Document doc = db.parse("   .xml");
    //                   
    NodeList nodeList = doc.getElementsByTagName("       ");
    //            
    for (int i = 0; i < nodeList.getLength(); i++) {
        //   NodeList   item(i)         
        Node node = nodeList.item(i);
        //          
        NamedNodeMap attributes = node.getAttributes();
        //        
        for (int j = 0; j < attributes.getLength(); j++) {
            Node attr = attributes.item(j);
            System.out.println(attr.getNodeName() + ":" + attr.getNodeValue());
        }
        //         
        NodeList childNodes = node.getChildNodes();
        for (int k = 0; k < childNodes.getLength(); k++) {
            Node subNode = childNodes.item(k);
            //    text        element      
            if (subNode.getNodeType() == Node.ELEMENT_NODE) {
                // getTextContent               value
                System.out.println(subNode.getNodeName() + ":" + subNode.getTextContent());
            }
        }
    }
} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

SAX解析
DOMとは異なり、DOMはxmlファイルをプログラム全体にロードして解析し、SAXは自分でHandlerクラスを作成して最外層から内側へ徐々に解析します.

//      SAXParserFactory   
SAXParserFactory factor = SAXParserFactory.newInstance();
//    SAXParser   
try {
    SAXParser parser = factor.newSAXParser();
    parser.parse("bookstore.xml", new handler());
} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}


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

public class handler extends DefaultHandler {
    //      xml        
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        // qName         
        System.out.print(qName + " ");
        //       
        if (attributes.getLength() > 0) {
            for (int i = 0; i < attributes.getLength(); i++) {
                System.out.println(attributes.getQName(i) + ":" + attributes.getValue(i));
            }
        }
    }
    //             
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String value = new String(ch, start, length);
        if (!value.trim().equals("")) {
            System.out.println(new String(ch, start, length));
        }
    }
    //      xml        
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
    }
    //         
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }
    //         
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

}