Exemple #1
0
  /**
   * Returns the value of an attribute for an element.
   *
   * @param element the element to check
   * @param definition the definition of the attribute to retrieve from the element
   * @return the defined attribute value, or <tt>null</tt> if the attribute was not found on the
   *     element
   */
  public static String getAttribute(Element element, XmlNode definition) {
    if (element == null) {
      return null;
    }

    if (definition.isNamespaceAware()) {
      if (element.hasAttributeNS(definition.getNamespace(), definition.getLocalName())) {
        return element.getAttributeNS(definition.getNamespace(), definition.getLocalName());
      }
    } else {
      if (element.hasAttribute(definition.getLocalName())) {
        return element.getAttribute(definition.getLocalName());
      }
    }
    return null;
  }
Exemple #2
0
  /**
   * Returns a sibling element that matches a given definition, or <tt>null</tt> if no match is
   * found.
   *
   * @param sibling the sibling DOM element to begin the search
   * @param target the node to search for
   * @return the matching element, or <tt>null</tt> if not found
   */
  public static Element findSibling(Element sibling, XmlNode target) {
    String xmlName = target.getLocalName();
    String xmlNamespace = target.getNamespace();

    Node node = sibling;
    if (node == null) {
      return null;
    }

    while ((node = node.getNextSibling()) != null) {
      if (node.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element element = (Element) node;
      if (!element.getLocalName().equals(xmlName)) {
        continue;
      }
      if (target.isNamespaceAware()) {
        String ns = element.getNamespaceURI();
        if (ns == null) {
          if (xmlNamespace != null) {
            continue;
          }
        } else {
          if (!ns.equals(xmlNamespace)) {
            continue;
          }
        }
      }
      return element;
    }
    return null;
  }
Exemple #3
0
  /**
   * Finds the Nth matching child of a DOM element.
   *
   * @param parent the parent DOM node
   * @param target the node to search for
   * @param offset the occurrence of the matching node
   * @return the matching element, or <tt>null</tt> if no match is found
   */
  public static Element findChild(Node parent, XmlNode target, int offset) {
    Node node = parent;
    if (node != null) {
      node = node.getFirstChild();
    }
    if (node == null) {
      return null;
    }

    String xmlName = target.getLocalName();
    String xmlNamespace = target.getNamespace();

    int count = 0;
    do {
      if (node.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element element = (Element) node;
      if (!element.getLocalName().equals(xmlName)) {
        continue;
      }
      if (target.isNamespaceAware()) {
        String ns = element.getNamespaceURI();
        if (ns == null) {
          if (xmlNamespace != null && xmlNamespace.length() != 0) {
            continue;
          }
        } else {
          if (!ns.equals(xmlNamespace)) {
            continue;
          }
        }
      }
      if (count == offset) {
        return element;
      }
      ++count;

    } while ((node = node.getNextSibling()) != null);

    return null;
  }