Example usage for org.w3c.dom Node getNodeType

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

Introduction

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

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static String findXslHref(Node node) {
    int type = node.getNodeType();

    if (type == Node.PROCESSING_INSTRUCTION_NODE) {
        String nodeName = node.getNodeName();
        if (nodeName.equalsIgnoreCase("xml-stylesheet")) {
            String nodeValue = node.getNodeValue();
            try {
                int i = nodeValue.indexOf("href=\"");
                int j = nodeValue.indexOf("\"", i + 6);
                String result = nodeValue.substring(i + 6, j);
                return result;
            } catch (StringIndexOutOfBoundsException e) {
                return null;
            }//w  ww.j av a 2  s .  co m
        }
        return null;
    } else {
        NodeList children = node.getChildNodes();
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            String result = findXslHref(children.item(i));
            if (result != null)
                return result;
        }
    }
    return null;
}

From source file:Main.java

public static Vector getElementsByAttribValue(org.w3c.dom.Element element, String attrib, String val) {
    NodeList desElements = element.getElementsByTagName("*");
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }//  w  w w  .j a v a  2  s .c o  m
        }
    }

    return selElements;
}

From source file:Main.java

/**
 * Gets the next sibling Element of the node, skipping any Text nodes such as whitespace.
 * /*from  w w w .  j  av a 2  s  .  co  m*/
 * @param n The sibling to start with
 * @return The next sibling Element of n, or null if none
 */
public static Element getNextSiblingElement(Node n) {
    Node sib = n.getNextSibling();
    while (sib != null && sib.getNodeType() != Node.ELEMENT_NODE) {
        sib = sib.getNextSibling();
    }

    if (sib != null) {
        return (Element) sib;
    } else {
        return null;
    }
}

From source file:Main.java

public static Map<String, Object> getPropertiesWithValuesFromXML(Node propNode) {
    Map<String, Object> propertiesWithValues = new HashMap<String, Object>();

    NodeList childList = propNode.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            String fqn = namespace + ":" + nodeName;
            propertiesWithValues.put(fqn, nodeValue(currentNode));

        }/*  ww w.ja va 2  s  .c  o m*/
    }
    return propertiesWithValues;
}

From source file:Main.java

public static String getTextChild(Element element) {
    NodeList children = element.getChildNodes();
    String value = null;/*from   w ww .  j  ava 2  s  .  c o m*/
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            if (null != value) {
                throw new IllegalStateException(
                        "Element " + elementToString(element) + " has more than one text child.");
            } else {
                value = child.getNodeValue();
            }
        }
    }
    return value;
}

From source file:Main.java

public static String getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {//  ww  w .  j  av a 2  s . c  o m

        DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = domBuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(nodeName)) {
                        returnVal = node.getAttributes().getNamedItem(attributeName).getNodeValue();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnVal;
}

From source file:Main.java

public static String getElementText(Element elem) {
    StringBuilder buf = new StringBuilder();
    NodeList nodeList = elem.getChildNodes();
    for (int i = 0, len = nodeList.getLength(); i < len; i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            buf.append(n.getNodeValue());
        } else {/*  www. ja  va 2  s  . co  m*/
            //TODO see jsf-samples
            //throw new FacesException("Unexpected node type " + n.getNodeType());
        }
    }
    return buf.toString();
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    if (null == parent)
        return null;

    NodeList nodes = parent.getChildNodes();
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            elements.add((Element) node);
    }/* ww  w. jav  a 2s .  c  o  m*/

    return elements;
}

From source file:Main.java

private static boolean checkNodeTypes(Node childNode) {
    short nodeType = childNode.getNodeType();

    if (nodeType == Node.ELEMENT_NODE) {
        cleanEmptyTextNodes(childNode); // recurse into subtree
    }/*  w ww  . jav a2s  .c  om*/

    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList children = parent.getChildNodes();
    int size = children.getLength();

    for (int i = 0; i < size; i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            if (childName.equals(element.getNodeName())) {
                return element;
            }/*  w  ww . ja  va  2s.  c  om*/
        }
    }

    return null;
}