解析xml文件:DomParser_Dom4j_SaxParser

COPY HYD:

实体包:Student类(stuno stuname skill


student.xml

<?xml version="1.0" encoding="UTF-8"?>
<students>

<student skill="playcard">
 <stuno>1</stuno>
 <stuname>jack</stuname>
</student>

<student skill="study">
<stuno>2</stuno>
 <stuname>rose</stuname>
</student>

<student skill="sleep">
<stuno>3</stuno>
 <stuname>lisi</stuname>
</student>

<student skill="eat">
<stuno>4</stuno>
 <stuname>wc</stuname>
</student>

</students>


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<Student> 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<Element> 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<Element> 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<Student> 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<Student> 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);
}


}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值