在线音乐播放网站模板,wordpress怎么上传php,深圳网站搭建多少钱,方微商城网站开发文档对象模型 (DOM) 是一个文档标准#xff0c;对于完备的文档和复杂的应用程序#xff0c;DOM 提供了大量灵活性。DOM标准是标准的。它很强壮且完整#xff0c;并且有许多实现。这是许多大型安装的决定因素--特别是对产品应用程序#xff0c;以避免在API发生改变时进行大量…文档对象模型 (DOM) 是一个文档标准对于完备的文档和复杂的应用程序DOM 提供了大量灵活性。DOM标准是标准的。它很强壮且完整并且有许多实现。这是许多大型安装的决定因素--特别是对产品应用程序以避免在API发生改变时进行大量的改写。以上是我在选择处理XML数据时之所以没有选择JDOM或者dom4j等其它面向对象的标准的原因不过也由于DOM从一开始就是一种与语言无关的模型而且它更趋向用于像C或Perl这类语言没有利用Java的面向对象的性能所以在使用的过程中也遇到了不少的麻烦今天这里做一个小结。另外我目前使用XML主要是作为数据传输的统一格式并统一用户界面展示的接口应用的面并不是很广所以使用到的DOM的内容其实不多。在准备使用它的时候是做了充足的准备的也有遇到困难的准备所以一开始就有了一个简单的工具类来封装DOM对象使用时必要的公共方法实际证明这样做是很明智的一个简单的创建Document对象的操作要是每次都需要写上5行以上代码并且还要处理那些烦人的Exception实在是会打击大家的积极性所以在最初做了一个XMLTool类专门封装了如下的公共方法1、 Document对象创建(包括空的Document对象创建以一个给定Node节点作为根节点创建。2、 将一个规范的XML字符串转换成一个Document对象。3、 从物理硬盘读取一个XML文件并返回一个Document对象。4、 将一个Node对象转换成字符串。其中每个方法都截获相关的DOM操作所抛出的异常转换成一个RuntimeException抛出这些异常在实际使用过程中一般状况下其实都不会抛出特别是象生成一个Document对象时的ParserConfigurationException、转换Node节点成字符串时要生成一个Transformer对象时的TransformerConfigurationException等等没有必要在它们身上花时间精力。而且真就出了相关的异常的话其实根本没有办法处理这样的状况通常是系统环境配置有问题(比如必要的DOM实现解析器等包没有加入环境)所以包装该异常时只是很简要的获取其Message抛出。代码如下/*** 初始化一个空Document对象返回。* return a Document*/public static Document newXMLDocument() {try {return newDocumentBuilder().newDocument();} catch (ParserConfigurationException e) {throw new RuntimeException(e.getMessage());}}/*** 初始化一个DocumentBuilder* return a DocumentBuilder* throws ParserConfigurationException*/public static DocumentBuilder newDocumentBuilder()throws ParserConfigurationException {return newDocumentBuilderFactory().newDocumentBuilder();}/*** 初始化一个DocumentBuilderFactory* return a DocumentBuilderFactory*/public static DocumentBuilderFactory newDocumentBuilderFactory() {DocumentBuilderFactory dbf DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);return dbf;}/*** 将传入的一个XML String转换成一个org.w3c.dom.Document对象返回。* param xmlString 一个符合XML规范的字符串表达。* return a Document*/public static Document parseXMLDocument(String xmlString) {if (xmlString null) {throw new IllegalArgumentException();}try {return newDocumentBuilder().parse(new InputSource(new StringReader(xmlString)));} catch (Exception e) {throw new RuntimeException(e.getMessage());}}/*** 给定一个输入流解析为一个org.w3c.dom.Document对象返回。* param input* return a org.w3c.dom.Document*/public static Document parseXMLDocument(InputStream input) {if (input null) {throw new IllegalArgumentException(参数为null);}try {return newDocumentBuilder().parse(input);} catch (Exception e) {throw new RuntimeException(e.getMessage());}}/*** 给定一个文件名获取该文件并解析为一个org.w3c.dom.Document对象返回。* param fileName 待解析文件的文件名* return a org.w3c.dom.Document*/public static Document loadXMLDocumentFromFile(String fileName) {if (fileName null) {throw new IllegalArgumentException(未指定文件名及其物理路径);}try {return newDocumentBuilder().parse(new File(fileName));} catch (SAXException e) {throw new IllegalArgumentException(目标文件( fileName )不能被正确解析为XML\n e.getMessage());} catch (IOException e) {throw new IllegalArgumentException(不能获取目标文件( fileName )\n e.getMessage());} catch (ParserConfigurationException e) {throw new RuntimeException(e.getMessage());}}/*** 给定一个节点将该节点加入新构造的Document中。* param node a Document node* return a new Document*/public static Document newXMLDocument(Node node) {Document doc newXMLDocument();doc.appendChild(doc.importNode(node, true));return doc;}/*** 将传入的一个DOM Node对象输出成字符串。如果失败则返回一个空字符串。* param node DOM Node 对象。* return a XML String from node*/public static String toString(Node node) {if (node null) {throw new IllegalArgumentException();}Transformer transformer newTransformer();if (transformer ! null) {try {StringWriter sw new StringWriter();transformer.transform(new DOMSource(node),new StreamResult(sw));return sw.toString();} catch (TransformerException te) {throw new RuntimeException(te.getMessage());}}return errXMLString(不能生成XML信息);}/*** 将传入的一个DOM Node对象输出成字符串。如果失败则返回一个空字符串。* param node DOM Node 对象。* return a XML String from node*/public static String toString(Node node) {if (node null) {throw new IllegalArgumentException();}Transformer transformer newTransformer();if (transformer ! null) {try {StringWriter sw new StringWriter();transformer.transform(new DOMSource(node),new StreamResult(sw));return sw.toString();} catch (TransformerException te) {throw new RuntimeException(te.getMessage());}}return errXMLString(不能生成XML信息);}/*** 获取一个Transformer对象由于使用时都做相同的初始化所以提取出来作为公共方法。* return a Transformer encoding gb2312*/public static Transformer newTransformer() {try {Transformer transformer TransformerFactory.newInstance().newTransformer();Properties properties transformer.getOutputProperties();properties.setProperty(OutputKeys.ENCODING, gb2312);properties.setProperty(OutputKeys.METHOD, xml);properties.setProperty(OutputKeys.VERSION, 1.0);properties.setProperty(OutputKeys.INDENT, no);transformer.setOutputProperties(properties);return transformer;} catch (TransformerConfigurationException tce) {throw new RuntimeException(tce.getMessage());}}/*** 返回一段XML表述的错误信息。提示信息的TITLE为系统错误。之所以使用字符串拼装主要是这样做一般* 不会有异常出现。* param errMsg 提示错误信息* return a XML String show err msg*/public static String errXMLString(String errMsg) {StringBuffer msg new StringBuffer(100);msg.append(?xml version\1.0\ encoding\gb2312\ ?);msg.append();return msg.toString();}/*** 返回一段XML表述的错误信息。提示信息的TITLE为系统错误* param errMsg 提示错误信息* param errClass 抛出该错误的类用于提取错误来源信息。* return a XML String show err msg*/public static String errXMLString(String errMsg, Class errClass) {StringBuffer msg new StringBuffer(100);msg.append(?xml version\1.0\ encoding\gb2312\ ?);msg.append( errMsg \ errSource\ errClass.getName() \/);return msg.toString();}/*** 返回一段XML表述的错误信息。* param title 提示的title* param errMsg 提示错误信息* param errClass 抛出该错误的类用于提取错误来源信息。* return a XML String show err msg*/public static String errXMLString(String title,String errMsg,Class errClass) {StringBuffer msg new StringBuffer(100);msg.append(?xml version\1.0\ encoding\gb2312\ ?);msg.append( title \ errMsg\ errMsg \ errSource\ errClass.getName() \/);return msg.toString();}以上都是DOM的基本应用所以就不一一详细说明了。在实际使用过程中有几种状况使用很频繁但是DOM的接口的设计却使该操作很麻烦所以分别添加了相应的处理方法。其中最麻烦的要数获取一个节点的Text子节点文本信息了如下的XML节点text在拥有element节点对象时要获取其中的文本信息text首先要获取element节点的子节点列表要判断其是否存在子节点如果存在那么遍历其子节点找到一个TextNode节点通过getNodeValue()方法来获取该文本信息由于这里element节点没有信息时没有子节点所以必须判断element节点是否存在子节点才能去访问真正包含了文本信息的TextNode节点那么如果要处理的数据都是以这种形式给出的就会增加大量的开发代码同时让开发工作枯燥无味因此这里使用了一个默认的约定实现就是给出了一个公共方法该方法取给定Node下的直接子节点的Text节点文本信息如果不存在Text节点则返回null这个约定虽然使该方法的使用有所限制也可能导致错误使用该方法但是按实际使用的状况来看这样的约定和使用方式是没有问题的因为实际用到的都是上面举的例子的状况代码/*** 这个方法获取给定Node下的Text节点文本信息如果不存在Text节点则返回null。* 注意是直接子节点相差2层或2层以上不会被考虑。* param node a Node 一个Node。* return a String 如果给定节点存在Text子节点则返回第一个访问到的Text子节点文本信息如果不存在则返回null。*/public static String getNodeValue(Node node) {if (node null) {return null;}Text text getTextNode(node);if (text ! null) {return text.getNodeValue();}return null;}/*** 这个方法获取给定Node下的Text节点如果不存在Text节点则返回null。* 注意是直接子节点相差2层或2层以上不会被考虑。* param node a Node 一个Node。* return a Text 如果给定节点存在Text子节点则返回第一个访问到的Text子节点如果不存在则返回null。*/public static Text getTextNode(Node node) {if (node null) {return null;}if (node.hasChildNodes()) {NodeList list node.getChildNodes();for (int i 0; i list.getLength(); i) {if (list.item(i).getNodeType() Node.TEXT_NODE) {return (Text) list.item(i);}}}return null;}上面代码将获取给定Node节点的直接Text子节点分开包装。另一个很经常碰到的状况是我希望直接定位到目标节点获取该节点对象而不需要通过一层一层的节点遍历来找到目标节点DOM2接口中至少提供了如下的方式来定位节点1、 对于Document对象1) getDocumentElement()