📄 xmlutil.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 */package crms.util;import org.w3c.dom.*;import org.xml.sax.*;import javax.xml.parsers.*;import java.io.*;import org.apache.log4j.Logger;/** * * @author dmurphy */public class XMLUtil { static Logger logger = Logger.getLogger(XMLUtil.class); /** Creates a new instance of XMLUtil */ private XMLUtil() { } /** * Finds the first node in the node heirarchy that is named * "name". * @param name Name of node to look for * @param rootNode Node to start search at * @return Node found, null if none. */ public static Node getNode(String name, Node rootNode) { if (rootNode == null) { return null; } if (rootNode.getNodeName().equals(name)) { return rootNode; } if (rootNode.hasChildNodes()) { NodeList list = rootNode.getChildNodes(); for (int i=0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeName() != null && node.getNodeName().equals(name)) { // We found the node, return it return node; } else { // This isn't the node we want, so recursively // search with this node as the rootNode, returning // the result if any. Node result = getNode(name, node); if (result != null) { return result; } } } } return null; } /** * Given the rootNode, searches for the first parent node 'parent' it * finds and returns a node list of any children of that parent named * 'child' as a NodeList. * @param parent Name of the parent node to look for. * @param child Name of child elements of the parent to collect. * @param rootNode Node to commence the search from. * @return NodeList object representing the collection of nodes found. */ public static NodeList getChildNodesNamed(String parentName, String childName, Node rootNode) { Element parent = (Element) getNode(parentName, rootNode); return parent.getElementsByTagName(childName); } public static Document createDocument(StringBuffer buf) { return createDocument(buf.toString()); } /** * Creates a DOM document based on the incoming XML text in 'buf'. * @param buf String containing XML character data. * @return DOM XML Document. */ public static Document createDocument(String buf) { Document document = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); logger.debug("Attempting to create document from xml:"); logger.debug(buf.toString()); try { InputSource input_source = new InputSource(new java.io.StringReader(buf)); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(input_source); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } return document; } /** * Friendly call for recursive enumerate document. * @param rootNode Node to start enumerating from. * @param tab Number of spaces to use for tabs. */ public static void enumerateDocument(Node rootNode, int tab) { enumerateDocument(rootNode, 0, tab); } /** * Outputs document structure in tabbed form to System.out * @param rootNode Document node to start outputting from * @param level current tab-level, when calling this use 0 * @param tab Number of spaces to use as tab between parent and child nodes. */ public static void enumerateDocument( Node rootNode, int level ,int tab){ String tabs = ""; for ( int i=0; i < level; i++ ) { tabs += " "; } logger.debug(tabs + "name: " + rootNode.getNodeName()); logger.debug(tabs + "value: " + rootNode.getNodeValue()); if (rootNode.hasChildNodes()) { NodeList children = rootNode.getChildNodes(); for (int i=0; i < children.getLength(); i++) { Node child = children.item(i); enumerateDocument( child, level+tab, tab); } } } /** * <p>Returns the text value of 'node'.<p> * <p>Note, this actually works by searching for the first child * of 'node' that is named "#text" and returns that value. It corresponds * to a chunk of XML like: * <pre><node>Some Text</node></pre> * Calling getTextForNode(node) will return <i>Some Text</i></p> * @param node Node to get text value of. * @return String value of node. */ public static String getTextForNode(Node node) { if (node == null || !node.hasChildNodes()) { return null; } NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i < children.getLength(); i++) { Node child = children.item(i); if ( child.getNodeName().equals("#text")) { return child.getNodeValue(); } } } return null; } /** * <p>Returns text value of the node named 'name' that exists as a * child of 'rootNode'.</p> * @param name The name of the node to find the text value of * @param rootNode The node to begin the search for 'name' from. * @return String value of node if found, null if not. */ public static String getTextForNodeNamed(AbstractCode name, Node rootNode) { return getTextForNodeNamed(name.getName(), rootNode); } public static String getTextForNodeNamed(String name, Node rootNode) { Node node = getNode(name, rootNode); if (rootNode == null || node == null) { return null; } return getTextForNode(getNode(name, rootNode)); } public static String getAttributeValue(String name, Node node) { if (node == null) { return null; } logger.debug("Attempting to get attribute named '" + name + "' from node: " + node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map == null) { return null; } Node attribute = map.getNamedItem(name); if (attribute == null) { return null; } return attribute.getNodeValue(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -