📄 xmlutil.java
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.xml;import org.w3c.dom.*;import java.util.ArrayList;/*** XMLUtil is a utility class to faciliate usage of XML DOM. * They should be useful throughout many projects in CyberDemia.* @author Alexander Yap*/ public class XMLUtil{ /** * Gets named attribute of specified element as a String. * The context is provided for exception messages. * @param elem Element to access. * @param attributeName Name of attribute * @param context Description of this element's context (may be null) * @return Attribute as a String. * @exception XMLUtilException if the named attribute does not exist in elem. */ public static String getStringAttribute( Element elem, String attributeName, String context ) throws XMLUtilException { String strAttr = elem.getAttribute(attributeName); if (strAttr==null) throw new XMLUtilException("Null attribute "+attributeName, elem.getTagName(), context); if (strAttr.length()==0) throw new XMLUtilException("Empty attribute "+attributeName, elem.getTagName(), context); return strAttr; } /** * Gets named attribute of specified element as an int. * The context is provided for exception messages. * @param elem Element to access. * @param attributeName Name of attribute * @param context Description of this element's context (may be null) * @return Attribute as an int. * @exception XMLUtilException * if the named attribute does not exist in elem, or the value cannot be converted to an int. */ public static int getIntAttribute( Element elem, String attributeName, String context ) throws XMLUtilException { String strAttr = getStringAttribute(elem,attributeName,context); int intAttr = 0; try { intAttr = Integer.parseInt( strAttr ); } catch (NumberFormatException nfex) { throw new XMLUtilException("Error converting "+strAttr+" to int", elem.getTagName(), context); } return intAttr; } /** * Gets all named child elements from its parent element. * The context is provided for exception messages. * @param parentElem Parent element. * @param elemName Name of elements to get. * @param context Description of this element's context (may be null) * @return Array of Named elements, or empty array if no matching child found. */ public static Element[] getChildElements( Element parentElem, String elemName, String context) { ArrayList matchList = new ArrayList(); NodeList nodeList = parentElem.getChildNodes(); for (int i=0; i<nodeList.getLength(); i++) { Node child = nodeList.item(i); if ((child instanceof Element) && child.getNodeName().equals(elemName)) { matchList.add(child); } } return (Element[])matchList.toArray(new Element[0]); } /** * Gets first named element from its parent element. This parent element must * contain at least one child element of this name. * The context is provided for exception messages. * @param parentElem Parent element. * @param elemName Name of element to get. * @param context Description of this element's context (may be null) * @return Named element, or null if none. */ public static Element getSingleElement( Element parentElem, String elemName, String context) { NodeList nodeList = parentElem.getChildNodes(); Element elem = null; for (int i=0; i<nodeList.getLength(); i++) { Node child = nodeList.item(i); if ((child instanceof Element) && child.getNodeName().equals(elemName)) { elem = (Element)child; break; } } return elem; } /** * Gets the value contained in a named element as a String, given the parent element. * This parent element must contain at least one child element of this name. * The context is provided for exception messages. * @param parentElem Parent element. * @param elemName Name of element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as a String. * @exception XMLUtilException * if there is not at least one such child element in parentElem. */ public static String getSingleElementStringValue( Element parentElem, String elemName, String context) throws XMLUtilException { Element elem = getSingleElement(parentElem, elemName, context); if (elem==null) { throw new XMLUtilException("Missing element",elemName,context); } return getStringValue(elem, context); } /** * Gets the value contained in the specified element as a String. * The context is provided for exception messages. * @param elem Element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as a String. * @exception XMLUtilException * if there is an error. */ public static String getStringValue( Element elem, String context) throws XMLUtilException { elem.normalize(); if (!elem.hasChildNodes()) throw new XMLUtilException("Missing value", elem.getTagName(), context); Node firstNode = elem.getFirstChild(); if (firstNode.getNodeType()!=Node.TEXT_NODE) throw new XMLUtilException("Invalid data type, expecting TEXT", elem.getTagName(), context); return ((Text)firstNode).getData(); } /** * Gets the value contained in a named element as an int, given the parent element. * This parent element must contain at least one child element of this name. * The context is provided for exception messages. * @param parentElem Parent element. * @param elemName Name of element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as an int. * @exception XMLUtilException * if there is not at least one such child element in parentElem, * or the value cannot be converted to an int. */ public static int getSingleElementIntValue( Element parentElem, String elemName, String context) throws XMLUtilException { Element elem = getSingleElement(parentElem, elemName, context); if (elem==null) { throw new XMLUtilException("Missing element",elemName,context); } return getIntValue(elem, context); } /** * Gets the value contained in the specified element as an int. * The context is provided for exception messages. * @param elem Element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as an int . * @exception XMLUtilException * if there is an error. */ public static int getIntValue( Element elem, String context) throws XMLUtilException { String strVal = getStringValue(elem, context); int intVal = 0; try { intVal = Integer.parseInt( strVal ); } catch (NumberFormatException nfex) { throw new XMLUtilException("Error converting "+strVal+" to int", elem.getTagName(), context); } return intVal; } /** * Gets the value contained in a named element as a byte, given the parent element. * This parent element must contain at least one child element of this name. * The context is provided for exception messages. * @param parentElem Parent element. * @param elemName Name of element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as a byte. * @exception XMLUtilException * if there is not at least one such child element in parentElem, * or the value cannot be converted to a byte. */ public static byte getSingleElementByteValue( Element parentElem, String elemName, String context) throws XMLUtilException { Element elem = getSingleElement(parentElem, elemName, context); if (elem==null) { throw new XMLUtilException("Missing element",elemName,context); } return getByteValue(elem, context); } /** * Gets the value contained in the specified element as a byte. * The context is provided for exception messages. * @param elem Element to access. * @param context Description of this element's context (may be null) * @return Value contained in element as a byte. * @exception XMLUtilException * if there is an error. */ public static byte getByteValue( Element elem, String context) throws XMLUtilException { String strVal = getStringValue(elem, context); byte byteVal = 0; try { byteVal = Byte.parseByte( strVal ); } catch (NumberFormatException nfex) { throw new XMLUtilException("Error converting "+strVal+" to byte", elem.getTagName(), context); } return byteVal; } /** * <p> * Encodes pre-formatted text to safely preserve certain * non-alphanumeric characters such as mathematical symbols, * spaces and new lines. * All characters that are not letters or digits are encoded. * </p> * <p> * The encoded text can be safely stored as content of an XML element. * However, if you want to display the pre-formatted text with XHTML * in a Web browser, you must put it inside the * <code><pre></code> element, or with the equivalent style-sheet. * </p> * * @param text Formatted text to be encoded. * @return Encoded text. */ public static String encodeText(String text) { // Estimate encoded text to be 3 times bigger than original StringBuffer encoded = new StringBuffer(3*text.length()); for (int cidx=0; cidx<text.length(); cidx++) { char ch = text.charAt(cidx); if (Character.isLetterOrDigit(ch)) encoded.append(ch); else encoded.append("&#"+Integer.toString((int)ch)+";"); } return encoded.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -