面向对象程序设计思想的重点是“工具化”编程思想,之前简单实现XML解析的代码有许多重复的部分,因此可以考虑将其工具化,为以后XML解析提供便利。
DocumentBuilder 的对象对于不同的xml文件解析时,只需要一份即可,所以可以将其通过静态本地块设置为static成员(在类加载后未产生xml解析类对象前,就已经产生了一个DocumentBuilder 的对象,由于静态本地块只被执行一次,所以该对象仅一份)
parse方法“打开”要处理的XML文件,并得到Document类对象 这一步需要用户提供XML的路径,将其生成一个方法如下: public static Document loadXML(String xmlPath) throws Exception { InputStream is = XMLParser.class.getResourceAsStream(xmlPath); return loadXML(is); }根据需要解析各元素
public abstract void dealElement(Element element,int index); // 这个方法的实现应该由使用该工具的用户完成 // 根据需要解析的xml文件来确定如何处理某些元素
XML解析工具化所有代码如下:
package com.mec.util; 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 { private static DocumentBuilder db; static { try { db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } public XMLParser() { } public static Document loadXml(InputStream is) throws SAXException, IOException { return db.parse(is); } public static Document loadXml(String xmlpath) throws SAXException, IOException { InputStream is = XMLParser.class.getResourceAsStream(xmlpath); return loadXml(is); } public abstract void dealElement(Element element,int index); public void parse(Document document,String tagName) { NodeList nodeList = document.getElementsByTagName(tagName); for(int index = 0;index < nodeList.getLength();index++) { Element element = (Element) nodeList.item(index); dealElement(element, index); } } public void parse(Element element,String tagName) { NodeList nodeList = element.getElementsByTagName(tagName); for(int index = 0;index < nodeList.getLength();index++) { Element ele = (Element) nodeList.item(index); dealElement(ele, index); } } }工具的使用
<?xml version="1.0" encoding="UTF-8"?> <!-- 文档声明 --> <players> <!-- 根标签 --> <player name = "sehun" age = "19"> <!-- 子标签 player ;属性 name、age--> <hobbies> <!-- 子标签 hobbies --> <hobby>篮球</hobby> <!-- 标签 文本内容 --> <hobby>跑步</hobby> <hobby>游泳</hobby> <hobby>唱歌</hobby> </hobbies> </player> <player name = "vivi" age = "20"> <hobbies> <hobby>游泳</hobby> <hobby>跑步</hobby> </hobbies> </player> <player name = "demo" age = "18"> <hobbies> <hobby>篮球</hobby> <hobby>跑步</hobby> <hobby>旅行</hobby> </hobbies> </player> </players> package com.mec.util.parser; import org.w3c.dom.Element; public class Test { public static void main(String[] args) { try { new XMLParser() { @Override public void dealElement(Element element, int index) { String playerName = element.getAttribute("name"); String age = element.getAttribute("age"); System.out.println("姓名:" + playerName + " 年龄 :" + age); System.out.println(" 爱好:"); new XMLParser() { @Override public void dealElement(Element element, int index) { String hobbyName = element.getTextContent(); //取得hobby标签的文本内容 System.out.print( " "+ hobbyName); } }.parse(element, "hobby"); System.out.println(); } }.parse(XMLParser.loadXML("/test.xml"), "player"); } catch (Exception e) { e.printStackTrace(); }; } }运行结果
姓名:sehun 年龄 :19 爱好: 篮球 跑步 游泳 唱歌 姓名:vivi 年龄 :20 爱好: 游泳 跑步 姓名:demo 年龄 :18 爱好: 篮球 跑步 旅行