先建立一个XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id = "00001" name = "张三" sex = "男" birth = "2001-5-13">
<hobby>打篮球</hobby>
<hobby>扣手机</hobby>
<hobby>爬山</hobby>
<hobby>跑步</hobby>
<hobby>追剧</hobby>
<hobby>喝茶</hobby>
</student>
<student id = "00002" name = "李五" sex = "女" birth = "1998-7-13">
<hobby>踢足球</hobby>
<hobby>散步</hobby>
<hobby>追星</hobby>
<hobby>嗑瓜子</hobby>
</student>
</students>
首先,先对XML文件用基本的方式进行解析。
public static void main(String[] args) {
try {
try {
InputStream is = Test.class.getResourceAsStream("/student.att.xml");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(is);
NodeList studentList = document.getElementsByTagName("student");
for (int i = 0;i < studentList.getLength();i++) {
Element studentElement = (Element) studentList.item(i);
String id = studentElement.getAttribute("id");
String name = studentElement.getAttribute("name");
String sex = studentElement.getAttribute("sex");
String birthday = studentElement.getAttribute("birth");
System.out.println("id:" + id + " name:" + name + " sex:" + sex + " birth:" + birthday );
NodeList hobbies = studentElement.getElementsByTagName("hobby");
for (int j = 0;j < hobbies.getLength();j++) {
Element hobbyElement = (Element) hobbies.item(j);
String hobby = hobbyElement.getTextContent();
System.out.println("hobby:" + hobby);
}
System.out.println();
}
} catch (SAXException | IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
结果如下:
现在,单独写一个工具,
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public abstract class XMLParser {
//只需一份就够
public static final DocumentBuilderFactory dbf;
public static DocumentBuilder db;
static {
dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
public XMLParser() {
}
// 获取Document,Document是w3c的,不要导错了
public static Document getDocument(InputStream is) {
Document document = null;
if (is == null) {
return document;
}
try {
document = db.parse(is);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
public static Document getDocument(String xmlPath) {
InputStream is = Class.class.getResourceAsStream(xmlPath);
if (is == null) {
System.out.println("路径 :" + xmlPath + "不存在");
return null;
}
return getDocument(is);
}
// 抽象方法,由用户自行处理得到的标签
public abstract void dealElement(Element element,int index);
// 抽象方法在for循环中,每次读取到一个标签,就交给抽象方法,由用户自行使用
public void parseTag(Document document,String tagName) {
NodeList nodeList = document.getElementsByTagName(tagName);
for (int i = 0;i < nodeList.getLength();i++) {
Element element = (Element) nodeList.item(i);
dealElement(element,i);
}
}
public void parseTag(Element ele,String tagName) {
NodeList nodeList = ele.getElementsByTagName(tagName);
for (int i = 0;i < nodeList.getLength();i++) {
Element element = (Element) nodeList.item(i);
dealElement(element,i);
}
}
}
结果如下: