xml的jaxp解析实例
来源:互联网 发布:cst电磁仿真软件 编辑:程序博客网 时间:2024/05/16 04:56
实现对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 exceptionthrow 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文件中的数据进行操作
- xml的jaxp解析实例
- xml的解析与jaxp解析器
- 20110529-Day01 Xml的解析,jaxp
- xml的dom解析(jaxp实现)
- XML的Jaxp中DOM解析
- XML的Jaxp中的SAX解析
- Jaxp解析XML的基本方法
- java--JAXP对xml文档的解析
- jaxp的sax方式解析xml
- 使用JAXP解析XML
- jaxp对xml解析
- Java JAXP 解析XML
- JAXP解析XML文档
- JAXP解析XML
- jaxp解析xml
- XML之JAXP解析
- jaxp解析xml
- XML解析之JAXP
- svn 修改url地址
- 杂记
- 跳转到网页 HtmlPage.PopupWindow
- 编程中无穷大的设定
- C/C++对汉字的读取
- xml的jaxp解析实例
- uboot 配置文件
- iPhone开发【十七】多线程开发之NSOperation&NSOperationQueue——异步下载图片
- Android开发 设置开机自动启动
- slab分配器
- [Linker Error] Unresolved external 'WinMain' referenced from C0W32.OBJ
- 乔布斯究竟是怎样造就的
- POJ1384&HDU1114
- 室内花卉