解析xml文件:DomParser_Dom4j_SaxParser

COPY HYD:

实体包:Student类(stuno stuname skill


student.xml





 1
 jack



2
 rose



3
 lisi



4
 wc




Dom4j

Web-Content -->WEB-INF-->lib-->dom4j-1.6.jar

TIPS:网站:FireJar

package com.igeekhome.xml;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import com.igeekhome.pojo.Student;


public class Dom4jTest {


public static void main(String[] args) {


List students = new ArrayList<>();
SAXReader reader = new SAXReader();
try {
Document doc = reader.read(Student.class.getClassLoader().getResourceAsStream("students.xml"));
Element root = doc.getRootElement();
System.out.println(root.getName());
Iterator eles = root.elementIterator();
while (eles.hasNext()) {
Element ele = eles.next();
System.out.println(ele.getName());
Student student = new Student();
// 获取属性
// System.out.println(ele.attribute("skill").getValue());;
student.setSkill(ele.attribute("skill").getValue());


Iterator secondEleIt = ele.elementIterator();
while (secondEleIt.hasNext()) {
Element currentEle = secondEleIt.next();
if (currentEle.getName().equals("stuno")) {
student.setStuNo(currentEle.getTextTrim());
System.out.println("学号:" + currentEle.getTextTrim());
} else if (currentEle.getName().equals("stuname")) {
student.setStuName(currentEle.getTextTrim());
}
}


students.add(student);


}


} catch (DocumentException e) {
e.printStackTrace();
}


System.out.println(students);
}


}



DomParser

package com.igeekhome.xml;


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


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


import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import com.igeekhome.pojo.Student;


public class DomParser {


public static void main(String[] args) {


List lists = new ArrayList<>();
// 创建工程类对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// ͨ通过工厂类对象创建DOM解析核心对象 DocumentBuilder
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
// 传入文件生成解析文档对象
Document doc = db.parse(Student.class.getClassLoader().getResourceAsStream("students.xml"));
// 获取根节点
Element root = doc.getDocumentElement();
System.out.println("根节点是:" + root.getTagName());// 获取节点名称
// 返回所有一级子节点
NodeList nodeList = root.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);// 取出当前节点
if (node.getNodeType() == Node.ELEMENT_NODE) {// 筛选节点类型为元素的节点删除其
System.out.println("二:" + node.getNodeName());
// Element是node子类
Element ele = (Element) node;
String skill = ele.getAttribute("skill");
System.out.println(skill);


Student stu = new Student();
stu.setSkill(skill);


NodeList secondNodeList = ele.getChildNodes();
for (int j = 0; j < secondNodeList.getLength(); j++) {
Node secondNode = secondNodeList.item(j);// 获取当前二级子节点
if (secondNode.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(secondNode.getNodeName());
if (secondNode.getNodeName().equals("stuno")) {
System.out.println("学号:" + secondNode.getTextContent());
stu.setStuNo(secondNode.getTextContent());
} else if (secondNode.getNodeName().equals("stuname")) {
System.out.println("姓名:" + secondNode.getTextContent());
stu.setStuName(secondNode.getTextContent());
}


}


}


lists.add(stu);


}


}


} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


System.out.println(lists);
}


}


SaxPaser

package com.igeekhome.xml;


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


import javax.xml.parsers.ParserConfigurationException;
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;


import com.igeekhome.pojo.Student;


public class SaxPaser {


public static void main(String[] args) {


SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = null;
try {
parser = parserFactory.newSAXParser();
SAX sax = new SAX();
parser.parse(Student.class.getClassLoader().getResourceAsStream("students.xml"), sax);
System.out.println(sax.lists);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}


class SAX extends DefaultHandler {


public List lists; // 存放解析后的数据集合
public Student student;// 解析student节点时赋值当前学生对象
public String tagName;// 记录当前的节点名


// 开始对xml文档进行解析只会调用一次
@Override
public void startDocument() throws SAXException {


lists = new ArrayList<>();// 初始化集合
}


// 遇到元素调用 调用多次
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {


System.out.println(qName);// qName节点名
if (qName.equals("student")) {
student = new Student();
// System.out.println("技能:"+);
student.setSkill(attributes.getValue("skill"));
}
tagName = qName;// 指向当前正在解析的元素
}


// 结束元素调用 调用多次
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {


if (qName.equals("student")) {
lists.add(student);
}


tagName = null;
// System.out.println("结束");


}


// 结束对整个文档的解析
@Override
public void endDocument() throws SAXException {


super.endDocument();
}


// 处理文档节点——处理字符,多次
@Override
public void characters(char[] ch, int start, int length) throws SAXException {


String str = new String(ch, start, length);
// 取出所需要的标签的值
if ("stuno".equals(tagName)) {
System.out.println("学号=" + str);
student.setStuNo(str);
}
if ("stuname".equals(tagName)) {
System.out.println("姓名=" + str);
student.setStuName(str);
}


}


}

你可能感兴趣的:(xml)