Java 5---xml的使用與解析
阿新 • • 發佈:2018-12-22
導讀
1.xml簡介
2.xml解析
xml簡介
在ecplise中使用xml
也可以在other->搜尋xml,選擇xml file
要將字尾定義為.xml
在open with中選擇xml Editor,如果沒有,在other中檢視,如果還沒有,那就是沒有配置
xml解析
SAX解析XML
例項
stus_info.xml
<?xml version="1.0" encoding="UTF-8"?> <students> <student> <id>1</id> <name>小明</name> <sex>男</sex> <age>18</age> </student> <student id="2"> <name>小紅</name> <sex>女</sex> <age>19</age> </student> </students>
Student.java
package com.hala.xml; public class Student { private int id; private String name; private int age; private String sex; public Student() { super(); } public Student(int id, String name, int age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
MyHandler.java
package com.hala.xml; import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyHandler extends DefaultHandler { //用來裝處理好的學生類物件 private List<Student> list; //儲存剛建立的學生物件 private Student stu; //記錄標籤名 private String tagName; public List<Student> getList() { return list; } @Override public void startDocument() throws SAXException { System.out.println("開始文件"); list=new ArrayList<>(); } @Override public void endDocument() throws SAXException { System.out.println("結束文件"); } /** * qName:標籤名 * attributes:屬性 */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("開始元素"); if(qName.equals("student")){ stu=new Student(); } //記錄這個開始標籤 tagName=qName; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("結束元素"); if(qName.equals("student")){ list.add(stu); stu=null; } tagName=null; } /** * ch:用陣列表示的內容 * start:內容開始位置 * length:內容的長度 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println("處理內容"); //去掉空格後轉化為字串 String content=new String(ch,start,length).trim(); if(!content.equals("")){ if(tagName.equals("id")){ stu.setId(Integer.valueOf(content)); }else if(tagName.equals("name")){ stu.setName(content); }else if(tagName.equals("age")){ stu.setAge(Integer.parseInt(content)); }else if(tagName.equals("sex")){ stu.setSex(content); } } } }
SAXParserDemo.java
package com.hala.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class SAXParserDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Student> list=parse("stus_info.xml");
for(Student stu:list){
System.out.println(stu);
}
}
/**
* 利用sax解析器解析檔案
* @param path 檔案路徑
*/
public static List<Student> parse(String path){
//1.獲取SAX解析器工廠物件
SAXParserFactory factory=SAXParserFactory.newInstance();
//2.利用解析器工廠物件獲取解析器物件
try {
SAXParser parser=factory.newSAXParser();
//3.解析
MyHandler handler=new MyHandler();
parser.parse(new FileInputStream(path),handler);
//獲取集合
return handler.getList();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}