Example usage for org.w3c.dom Node getNodeName

List of usage examples for org.w3c.dom Node getNodeName

Introduction

In this page you can find the example usage for org.w3c.dom Node getNodeName.

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

/**
 * Search for the first <code>Node</code> with the nodeName().equals(nodeName) in the nodeList
 * /*from   w  w w .  j a  v  a2  s  . c o m*/
 * @param nodeList
 * @param nodeName
 * @return Node or null if no Node has been found
 */
private static Node findNode(NodeList nodeList, String nodeName) {
    Node curNode;
    for (int i = 0; i < nodeList.getLength(); i++) {
        curNode = nodeList.item(i);
        if (curNode.getNodeType() == Node.ELEMENT_NODE && nodeName.equals(curNode.getNodeName())) {
            return curNode;
        }
    }
    return null;
}

From source file:Main.java

private static void serializeElement(StringBuilder sb, Element element, int tabIndex) {
    sb.append('\n');
    for (int i = 0; i < tabIndex; i++) {
        sb.append('\t');
    }//from w w  w  . j a v  a 2s. co  m
    sb.append("<");
    sb.append(element.getTagName());
    if (element.hasAttributes()) {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);

            sb.append(" ");
            sb.append(attribute.getNodeName());
            sb.append("=");
            sb.append("\"");
            String value = attribute.getNodeValue();
            sb.append(value.replace("\"", "\\\""));
            sb.append("\"");
        }
    }
    sb.append(">");
    NodeList nodeList = element.getChildNodes();
    ArrayList<Element> childElements = new ArrayList<Element>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode instanceof Element) {
            childElements.add((Element) childNode);
        }
    }
    if (childElements.size() == 0) {
        sb.append(escapeInvalidCharacters(getTextContent(element)));
    } else {
        for (Element childElement : childElements) {
            serializeElement(sb, childElement, tabIndex + 1);
        }
        sb.append('\n');
        for (int i = 0; i < tabIndex; i++) {
            sb.append('\t');
        }
    }
    sb.append("</");
    sb.append(element.getTagName());
    sb.append(">");
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }//from  ww w .j  a  v a 2 s.c o  m

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue(), node instanceof Attr));
    }

    return b.toString();
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }//from   w  ww  .ja  v  a2 s. co  m

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {

        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}

From source file:com.tascape.qa.th.android.model.UIA.java

public static UIANode parseNode(Node node) {
    if (!node.getNodeName().equals(UIANode.TAG_NAME)) {
        return null;
    }/*  ww w .  j a  v  a2s  .co m*/

    NamedNodeMap map = node.getAttributes();
    String klass = map.getNamedItem("class").getNodeValue();
    UIANode uiNode = newNode(klass);

    for (int i = 0, j = map.getLength(); i < j; i++) {
        Node attr = map.item(i);
        uiNode.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        UIANode n = parseNode(nl.item(i));
        if (n == null) {
            continue;
        }
        uiNode.addNode(n);
    }

    return uiNode;
}

From source file:Main.java

/**
 * This method will find all the parameters under this <code>paramsElement</code> and return them as
 * Map<String, String>. For example,
 * <pre>//from   www  . j a  v a 2  s  . c  o m
 *   <result ... >
 *      <param name="param1">value1</param>
 *      <param name="param2">value2</param>
 *      <param name="param3">value3</param>
 *   </result>
 * </pre>
 * will returns a Map<String, String> with the following key, value pairs :-
 * <ul>
 * <li>param1 - value1</li>
 * <li>param2 - value2</li>
 * <li>param3 - value3</li>
 * </ul>
 *
 * @param paramsElement
 * @return
 */
public static Map<String, String> getParams(Element paramsElement) {
    LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();

    if (paramsElement == null) {
        return params;
    }

    NodeList childNodes = paramsElement.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);

        if ((childNode.getNodeType() == Node.ELEMENT_NODE) && "param".equals(childNode.getNodeName())) {
            Element paramElement = (Element) childNode;
            String paramName = paramElement.getAttribute("name");

            String val = getContent(paramElement);
            if (val.length() > 0) {
                params.put(paramName, val);
            }
        }
    }

    return params;
}

From source file:Main.java

/**
 * Search for an XML element in the direct children of parent only.
 *
 * This compares localName (nodeName if localName is null) to name, and
 * checks the tags namespace with the provided namespace. A
 * <code>null</code> namespace will match any namespace.
 * <p>/*  w  w w .  jav  a 2  s  . c  o m*/
 * This is differs from the DOM version by:
 * <ul>
 * <li>not searching recursively</li>
 * <li>returns a single result</li>
 * </ul>
 *
 * @param parent    a parent element
 * @param name      the intended local name
 * @param namespace the intended namespace (or null)
 * @return the one child element with that name, or null if none
 * @throws IllegalArgumentException if there is multiple elements of the
 *                                  same name
 *
 * @since 8.4
 */
public static Element findElement(Element parent, String name, String namespace)
        throws IllegalArgumentException {
    Element result = null;
    NodeList l = parent.getChildNodes();
    int nodeCount = l.getLength();
    for (int i = 0; i < nodeCount; i++) {
        if (l.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Node node = l.item(i);
            String localName = node.getLocalName();
            localName = localName == null ? node.getNodeName() : localName;

            if (name.equals(localName) && (namespace == null || namespace.equals(node.getNamespaceURI()))) {
                if (result == null) {
                    result = (Element) node;
                } else {
                    throw new IllegalArgumentException("more than one element with same name found");
                }
            }
        }
    }
    return result;
}

From source file:Main.java

/** Finds and returns the next sibling node with the given name. */
public static Element getNextSiblingElement(Node node, String elemName) {

    if (node == null)
        return null;
    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            if (sibling.getNodeName().equals(elemName)) {
                return (Element) sibling;
            }//from  w  w w .ja  v a2s. c o  m
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static Object xmlToBean(Node beanNode) throws ClassNotFoundException, IllegalAccessException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {
    String className = beanNode.getNodeName();
    System.out.println(className);
    Class clazz = Class.forName(className);
    Object bean = clazz.newInstance();
    NodeList fieldNodeList = beanNode.getChildNodes();
    for (int i = 0; i < fieldNodeList.getLength(); i++) {
        Node fieldNode = fieldNodeList.item(i);
        if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
            String fieldName = fieldNode.getNodeName();
            if (!fieldName.contains(".")) {
                String getName = analyzeMethodName(fieldName, "get");
                String setName = analyzeMethodName(fieldName, "set");
                System.out.println(setName);
                clazz.getMethod(setName, clazz.getMethod(getName).getReturnType()).invoke(bean,
                        fieldNode.getTextContent());
            }//from   www .  j  ava 2s  .c o m
        }
    }
    System.out.println(bean);
    return bean;
}

From source file:Main.java

/**
 * Filter node list for Element nodes of specified name.
 *//*from  ww w  . j  a v  a 2 s  .  c o  m*/
public static List<Node> filterNodeListElements(NodeList nodeList, String nodeName) {
    List<Node> nodes = new ArrayList<Node>();
    for (int k = 0; k < nodeList.getLength(); k++) {
        Node node = nodeList.item(k);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (nodeName != null && (node.getNodeName().equals(nodeName) == false)) {
            continue;
        }
        nodes.add(node);
    }
    return nodes;
}