java读取xml文件的四种方法 1 import javax.xml.parsers.SAXParser; 2 import javax.xml.parsers.SAXParserFactory; 3 4 import org.xml.sax.Attributes; 5 import org.xml.sax.InputSource; 6 import org.xml.sax.SAXException; 7 import org.xml.sax.helpers.DefaultHandler; 8 9 public class MyXMLReader2SAX extends DefaultHandler { 10 11 java.util.Stack tags = new java.util.Stack(); 12 13 public MyXMLReader2SAX() { 14 super(); 15 } 16 17 public static void main(String args[]) { 18 long lasting = System.currentTimeMillis(); 19 try { 20 SAXParserFactory sf = SAXParserFactory.newInstance(); 21 SAXParser sp = sf.newSAXParser(); 22 MyXMLReader2SAX reader = new MyXMLReader2SAX(); 23 sp.parse(new InputSource(“data_10k.xml”), reader); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 28 System.out.println(“运行时间:” + (System.currentTimeMillis() – lasting) 29 + “毫秒”); 30 } 31 32 public void characters(char ch[], int start, int length) 33 throws SAXException { 34 String tag = (String) tags.peek(); 35 if (tag.equals(“NO”)) { 36 System.out.print(“车牌号码:” + new String(ch, start, length)); 37 } 38 if (tag.equals(“ADDR”)) { 39 System.out.println(“地址:” + new String(ch, start, length)); 40 } 41 } 42 43 public void startElement(String uri, String localName, String qName, 44 Attributes attrs) { 45 tags.push(qName); 46 } 47 }
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/18273.html