Example usage for org.w3c.dom Node ELEMENT_NODE

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

Introduction

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

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

/**
 * Get the text content of an element.//from w  w w  .  j  a v  a2s.c  om
 *
 * @param element
 *            The element.
 * @param sbuf
 *            The buffer to append to.
 * @param decend
 *            Whether to descend into child elements.
 */
public static void getText(final Element element, final StringBuilder sbuf, final boolean decend) {
    Node node = element.getFirstChild();

    while (node != null) {
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
            sbuf.append(node.getNodeValue());
            break;

        case Node.ELEMENT_NODE:
            if (decend) {
                getText((Element) node, sbuf, decend);
            }

            break;
        }

        node = node.getNextSibling();
    }
}

From source file:Main.java

/**
 * Given a node, returns a map where, for each immediate child of this node
 * that is an element named A with a Text node with data B, there is an
 * entry in the map from A to B. If A contains no textual node, A maps to
 * <TT>null</TT>. If the element A appears more than once, the last
 * element encountered is respected.//from   w w  w. jav  a2s  . c o m
 * 
 * @param node
 *            the node to get the map for
 * @return the map from children element names to their textual contents
 */
public static Map<String, String> elementsToText(Node node) {
    NodeList children = node.getChildNodes();
    Map<String, String> e2t = new HashMap<String, String>();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.ELEMENT_NODE)
            continue;
        String elementName = ((Element) c).getTagName();
        String text = containedText(c);
        e2t.put(elementName, text);
    }
    return e2t;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    List<Element> ret = new ArrayList<Element>();
    NodeList childList = parent.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element child = (Element) childList.item(i);
        ret.add(child);/*ww  w.  ja v  a  2s  . com*/
    }
    return ret;
}

From source file:Main.java

static public void fillHashtable(NodeList list, Hashtable<String, String> fillIn) {
    for (int curElemNum = 0; curElemNum < list.getLength(); curElemNum++) {
        if (list.item(curElemNum).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element curValue = (Element) list.item(curElemNum);
        String valueName = curValue.getNodeName();
        String value = curValue.getFirstChild().getNodeValue();
        fillIn.put(valueName, value);/*w  w w  . j av a  2s  .c  om*/
    }
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//from   w  ww .j  a v  a 2s. co  m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

public static String[] getNodesValue(Element node, String nodeName) {
    List<String> ret = new ArrayList<String>();

    NodeList ndLs = node.getElementsByTagName(nodeName);
    for (int s = 0; s < ndLs.getLength(); s++) {
        Node fstNode = ndLs.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add(fstNode.getTextContent());
        }/*  w w w  .  jav  a  2 s .  co  m*/
    }

    return ret.toArray(new String[0]);
}

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCols(String Xpath, Node node, String[] attrNames,
        String sep) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    if (sep == null) {
        sep = " ";
    }/*from w w w.j  a  v  a 2  s  .c  o m*/
    int aNamesL = attrNames.length;
    if (aNamesL > 0) {

        NodeList nl = getNodesListXpathNode(Xpath, node);
        int l = nl.getLength();
        Element e = null;
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                StringBuilder sb = new StringBuilder();
                for (int y = 0; y < aNamesL; y++) {
                    sb.append(e.getAttribute(attrNames[y]));
                    if (y < aNamesL - 1) {
                        sb.append(sep);
                    }
                }
                val = sb.toString();
                if (val != null && val.length() > 0) {
                    retV.add(val);
                }
            }
        }
    }
    return retV;
}

From source file:Main.java

private static String getTextValue(String def, Element doc, String tag) {
    String value = def;//from   w  w  w . j a  va 2s  . c om
    NodeList noteList = doc.getElementsByTagName(tag);
    if (noteList.getLength() > 0) {
        for (int i = 0; i < noteList.getLength(); i++) {
            Node note = noteList.item(i);

            Element noteElement = (Element) note;
            if (noteElement.getNodeType() == Node.ELEMENT_NODE) {
                if (noteElement.getElementsByTagName("pitch").getLength() > 0) {
                    Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0);
                    System.out.println("Staff idX : "
                            + pitchElement.getElementsByTagName("step").item(0).getTextContent());
                } else {
                    System.out.println("yoktir");
                }
                //               System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                //               System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());                 
            } else {
                System.out.println("not element");
            }
        }

        //          value = nl.item(0).getFirstChild().getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Find the previous sibling element.//from  w  w w.  jav a2  s  .co m
 * 
 * @param startNode XML start node.
 * @return the previous sibling element.
 */
public static Element findPreviousSibling(Node startNode) {
    if (startNode == null)
        return null;

    while (startNode != null) {
        startNode = startNode.getPreviousSibling();
        if (startNode == null)
            return null;
        if (startNode.getNodeType() == Node.ELEMENT_NODE)
            return (Element) startNode;
    }

    return null;
}

From source file:Main.java

public static Node getPreviousSiblingElement(Node node) {
    if (node == null)
        return null;
    Node prevSibling = node.getPreviousSibling();
    while ((prevSibling != null) && (prevSibling.getNodeType() != Node.ELEMENT_NODE))
        prevSibling = prevSibling.getPreviousSibling();
    if ((prevSibling != null) && (prevSibling.getNodeType() == Node.ELEMENT_NODE))
        return prevSibling;
    return null;/*from ww  w .j a  va 2  s .  c om*/
}