java學習

          java學習

           

          java讀取xml數據方法

          xml文件:

            Xml代碼


            <?xml version="1.0" encoding="GB2312"?>  
            <RESULT> 
            <VALUE> 
            <NO>A1234</NO> 
            <ADDR>河南省鄭州市</ADDR> 
            </VALUE> 
            <VALUE> 
            <NO>B1234</NO> 
            <ADDR>河南省鄭州市二七區</ADDR> 
            </VALUE> 
            </RESULT>

          第一種 DOM 實現方法:

            Java代碼


              import java.io.File; 
            import javax.xml.parsers.DocumentBuilder; 
            import javax.xml.parsers.DocumentBuilderFactory; 
            import org.w3c.dom.Document; 
            import org.w3c.dom.NodeList; 
            public class MyXMLReader2DOM { 
            public static void main(String arge[]) { 
            long lasting = System.currentTimeMillis(); 
            try { 
            File f = new File("data_10k.xml"); 
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
            DocumentBuilder builder = factory.newDocumentBuilder(); 
            Document doc = builder.parse(f); 
            NodeList nl = doc.getElementsByTagName("VALUE"); 
            for (int i = 0; i < nl.getLength(); i++) { 
            System.out.print("車牌號碼:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); 
            System.out.println("車主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); 
            System.out.println("運行時間:" + (System.currentTimeMillis() - lasting) 
            + "毫秒"); 
            } 
            } 
            } catch (Exception e) { 
            e.printStackTrace(); 
            } 
            } 
            }

            第二種,DOM4J實現方法:

            Java代碼


              import java.io.*; 
            import java.util.*; 
            import org.dom4j.*; 
            import org.dom4j.io.*; 
            public class MyXMLReader2DOM4J { 
            public static void main(String arge[]) { 
            long lasting = System.currentTimeMillis(); 
            try { 
            File f = new File("data_10k.xml"); 
            SAXReader reader = new SAXReader(); 
            Document doc = reader.read(f); 
            Element root = doc.getRootElement(); 
            Element foo; 
            for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { 
            foo = (Element) i.next(); 
            System.out.print("車牌號碼:" + foo.elementText("NO")); 
            System.out.println("車主地址:" + foo.elementText("ADDR")); 
            } 
            System.out.println("運行時間:" + (System.currentTimeMillis() - lasting) 
            + "毫秒"); 
            } 
            } catch (Exception e) { 
            e.printStackTrace(); 
            } 
            } 
            }

            第三種 JDOM實現方法:

            Java代碼


              import java.io.*; 
            import java.util.*; 
            import org.jdom.*; 
            import org.jdom.input.*; 
            public class MyXMLReader2JDOM { 
            public static void main(String arge[]) { 
            long lasting = System.currentTimeMillis(); 
            try { 
            SAXBuilder builder = new SAXBuilder(); 
            Document doc = builder.build(new File("data_10k.xml")); 
            Element foo = doc.getRootElement(); 
            List allChildren = foo.getChildren(); 
            for (int i = 0; i < allChildren.size(); i++) { 
            System.out.print("車牌號碼:"+ ((Element) allChildren.get(i)).getChild("NO").getText()); 
            System.out.println("車主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText()); 
            } 
            System.out.println("運行時間:" + (System.currentTimeMillis() - lasting) 
            + "毫秒"); 
            } 
            } catch (Exception e) { 
            e.printStackTrace(); 
            } 
            } 
            }

            第四種SAX實現方法:

            Java代碼

           


           import javax.xml.parsers.SAXParser; 
            import javax.xml.parsers.SAXParserFactory; 
            import org.xml.sax.Attributes; 
            import org.xml.sax.InputSource; 
            import org.xml.sax.SAXException; 
            import org.xml.sax.helpers.DefaultHandler; 
            public class MyXMLReader2SAX extends DefaultHandler { 
            java.util.Stack tags = new java.util.Stack(); 
            public MyXMLReader2SAX() { 
            super(); 
            } 
            public static void main(String args[]) { 
            long lasting = System.currentTimeMillis(); 
            try { 
            SAXParserFactory sf = SAXParserFactory.newInstance(); 
            SAXParser sp = sf.newSAXParser(); 
            MyXMLReader2SAX reader = new MyXMLReader2SAX(); 
            sp.parse(new InputSource("data_10k.xml"), reader); 
            } catch (Exception e) { 
            e.printStackTrace(); 
            } 
            System.out.println("運行時間:" + (System.currentTimeMillis() - lasting) 
            + "毫秒"); 
            } 
            public void characters(char ch[], int start, int length) 
            throws SAXException { 
            String tag = (String) tags.peek(); 
            if (tag.equals("NO")) { 
            System.out.print("車牌號碼:" + new String(ch, start, length)); 
            } 
            if (tag.equals("ADDR")) { 
            System.out.println("地址:" + new String(ch, start, length)); 
            } 
            } 
            public void startElement(String uri, String localName, String qName, 
            Attributes attrs) { 
            tags.push(qName); 
            } 
            }

          posted on 2012-12-29 15:04 楊軍威 閱讀(215) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 深圳市| 当涂县| 荣成市| 和田县| 城口县| 祁门县| 东源县| 松原市| 聂拉木县| 大余县| 随州市| 炉霍县| 海晏县| 扎囊县| 习水县| 平利县| 田东县| 孟州市| 凤山县| 望江县| 库伦旗| 八宿县| 辰溪县| 沅江市| 禹州市| 藁城市| 晴隆县| 通辽市| 渭南市| 安庆市| 瑞丽市| 长岭县| 临湘市| 左云县| 郑州市| 阆中市| 晋宁县| 碌曲县| 汉阴县| 礼泉县| 仪陇县|