How to read and write xml files?

前端 未结 6 571
庸人自扰
庸人自扰 2020-11-22 09:03

I have to read and write to and from an XML file. What is the easiest way to read and write XML files using Java?

6条回答
  •  故里飘歌
    2020-11-22 09:46

    SAX parser is working differently with a DOM parser, it neither load any XML document into memory nor create any object representation of the XML document. Instead, the SAX parser use callback function org.xml.sax.helpers.DefaultHandler to informs clients of the XML document structure.

    SAX Parser is faster and uses less memory than DOM parser. See following SAX callback methods :

    startDocument() and endDocument() – Method called at the start and end of an XML document. startElement() and endElement() – Method called at the start and end of a document element. characters() – Method called with the text contents in between the start and end tags of an XML document element.

    1. XML file

    Create a simple XML file.

    
    
        
            yong
            mook kim
            mkyong
            100000
        
        
            low
            yin fong
            fong fong
            200000
        
    
    
    1. XML parser:

    Java file Use SAX parser to parse the XML file.

    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;
    
    public class ReadXMLFile {
        public static void main(String argv[]) {
    
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
    
                DefaultHandler handler = new DefaultHandler() {
                    boolean bfname = false;
                    boolean blname = false;
                    boolean bnname = false;
                    boolean bsalary = false;
    
                    public void startElement(String uri, String localName,String qName, 
                                Attributes attributes) throws SAXException {
    
                        System.out.println("Start Element :" + qName);
    
                        if (qName.equalsIgnoreCase("FIRSTNAME")) {
                            bfname = true;
                        }
    
                        if (qName.equalsIgnoreCase("LASTNAME")) {
                            blname = true;
                        }
    
                        if (qName.equalsIgnoreCase("NICKNAME")) {
                            bnname = true;
                        }
    
                        if (qName.equalsIgnoreCase("SALARY")) {
                            bsalary = true;
                        }
    
                    }
    
                    public void endElement(String uri, String localName,
                        String qName) throws SAXException {
    
                        System.out.println("End Element :" + qName);
    
                    }
    
                    public void characters(char ch[], int start, int length) throws SAXException {
    
                        if (bfname) {
                            System.out.println("First Name : " + new String(ch, start, length));
                            bfname = false;
                        }
    
                        if (blname) {
                            System.out.println("Last Name : " + new String(ch, start, length));
                            blname = false;
                        }
    
                        if (bnname) {
                            System.out.println("Nick Name : " + new String(ch, start, length));
                            bnname = false;
                        }
    
                        if (bsalary) {
                            System.out.println("Salary : " + new String(ch, start, length));
                            bsalary = false;
                        }
    
                    }
    
                };
    
                saxParser.parse("c:\\file.xml", handler);
    
            } catch (Exception e) {
               e.printStackTrace();
            }
    
        }
    
    }
    

    Result

    Start Element :company
    Start Element :staff
    Start Element :firstname
    First Name : yong
    End Element :firstname
    Start Element :lastname
    Last Name : mook kim
    End Element :lastname
    Start Element :nickname
    Nick Name : mkyong
    End Element :nickname
    and so on...

    Source(MyKong) - http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

提交回复
热议问题