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

/**
 * Set the text of the specified element to the given string.
 * /*from www .j  a  v  a2 s.  c om*/
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

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 ww .  j  a v a 2 s  .  c om*/
 * 
 * @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 String getPrefixNS(String uri, Node e) {
    while (e != null && e.getNodeType() == Node.ELEMENT_NODE) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name = a.getName();
            if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring("xmlns:".length());
            }/*  www . j a  v a2 s  .  co  m*/
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

/**
 * Prints a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 *///www . j  a v  a 2 s. c om
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}

From source file:Main.java

static public String getFragmentText(DocumentFragment elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;/* w  ww.  j a v a 2 s  . c  o  m*/
}

From source file:Main.java

static public String getElementText(Element elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;/*from www  .j a v a 2  s  . com*/
}

From source file:Main.java

/**
 * Retrieves the child element with the specified tag name for the given element.
 *
 * @param   elem     The element whose child element is to be retrieved.
 * @param   tagName  Name of the child element.
 * @pre     elem != null && tagName != null && !tagName.equals("")
 * @return  The text contents of the specified sub element, or null if the subelement
 *          does not exist./*  w ww.j av  a  2s  .  co m*/
 */
public static Element getChildElement(Element elem, String tagName) {
    NodeList children = elem.getChildNodes();
    Element childElement = null;
    int index = 0;
    while (childElement == null && index < children.getLength()) {
        Node child = children.item(index);
        if (child.getNodeType() == Node.ELEMENT_NODE && ((Element) child).getTagName().equals(tagName)) {
            childElement = (Element) child;
        } else {
            index++;
        }
    }
    return childElement;
}

From source file:Main.java

public static Node getNextElementSibling(Node node) {
    node = node.getNextSibling();//from w  w  w .j a v a 2s  .co m
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();
    }

    return node;
}

From source file:Main.java

public static String getText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);

        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            sb.append(child.getNodeValue());
        }/*from   w  ww  .  j a  v  a 2  s.c  o m*/
    }

    return sb.toString();
}

From source file:Main.java

/***************************************************************************
 * Returns the value of the first child under the give element with the
 * given name./*from w w w. j a  va 2  s . c o  m*/
 * 
 * @param e
 * @param name
 * @return
 * @throws Exception
 **************************************************************************/
public static String getChildValueByName(Element e, String name) throws Exception {
    String s = "Not found";

    /*
     * The getElementsByTagName() function returns ANY children under the
     * given element with the given tag name. This function is intended to
     * return the value of only an immediate child with a given name.
     */
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            if (node.getFirstChild() != null)
                s = node.getFirstChild().getNodeValue();
            else
                s = "";
            break;
        }
    }

    return s;
}