JAXP开发工具包j2se的一部分,它由javax.xml、org.w3c.dom、org.xml.sax 包及其子包组成。
在javax.xml.parsers包中,定义了几个工厂类,通过调用这几个工厂类,可以创建dom和sax解析器,对XML文档进行解析。
自己尝试使用了DOM解析器来解析XML文档,给大家分享下经验,第一次写技术BLOG,有缺陷的地方还望大家拍砖。
具体代码和注释如下:
1、创建一个XmlUtils类,这里面写2个方法,一个方法是用来得到Document对象,一个方法用来将内存中的数据写入指定XML文档。
import java.io.FileOutputStream; import java.io.IOException; 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.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class XmlUtils { //定义文档路径 public static String filename = "src/test.xml"; //获取解析后的XML文档方法 public static Document getDocument() throws Exception { //获取Dom工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //获取Dom解析器 DocumentBuilder builder = factory.newDocumentBuilder(); //解析所需文档 return builder.parse(filename); } //将内存中的数据写入XML中 public static void document2Xml(Document document) throws Exception{ //获取工厂实例 TransformerFactory tffactory=TransformerFactory.newInstance(); //获取Transformer对象 Transformer tf =tffactory.newTransformer(); //调用transform(Source xmlSource,Result outputTarget) 方法,将内存数据写入指定XML tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream(filename))); } }
2、用XML作为数据库存储学生的信息(姓名,城市和身份证号),创建一个StudentBean类,来获取Student对象
public class StudentBean { private String name; private double idcard; private String city; public String getName() { return name; } public void setName(String name) { this.name = name; } public double getIdcard() { return idcard; } public void setIdcard(double idcard) { this.idcard = idcard; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
3、StudentDao类来处理对XML增删查的功能
import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import cn.shenyoujun.Bean.StudentBean; import cn.shenyoujun.Exception.StudentNotExistException; import cn.shenyoujun.XmlUtils.XmlUtils; public class StudentDao { //增加学生信息方法 public void add(StudentBean student) { try { Document document = XmlUtils.getDocument(); Element student_tag = document.createElement("student"); student_tag.setAttribute("name",student.getName()); Element idcard = document.createElement("idcard"); Element city = document.createElement("city"); idcard.setTextContent(student.getIdcard() + ""); city.setTextContent(student.getCity()); student_tag.appendChild(idcard); student_tag.appendChild(city); document.getElementsByTagName("exam").item(0) .appendChild(student_tag); XmlUtils.document2Xml(document); System.out.print("添加成功"); } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } //删除学生信息方法 public void remove(String name) throws StudentNotExistException { try { Document document = XmlUtils.getDocument(); NodeList list = document.getElementsByTagName("student"); StudentBean sb = new StudentBean(); for (int i = 0; i < list.getLength(); i++) { Element student = (Element) list.item(i); if (student.getAttribute("name").equals(name)) { list.item(i).getParentNode().removeChild(list.item(i)); XmlUtils.document2Xml(document); } } throw new StudentNotExistException(name + "不存在!"); } catch (StudentNotExistException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } //寻找学生信息方法 public StudentBean search(String name) { try { Document document = XmlUtils.getDocument(); NodeList list = document.getElementsByTagName("student"); StudentBean sb = new StudentBean(); for (int i = 0; i < list.getLength(); i++) { Element student = (Element) list.item(i); if (student.getAttribute("name").equals(name)) { sb.setName(name); sb.setIdcard(Double.parseDouble(student .getElementsByTagName("idcard").item(0) .getTextContent())); sb.setCity(student.getElementsByTagName("city").item(0) .getTextContent()); return sb; } } return null; } catch (Exception e) { // TODO Auto-generated catch block throw new RuntimeException(e); } } }
4、最后写个单元测试类,来测试这些功能
package Junit; import org.junit.Test; import cn.shenyoujun.Bean.StudentBean; import cn.shenyoujun.Dao.StudentDao; import cn.shenyoujun.Exception.StudentNotExistException; public class TestStudentDao { @Test public void testAdd() { StudentDao sd = new StudentDao(); StudentBean s = new StudentBean(); s.setName("张三"); s.setCity("nanjing"); s.setIdcard(2); sd.add(s); } @Test public void testSearch(){ StudentDao sd = new StudentDao(); String name="张三"; StudentBean sb=sd.search(name); System.out.print(sb.getName()+" "+sb.getCity()+" "+sb.getIdcard()); } @Test public void testRemove(){ StudentDao sd = new StudentDao(); String name="张三"; try { sd.remove(name); } catch (StudentNotExistException e) { e.printStackTrace(); } } }
总结下Dom解析的优劣点:
使用Dom解析的原理是将所有的标签和熟悉都解析成对象存入到内存中,优点是对做增删改的操作比较方便
但是缺点是对内存消耗大,不宜做查询。