DocumentHelper xml工具包

前端之家收集整理的这篇文章主要介绍了DocumentHelper xml工具包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public class DocumentHelper { public static Document parse(InputStream is) throws ParserConfigurationException,IOException,SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); Document document = factory.newDocumentBuilder().parse(is); return document; } public static Document parse(File file) throws ParserConfigurationException,SAXException { InputStream is = null; try { is = new FileInputStream(file); Document localDocument = parse(is); return localDocument; } finally { if (is != null) is.close(); } } public static Element getElementChild(Element element,String name,boolean required) throws Exception { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); ++i) { Node node = nodes.item(i); if ((node instanceof Element) && (node.getLocalName().equals(name)) && (node.getNamespaceURI() == null)) { return ((Element)node); } } if (required) { throw new Exception("Missing element " + name + " in " + element.getLocalName()); } return null; } public static Element[] getElementChildren(Element element) { List elements = new ArrayList(); NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); ++i) { if (nodes.item(i) instanceof Element) { elements.add((Element)nodes.item(i)); } } return ((Element[])elements.toArray(new Element[0])); } public static Element[] getElementChildren(Element element,String name) { Element[] elements = getElementChildren(element); List taggedElements = new ArrayList(); for (int i = 0; i < elements.length; ++i) { if ((elements[i].getLocalName().equals(name)) && (elements[i].getNamespaceURI() == null)) { taggedElements.add(elements[i]); } } return ((Element[])taggedElements.toArray(new Element[0])); } public static String getAttribute(Element element,boolean required) throws Exception { if (!(element.hasAttribute(name))) { if (required) { throw new Exception("Missing attribute " + name + " on element " + element.getLocalName()); } return null; } return element.getAttribute(name); } public static boolean getBooleanAttribute(Element element,boolean defaultValue) throws Exception { String value = element.getAttribute(name); if (value.equals("")) { return defaultValue; } return value.equalsIgnoreCase("true"); } public static boolean getBooleanElement(Element element,boolean defaultValue) throws Exception { Element child = getElementChild(element,name,false); if (child == null) { return defaultValue; } return getElementText(child,true).equalsIgnoreCase("true"); } public static String getStringElement(Element element,String defaultValue) throws Exception { Element child = getElementChild(element,true); } public static int getIntegerElement(Element element,int defaultValue) throws Exception { Element child = getElementChild(element,false); if (child == null) { return defaultValue; } String text = getElementText(child,true); try { return Integer.parseInt(text); } catch (NumberFormatException e) { throw new RuntimeException("Invalid integer value " + text + " in element " + name); } } public static String getElementText(Element element,boolean required) throws Exception { StringBuilder text = new StringBuilder(30); NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); ++i) { Node node = nodes.item(i); if (node instanceof Text) { text.append(node.getNodeValue()); } } if ((required) && (text.length() == 0)) throw new Exception("Missing text content in element " + element.getLocalName()); if (text.length() == 0) { return null; } return text.toString(); } }

猜你在找的XML相关文章