sweet

android学习

xml的两种解析方法

 
 一个student类:
package com.xml.test;
public class Student {
 
 private String name;
 private String sex;
 private int age;
 
 public Student(){}
 
 public Student(String name, String sex, int age) {
  super();
  this.name = name;
  this.sex = sex;
  this.age = age;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
 public String toString(){
  return "姓名:"+name+" 性别:"+sex+" 年龄:"+age;
 }
 
 
}

----------------------------------------------------------------------------------------------------------------------
 
dom解析
package com.xml.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DomTest {
 
 public void createXml(){
  try {
   DocumentBuilderFactory factory = 
    DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = 
    factory.newDocumentBuilder();
   Document doc = builder.newDocument();
   Element root = doc.createElement("students");
   //设置根节点
   doc.appendChild(root);
   
   //创建二级节点
   Element stu1 = doc.createElement("student");
   Element stu2 = doc.createElement("student");
   Element stu3 = doc.createElement("student");
   
   root.appendChild(stu1);
   root.appendChild(stu2);
   root.appendChild(stu3);
   
   //创建三级节点
   Element name1 = doc.createElement("name");
   name1.setTextContent("康");
   Element sex1 = doc.createElement("sex");
   sex1.setTextContent("男");
   Element age1 = doc.createElement("age");
   age1.setTextContent("29");
   
   stu1.appendChild(name1);
   stu1.appendChild(sex1);
   stu1.appendChild(age1);
   
   
   Element name2 = doc.createElement("name");
   name2.setTextContent("sweet");
   Element sex2 = doc.createElement("sex");
   sex2.setTextContent("女");
   Element age2 = doc.createElement("age");
   age2.setTextContent("20");
   
   stu2.appendChild(name2);
   stu2.appendChild(sex2);
   stu2.appendChild(age2);
   
   Element name3 = doc.createElement("name");
   name3.setTextContent("大神");
   Element sex3 = doc.createElement("sex");
   sex3.setTextContent("男");
   Element age3 = doc.createElement("age");
   age3.setTextContent("25");
   
   stu3.appendChild(name3);
   stu3.appendChild(sex3);
   stu3.appendChild(age3);
   
   TransformerFactory tranf = 
TransformerFactory.newInstance();
   Transformer trans = tranf.newTransformer();
   //给文件转换器设置字符编码
   trans.setOutputProperty("encoding", "utf-8");
   trans.setOutputProperty(OutputKeys.INDENT, "yes");
   //定义转换器输入对象
   DOMSource source = new DOMSource(doc);
   //定义转换器输出对象
   Result target = new StreamResult("test.xml");
   trans.transform(source, target);
   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public List<Student> parseXml(String path){
  try {
   DocumentBuilderFactory factory = 
    DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = 
    factory.newDocumentBuilder();
   Document doc = builder.parse(path);
   NodeList nl = doc.getElementsByTagName("student");
   List<Student> list = new ArrayList<Student>();
   for(int i=0;i<nl.getLength();i++){
    Element e = (Element)nl.item(i);
    String name = e.getElementsByTagName("name").item
(0).getFirstChild().getNodeValue();
    String sex = e.getElementsByTagName("sex").item
(0).getFirstChild().getNodeValue();
    String age = e.getElementsByTagName("age").item
(0).getFirstChild().getNodeValue();
    Student stu = new Student(name,sex,Integer.parseInt
(age));
    list.add(stu);
   }
   return list;
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
 
 public static void main(String[] args) {
  DomTest dt = new DomTest();
//  List<Student> list = dt.parseXml("test.xml");
//  for (Iterator iterator = list.iterator(); 
iterator.hasNext();) {
//   Student student = (Student) iterator.next();
//   System.out.println(student);
//   
//  }
  dt.createXml();
 }
}
 

----------------------------------------------------------------------------------------------------------------------
sax解析
package com.xml.test;
import java.util.ArrayList;
import java.util.Iterator;
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 SaxTest {
 public List<Student> saxParse(String path) {
  try {
   SAXParserFactory factory = 
    SAXParserFactory.newInstance();
   SAXParser parser = factory.newSAXParser();
   List<Student> list = new ArrayList<Student>();
   parser.parse(path, new MyHandler(list));
   return list;
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
  
 }
 
 public static void main(String[] args) {
  SaxTest st = new SaxTest();
  List<Student> list = st.saxParse("student.xml");
  for (Iterator iterator = list.iterator(); 
iterator.hasNext();) {
   Student student = (Student) iterator.next();
   System.out.println(student);
   
  }
 }
}
class MyHandler extends DefaultHandler{
 
 private List list;
 private Student stu;
 private String target = "";
 
 public MyHandler(List list){
  this.list = list;
  
 }
 
 //开始文档
 @Override
 public void startDocument() throws SAXException {
  System.out.println("开始解析文档");
 }
 
 //开始解析节点
 @Override
 public void startElement(String uri, String localName, 
String qName,
   Attributes attributes) throws SAXException {
  System.out.println("开始解析节点:uri:"+uri
    +" localName:"+localName+" qName:"+qName);
  target = qName;
  if(qName.equals("student")){
   stu = new Student();
  }
 }
 
 //节点内容
 @Override
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  String content = new String(ch,start,length);
  System.out.println("节点内容:"+content);
  if(target.equals("name")){
   stu.setName(content);
  }else if(target.equals("sex")){
   stu.setSex(content);
  }else if(target.equals("age")){
   stu.setAge(Integer.parseInt(content));
  }
 }
 
 //结束解析节点
 @Override
 public void endElement(String uri, String localName, 
String qName)
   throws SAXException {
  System.out.println("结束解析节点:uri:"+uri
    +" localName:"+localName+" qName:"+qName);
  target = "";
  if(qName.equals("student")){
   list.add(stu);
  }
 }
 
 //文档解析结束
 @Override
 public void endDocument() throws SAXException {
  System.out.println("结束解析文档");
 }
 
}
 
 
 
 
 
 
 


 

阅读更多
个人分类: java
想对作者说点什么? 我来说一句

JSON和XML解析的Demo

2016年04月21日 19.25MB 下载

xml解析方法

2017年12月20日 1KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭