Example usage for org.w3c.dom Node hasChildNodes

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

Introduction

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

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("foo.xml");

    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    NodeList list = doc.getFirstChild().getChildNodes();

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

        if (element.hasChildNodes()) {
            Source src = new DOMSource(element);
            FileOutputStream fs = new FileOutputStream("k" + i + ".xml");
            Result dest = new StreamResult(fs);
            aTransformer.transform(src, dest);
            fs.close();/*from  w w  w  .j  a v a2s. c  o m*/
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  w w  w.  ja  v a2  s.com

    Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));

    String fragment = "<fragment>aaa</fragment>";

    factory = DocumentBuilderFactory.newInstance();
    Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment)));

    Node node = doc.importNode(d.getDocumentElement(), true);

    DocumentFragment docfrag = doc.createDocumentFragment();

    while (node.hasChildNodes()) {
        docfrag.appendChild(node.removeChild(node.getFirstChild()));
    }

    Element element = doc.getDocumentElement();
    element.appendChild(docfrag);
}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }//from w  ww  .j a  va 2s .  c  o  m
        }
    }
    return null;
}

From source file:Main.java

public static void removeChildNodes(Node parent) {
    while (parent.hasChildNodes()) {
        parent.removeChild(parent.getFirstChild());
    }/*from w w w .  j a va  2s . c o m*/
}

From source file:Main.java

public static void removeChildren(Node node) {
    while (node.hasChildNodes()) {
        node.removeChild(node.getFirstChild());
    }//from ww w  .ja  v a 2 s  .  com
}

From source file:Main.java

/**
 * Remove all children from the specified node
 *///from   w ww. j a v  a2s.co  m
public static void removeAllChildren(Node node) {
    while (node.hasChildNodes())
        node.removeChild(node.getLastChild());
}

From source file:Main.java

/**
 * Removes the Leaf Node//  w w  w.  j  av a 2s  .  c  o  m
 */

public static void removeLeafNode(Node node)

{

    if (!node.hasChildNodes())

        node.getParentNode().removeChild(node);

}

From source file:Main.java

public static String getTextData(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }//from  w  ww  . j  a  va 2 s .c o m
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.TEXT_NODE
            && child.getNodeType() != Node.CDATA_SECTION_NODE) {
        child = child.getNextSibling();
    }
    if (child == null) {
        return null;
    }

    if (child.getNodeType() == Node.TEXT_NODE) {
        return ((Text) child).getData();
    } else {
        return ((CDATASection) child).getData();
    }

}

From source file:Main.java

public static String getElementValue(Node elem) {
    if ((elem != null) && (elem.hasChildNodes())) {
        for (Node kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
            if (kid.getNodeType() == Node.TEXT_NODE) {
                return kid.getNodeValue();
            }/*  w w w . j  a  va2 s  . c o  m*/
        }
    }
    return "";
}

From source file:Main.java

public static void processNode(Node node) throws Exception {
    if (node.hasChildNodes()) {
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            processNode(child);// w ww. j a  v  a 2 s . co  m
        }
    } else {
        node.setTextContent(" ");
    }
}