Example usage for org.w3c.dom Element appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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

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);
    find.appendChild(type);/*from w  ww  .ja  va 2  s. co  m*/

    print(doc);

}

From source file:Main.java

public static void duplicatePerson(Document doc) {
    Element root = doc.getDocumentElement();
    Element origPerson = (Element) root.getFirstChild();
    Element newPerson = (Element) origPerson.cloneNode(true);
    root.appendChild(newPerson);
}

From source file:Main.java

public static void writeStringElement(Document document, Element parentElement, String elementTagName,
        String elementValue) {/*from   w  w  w.ja v  a2s.co m*/
    Element stringElement = createChildElement(document, parentElement, elementTagName);
    Text textNode = document.createTextNode(elementValue);
    stringElement.appendChild(textNode);
}

From source file:Main.java

/**
 * /* ww w . j a v  a  2s  .c  o  m*/
 */
public static Document createVmlDocument() {
    Document document = createDocument();

    Element root = document.createElement("html");
    root.setAttribute("xmlns:v", "urn:schemas-microsoft-com:vml");
    root.setAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");

    document.appendChild(root);

    Element head = document.createElement("head");

    Element style = document.createElement("style");
    style.setAttribute("type", "text/css");
    style.appendChild(document.createTextNode("<!-- v\\:* {behavior: url(#default#VML);} -->"));

    head.appendChild(style);
    root.appendChild(head);

    Element body = document.createElement("body");
    root.appendChild(body);

    return document;
}

From source file:Main.java

public static Element addTextNode(Document xmlDoc, Element ndParent, String nodeName, String nodeValue) {

    Element ndNode = xmlDoc.createElement(nodeName);
    Text ndTextNode = xmlDoc.createTextNode(nodeName);
    ndTextNode.setData(nodeValue);//from w  w w  .j av a  2 s . co  m
    ndNode.appendChild(ndTextNode);
    ndParent.appendChild(ndNode);
    return ndNode;

}

From source file:Main.java

/** @param text <code>null</code> removes 'subElement' completely */
public static Text setTextField(Element node, String subElement, String text) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl == null || nl.getLength() == 0) {
        if (text == null)
            return null;
        Document d = node.getOwnerDocument();
        Element e = d.createElement(subElement);
        Text ans = d.createTextNode(text);
        e.appendChild(ans);
        node.appendChild(e);//from  w  w w  .j  av  a 2  s. c o  m
        return ans;
    } else if (text != null) {
        return setText(nl.item(0), text);
    } else {
        node.removeChild(nl.item(0));
        return null;
    }
}

From source file:Main.java

public static boolean creteEntity(Object entity, String fileName) {
    boolean flag = false;
    try {//from  www .  ja  v  a 2  s.co 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:Utils.java

public static Element findContainerElseCreate(Document document, Element parent, String child) {
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }//ww  w.  j av a2 s  . c om
    return (Element) parent.getElementsByTagName(child).item(0);
}

From source file:Main.java

public static void appendNewElement(Document doc, Element targetElement, String elementName, String value) {

    if (value != null) {
        targetElement.appendChild(createElement(elementName, value, doc));
    }/*from   w ww  .j  av  a  2  s  .  com*/
}

From source file:Main.java

public static void insertBeforeFirstChild(Element object, Element attrId) {
    if (object.hasChildNodes()) {
        object.insertBefore(attrId, object.getFirstChild());
    } else {/*from w w  w  .  jav a 2 s . c om*/
        object.appendChild(attrId);
    }
}