JDOM解析XML

Java代码 复制代码
  1. package com.xml.parseXml;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.util.List;   
  6.   
  7. import org.jdom.Document;   
  8. import org.jdom.Element;   
  9. import org.jdom.JDOMException;   
  10. import org.jdom.input.SAXBuilder;   
  11.   
  12. import com.xml.bean.Address;   
  13. import com.xml.bean.ReadXmlFileStream;   
  14.   
  15. public class JDOMParseXml {   
  16.   
  17.     private static Address address = new Address();   
  18.   
  19.     public static void main(String[] args) {   
  20.         long lasting = System.currentTimeMillis();   
  21.         try {   
  22.             SAXBuilder builder = new SAXBuilder();   
  23.             InputStream in = ReadXmlFileStream.getXmlFileStream();   
  24.             Document doc = builder.build(in);   
  25.   
  26.             Element root = doc.getRootElement();   
  27.   
  28.             List allChildren = root.getChildren();   
  29.   
  30.             for (int i = 0; i < allChildren.size(); i++) {   
  31.                 address.setNo(((Element) allChildren.get(i)).getChild("no")   
  32.                         .getTextTrim());   
  33.                 address.setAddr(((Element) allChildren.get(i)).getChild("addr")   
  34.                         .getTextTrim());   
  35.                 System.out.println(address);   
  36.             }   
  37.         } catch (JDOMException e) {   
  38.             e.printStackTrace();   
  39.         } catch (IOException e) {   
  40.             e.printStackTrace();   
  41.         }   
  42.         System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)   
  43.                 + " 毫秒");   
  44.     }   
  45. }  
Java代码 复制代码
  1. package com.xml.parseXml;   
  2.   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.util.List;   
  6.   
  7. import org.jdom.Document;   
  8. import org.jdom.Element;   
  9. import org.jdom.JDOMException;   
  10. import org.jdom.input.SAXBuilder;   
  11.   
  12. import com.xml.bean.Address;   
  13. import com.xml.bean.ReadXmlFileStream;   
  14.   
  15. public class JDOMParseXml {   
  16.   
  17.     private static Address address = new Address();   
  18.   
  19.     public static void main(String[] args) {   
  20.         long lasting = System.currentTimeMillis();   
  21.         try {   
  22.             SAXBuilder builder = new SAXBuilder();   
  23.             InputStream in = ReadXmlFileStream.getXmlFileStream();   
  24.             Document doc = builder.build(in);   
  25.   
  26.             Element root = doc.getRootElement();   
  27.   
  28.             List allChildren = root.getChildren();   
  29.   
  30.             for (int i = 0; i < allChildren.size(); i++) {   
  31.                 address.setNo(((Element) allChildren.get(i)).getChild("no")   
  32.                         .getTextTrim());   
  33.                 address.setAddr(((Element) allChildren.get(i)).getChild("addr")   
  34.                         .getTextTrim());   
  35.                 System.out.println(address);   
  36.             }   
  37.         } catch (JDOMException e) {   
  38.             e.printStackTrace();   
  39.         } catch (IOException e) {   
  40.             e.printStackTrace();   
  41.         }   
  42.         System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)   
  43.                 + " 毫秒");   
  44.     }   

你可能感兴趣的:(java,xml,bean)