java中XML解析
程序员文章站
2022-06-17 08:25:59
...
原生xml解析
package com.qust;
import com.qust.entity.Student;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestXML {
@Test
public void testXMLPare(){
try {
System.out.println(studentList("src/student.xml"));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
public List<Student> studentList(String xmlFile) throws ParserConfigurationException, IOException, SAXException {
List<Student> students = new ArrayList<>();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFile);
Student student = null;
NodeList nodeList = document.getElementsByTagName("student");
for (int i=0;i<nodeList.getLength();i++){
student = new Student();
Node item = nodeList.item(i);
NodeList childNodes = item.getChildNodes();
for (int j=0;j<childNodes.getLength();j++){
Node item1 = childNodes.item(j);
switch (item1.getNodeName()){
case "sname":
student.setSname(item1.getTextContent());
break;
case "sex":
student.setSex(item1.getTextContent());
break;
}
}
students.add(student);
}
return students;
}
}
DOM4J方式解析XML
导入依赖
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
解析xml文件
package com.qust;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestXML {
@Test
public void testXMLPare() throws Exception {
System.out.println(studentList("src/student.xml"));
}
public List<Student> studentList(String xmlFile) throws Exception {
List<Student> students = new ArrayList<>();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlFile);
Element rootElement = document.getRootElement();
List<Element> elements = rootElement.elements();
Student student = null;
for (Element element : elements){
List<Element> list = element.elements();
student = new Student();
for (Element element1 : list) {
switch (element1.getName()){
case "sname":
student.setSname(element1.getText());
break;
case "sex":
student.setSex(element1.getText());
break;
}
}
students.add(student);
}
return students;
}
}
文件结构和原生相同
下一篇: JS高级回顾——第五篇