xml的jaxp解析实例
实现对xml文件的增删查等操作:
package rowsy.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import rowsy.bean.Student;
import rowsy.exception.StudentNotExistException;
import rowsy.utils.XmlUtils;
public class StudentDao {
public void add(Student s) {
try {
Document document = XmlUtils.getDocument();
Element studentTag = document.createElement("student");
studentTag.setAttribute("idcard", s.getIdcard());
studentTag.setAttribute("examid", s.getExamid());
Element name = document.createElement("name");
Element location = document.createElement("location");
Element grade = document.createElement("grade");
name.setTextContent(s.getName());
location.setTextContent(s.getLocation());
grade.setTextContent(s.getGrade() + "");
studentTag.appendChild(name);
studentTag.appendChild(location);
studentTag.appendChild(grade);
document.getElementsByTagName("exam").item(0).appendChild(studentTag);
XmlUtils.writeToXml(document);
} catch (Exception e) {
// 转型 unchecked exception
throw new RuntimeException(e);
}
}
public Student find(String examid) {
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");
for (int i = 0; i < list.getLength(); i++) {
Element studentTag = (Element) list.item(i);
if (studentTag.getAttribute("examid").equals(examid)) {
Student s = new Student();
s.setExamid(examid);
s.setIdcard(studentTag.getAttribute("idcarg"));
s.setName(studentTag.getElementsByTagName("name").item(0).getTextContent());
s.setLocation(studentTag.getElementsByTagName("location").item(0).getTextContent());
s.setGrade(Double.parseDouble(studentTag.getElementsByTagName("grade").item(0).getTextContent()));
return s;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String name) throws StudentNotExistException {
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for (int i = 0; i < list.getLength(); i++) {
if (list.item(i).getTextContent().equals(name)) {
Node node = list.item(i).getParentNode();
node.getParentNode().removeChild(node);
XmlUtils.writeToXml(document);
return ;
}
}
throw new StudentNotExistException(name + "not exist!");
} catch (StudentNotExistException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
xml工具类代码如下:
package rowsy.utils;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class XmlUtils {
private static String filename = "src/exam.xml";
public static Document getDocument() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(filename);
}
public static void writeToXml(Document document) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}
}
student实体类如下:
package rowsy.bean;
public class Student {
private String idcard;
private String examid;
private String name;
private String location;
private double grade;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <exam> <student idcard="111" examid="222"> <name>Ronnie</name> <location>Africa</location> <grade>100</grade> </student> </exam>
通过对StudentDao类中函数的调用可以对xml文件中的数据进行操作

浙公网安备 33010602011771号