XML读取两种方法

时间:2024-08-31 19:34:02
 //第一种SAX方法解析

 package a20170722.xmlex;

 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;

 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 parseUserInfoSAX {
 public static void main(String[] args) throws Exception {
 File file=new File("src/a20170722/xmlex/UserInfoxml.xml");
 SAX(file);
 }
 //SAX解析
 public static void SAX(File file) throws Exception{
 //创建解析工厂
 SAXParserFactory sax=SAXParserFactory.newInstance();
 //创建解析对象
 SAXParser saxParser = sax.newSAXParser();
 //创建自定义解析
 MyHeader mh=new MyHeader();
 saxParser.parse(file,mh);
 for (User userlist:mh.userList) {
 System.out.println(userlist);
 }
 }
 }
 class MyHeader extends DefaultHandler{
 List<User> userList=new ArrayList<User>();
 static String name;
 static String age;
 static String address;
 @Override
 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 if(qName.equals("user")){
 name=attributes.getValue("name");
 age=attributes.getValue("age");
 }
 }
 @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
 String str=new String(ch,start,length);
 str=str.trim();
 address=str;
 if((name!=null)){
 if((address.length()>1)){
 userList.add(new User(name,age,address));
 }
 }
 }
 }
 class User{
 String name;
 String age;
 String address;
 public User(String name, String age, String address) {
 super();
 this.name = name;
 this.age = age;
 this.address = address;
 }
 @Override
 public String toString() {
 return "[name]="+name+"[age]="+age+"[address]="+address;
 }

 }

 //第二种DOM方法解析

 package a20170722.xmlex;

 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;

 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;

 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;

 public class parseUserInfoDom {
 public static void main(String[] args) throws Exception {
 File file=new File("src/a20170722/xmlex/UserInfoxml.xml");
 dom(file);
 }
 public static void dom(File file) throws Exception{
 List<UserInfo> userList=new ArrayList<UserInfo>();
 //创建dom解析工厂
 DocumentBuilderFactory DBFactory=DocumentBuilderFactory.newInstance();
 //创建解析对象
 DocumentBuilder DBuilder = DBFactory.newDocumentBuilder();
 Document Dt = DBuilder.parse(file);
 NodeList TagName = Dt.getElementsByTagName("user");
 for (int i = 0; i < TagName.getLength(); i++) {
 Node node = TagName.item(i);
 Element ele=(Element) node;
 String name=ele.getAttribute("name");
 String address=ele.getTextContent();
 if(Integer.parseInt(ele.getAttribute("age"))>18){
 userList.add(new UserInfo(name,ele.getAttribute("age"),address.trim()));
 }
 }
 for (UserInfo list:userList) {
 System.out.println(list);
 }
 }
 }
 class UserInfo{
 String name;
 String age;
 String address;
 public UserInfo(String name, String age, String address) {
 super();
 this.name = name;
 this.age = age;
 this.address = address;
 }
 @Override
 public String toString() {
 return "[name]="+name+"[age]="+age+"[address]="+address;
 }
 }