标签:
1 <?xml version="1.0" encoding="GB2312"?> 2 <RESULT> 3 <VALUE> 4 <NO>A1234</NO> 5 <ADDR>XX号</ADDR> 6 </VALUE> 7 <VALUE> 8 <NO>B1234</NO> 9 <ADDR>XX组</ADDR> 10 </VALUE> 11 </RESULT>
1)DOM
1 import java.io.*; 2 import java.util.*; 3 import org.w3c.dom.*; 4 import javax.xml.parsers.*; 5 public class MyXMLReader{ 6 public static void main(String arge[]){ 7 long lasting =System.currentTimeMillis(); 8 try{ 9 File f=new File("data_10k.xml"); 10 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 11 DocumentBuilder builder=factory.newDocumentBuilder(); 12 Document doc = builder.parse(f); 13 NodeList nl = doc.getElementsByTagName("VALUE"); 14 for (int i=0;i<nl.getLength();i++){ 15 System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); 16 System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); 17 } 18 }catch(Exception e){ 19 e.printStackTrace(); 20 }
2)SAX
1 import org.xml.sax.*; 2 import org.xml.sax.helpers.*; 3 import javax.xml.parsers.*; 4 public class MyXMLReader extends DefaultHandler { 5 java.util.Stack tags = new java.util.Stack(); 6 public MyXMLReader() { 7 super(); 8 } 9 public static void main(String args[]) { 10 long lasting = System.currentTimeMillis(); 11 try { 12 SAXParserFactory sf = SAXParserFactory.newInstance(); 13 SAXParser sp = sf.newSAXParser(); 14 MyXMLReader reader = new MyXMLReader(); 15 sp.parse(new InputSource("data_10k.xml"), reader); 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } 19 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒");} 20 public void characters(char ch[], int start, int length) throws SAXException { 21 String tag = (String) tags.peek(); 22 if (tag.equals("NO")) { 23 System.out.print("车牌号码:" + new String(ch, start, length)); 24 } 25 if (tag.equals("ADDR")) { 26 System.out.println("地址:" + new String(ch, start, length)); 27 } 28 } 29 public void startElement(String uri,String localName,String qName,Attributes attrs) { 30 tags.push(qName);} 31 }
3) JDOM
1 import java.io.*; 2 import java.util.*; 3 import org.jdom.*; 4 import org.jdom.input.*; 5 public class MyXMLReader { 6 public static void main(String arge[]) { 7 long lasting = System.currentTimeMillis(); 8 try { 9 SAXBuilder builder = new SAXBuilder(); 10 Document doc = builder.build(new File("data_10k.xml")); 11 Element foo = doc.getRootElement(); 12 List allChildren = foo.getChildren(); 13 for(int i=0;i<allChildren.size();i++) { 14 System.out.print("车牌号码:" + ((Element)allChildren.get(i)).getChild("NO").getText()); 15 System.out.println("车主地址:" + ((Element)allChildren.get(i)).getChild("ADDR").getText()); 16 } 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 }
4)DOM4J
1 import java.io.*; 2 import java.util.*; 3 import org.dom4j.*; 4 import org.dom4j.io.*; 5 public class MyXMLReader { 6 public static void main(String arge[]) { 7 long lasting = System.currentTimeMillis(); 8 try { 9 File f = new File("data_10k.xml"); 10 SAXReader reader = new SAXReader(); 11 Document doc = reader.read(f); 12 Element root = doc.getRootElement(); 13 Element foo; 14 for (Iterator i = root.elementIterator("VALUE"); i.hasNext() { 15 foo = (Element) i.next(); 16 System.out.print("车牌号码:" + foo.elementText("NO")); 17 System.out.println("车主地址:" + foo.elementText("ADDR")); 18 } 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 )
java操作xml方式比较与详解(DOM、SAX、JDOM、DOM4J)
标签:
原文地址:http://www.cnblogs.com/starainDou/p/4720830.html