Example usage for org.w3c.dom Node appendChild

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

Introduction

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

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:Main.java

/**
 * Updates the specified the child node/*from  w w w .ja  v  a  2 s  .  c o  m*/
 *  
 * @param parent   the parent node
 * @param nodeName   the name of child node to update
 * @param nodeValue   the new value
 * @return boolean  true 
 * @throws Exception
 */
public static boolean updateChildNodeValueByName(Document doc, Node parent, String nodeName, String nodeValue)
        throws Exception {
    Node node = findChildNodeByName(parent, nodeName);
    if (node == null)
        return false;

    Node firstNode = node.getFirstChild();
    if (firstNode == null) {
        node.appendChild(doc.createTextNode(nodeValue));
    } else {
        firstNode.setNodeValue(nodeValue);
    }
    return true;

}

From source file:Main.java

public static Element createNewCDATAElement(Node elem, String tag, String value) {
    Document doc = elem.getOwnerDocument();
    Element res = doc.createElement(tag);
    CDATASection cdata = doc.createCDATASection(value);
    res.appendChild(cdata);/*from   www .j av a 2  s. co  m*/
    elem.appendChild(res);
    return res;
}

From source file:Main.java

public static void moveDown(final Node currentN) {
    Node nextSibling = findNextElement(currentN, false);
    Node nextNextSibling = findNextElement(nextSibling, false);
    Node parent = currentN.getParentNode();
    parent.removeChild(currentN);/*  w w w . j a v a 2  s  . c o  m*/
    if (nextNextSibling != null) {
        parent.insertBefore(currentN, nextNextSibling);
    } else {
        parent.appendChild(currentN);
    }

}

From source file:Main.java

private static Element addNewProfileSection(Document document, Element profiles, String profileIdVal) {
    //start with profile node
    Element profile = document.createElement(PROFILE_NODE_NAME);
    profiles.appendChild(profile);//from  w  ww  .  ja  v a2s.  co  m

    //create the id tag
    Node id = document.createElement(PROFILE_ID_NAME);
    //id.setNodeValue(profileIdVal);
    id.setTextContent(profileIdVal);
    //add it to the profile
    profile.appendChild(id);

    //create activation tag
    Node activeByDefault = document.createElement(ACTIVEBYDEFAULT_NAME);
    //activeByDefault.setNodeValue(ACTIVEBYDEFAULT_VAL);
    activeByDefault.setTextContent(ACTIVEBYDEFAULT_VAL);
    Node activation = document.createElement(ACTIVATION_NAME);
    activation.appendChild(activeByDefault);
    //add it to profile
    profile.appendChild(activation);

    //create build tag
    Node build = document.createElement(BUILD_NAME);
    //add it to profile
    profile.appendChild(build);

    //create plugins tag, add to build
    Node plugins = document.createElement(PLUGINS_NAME);
    build.appendChild(plugins);

    return (Element) plugins;
}

From source file:Main.java

public static Element createNewTextElement(Node elem, String tag, String value) {// throws Exception {
    Document doc = elem.getOwnerDocument();
    Element res = doc.createElement(tag);
    Text content = doc.createTextNode(value);
    res.appendChild(content);/*from   w w  w . j a  v  a 2 s. c o m*/
    elem.appendChild(res);
    return res;
}

From source file:Main.java

/**
 *
 * @param doc Document/*from   w ww .j a  v a 2s  .  c om*/
 * @param namespace String
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, String namespace, org.w3c.dom.Node parent, String name,
        String value) {
    Element child = doc.createElementNS(namespace, name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {//w  w w .  j a va2s .c o  m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fileName);

        Class clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0);

        Element newEntity = document.createElement(clazz.getSimpleName());
        EntityElement.appendChild(newEntity);

        for (Field field : fields) {
            field.setAccessible(true);
            Element element = document.createElement(field.getName());
            element.appendChild(document.createTextNode(field.get(entity).toString()));
            newEntity.appendChild(element);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File(fileName));
        transformer.transform(domSource, streamResult);
        flag = true;
    } catch (ParserConfigurationException pce) {
        System.out.println(pce.getLocalizedMessage());
        pce.printStackTrace();
    } catch (TransformerException te) {
        System.out.println(te.getLocalizedMessage());
        te.printStackTrace();
    } catch (IOException ioe) {
        System.out.println(ioe.getLocalizedMessage());
        ioe.printStackTrace();
    } catch (SAXException sae) {
        System.out.println(sae.getLocalizedMessage());
        sae.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return flag;
}

From source file:Main.java

/**
 * Insert a node into the document.//from   w w  w  . j  ava  2s.com
 *
 * @param poDocument        Source Document XML
 * @param poNode            Node where the new node is going to be inserted into
 * @param poNodeToInsert    Node to be inserted into
 * @param pbFlag            Flag to indicate if poNodeToInsert is created by document poDocument
 *
 * @return                  Node
 */
public static void insertNode(Document poDocument, Node poNode, Node poNodeToInsert, boolean pbFlag) {
    Node elementNode = null;
    try {
        if (!pbFlag) {
            elementNode = poDocument.importNode(poNodeToInsert, true);
            poNode.appendChild(elementNode);
        } else {
            poNode.appendChild(poNodeToInsert);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static Element addChildElement(Node parent, String elementName) {
    Document ownerDocument = parent.getOwnerDocument();
    Objects.requireNonNull(ownerDocument, "nodes ownerDocument " + parent);
    Element element = ownerDocument.createElement(elementName);
    parent.appendChild(element);
    return element;
}

From source file:Main.java

public static void nodeSubInsert(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    Element type = doc.createElement(queryType);

    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);/*from  ww  w.  ja  v a  2s  .  co  m*/
    find.appendChild(type);

    print(doc);

}