DocumentHelper xml工具包

来源:互联网 发布:知乎 大胸妹子 编辑:程序博客网 时间:2024/06/05 19:10
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, IOException, 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, String name, 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, String name, boolean defaultValue) throws Exception {
    String value = element.getAttribute(name);
    if (value.equals("")) {
      return defaultValue;
    }
    return value.equalsIgnoreCase("true");
  }


  public static boolean getBooleanElement(Element element, String name, 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 name, String defaultValue) throws Exception {
    Element child = getElementChild(element, name, false);
    if (child == null) {
      return defaultValue;
    }
    return getElementText(child, true);
  }


  public static int getIntegerElement(Element element, String name, int defaultValue) throws Exception {
    Element child = getElementChild(element, name, 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();
  }
}
0 0