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

/**
 * Add a RSS 2.0 Channel Information to a given node - which is your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument current XML Document
 * @param channelRoot the channel node to which you want the information to be attached to
 * @param title title of your channel//from   w ww  . jav  a  2 s.  co  m
 * @param link  link to your channel home
 * @param description description of your channel
 */
public static void addRSSChannelInformation(Document XMLDocument, Node channelRoot, String title, String link,
        String description) {
    // Title node
    Node entry = XMLDocument.createElement("title");
    entry.appendChild(XMLDocument.createTextNode(title));
    channelRoot.appendChild(entry);

    // Link node
    entry = XMLDocument.createElement("link");
    entry.appendChild(XMLDocument.createTextNode(link));
    channelRoot.appendChild(entry);

    // Description node
    entry = XMLDocument.createElement("description");
    entry.appendChild(XMLDocument.createTextNode(description));
    channelRoot.appendChild(entry);

    // lastBuildDate node
    entry = XMLDocument.createElement("lastBuildDate");
    entry.appendChild(XMLDocument.createTextNode(
            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date())));
    channelRoot.appendChild(entry);

    // language node
    entry = XMLDocument.createElement("language");
    entry.appendChild(XMLDocument.createTextNode("en-gb"));
    channelRoot.appendChild(entry);
}

From source file:Main.java

/**
 * Append the nodes from the supplied list to the supplied node.
 * @param node Node to be appended to./*from  w ww . ja va2  s .  c o  m*/
 * @param nodes List of nodes to append.
 * @return the Node with the appended list.
 */
public static Node appendList(Node node, List<Node> nodes) {
    //int nodeCount = nodes.size();
    for (Object node1 : nodes) {
        node.appendChild((Node) node1);
    }
    return node;
}

From source file:Main.java

/**
 * Add a RSS 2.0 Item to your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument the current XML Document
 * @param channelRoot your channel node/*from  w w  w .j a v a  2  s.  co  m*/
 * @param title title of your entry
 * @param link link to further information
 * @param guid globally unique identifier - same as link
 * @param pubDate date of the new entry
 * @param text text of the item
 */
public static void addRSSItem(Document XMLDocument, Node channelRoot, String title, String link, String guid,
        String pubDate, String text) {
    // Create <item> node
    Node entry = XMLDocument.createElement("item");

    // Set title node beneath <item>
    Node subentry = XMLDocument.createElement("title");
    subentry.appendChild(XMLDocument.createTextNode(title));
    entry.appendChild(subentry);

    // Set link node beneath <item>
    subentry = XMLDocument.createElement("link");
    subentry.appendChild(XMLDocument.createTextNode(link));
    entry.appendChild(subentry);

    // Set guid node beneath <item>
    subentry = XMLDocument.createElement("guid");
    subentry.appendChild(XMLDocument.createTextNode(guid));
    entry.appendChild(subentry);

    // Set pubDate beneath <item>
    subentry = XMLDocument.createElement("pubDate");
    subentry.appendChild(XMLDocument.createTextNode(pubDate));
    entry.appendChild(subentry);

    // Set last message node beneath <host>
    subentry = XMLDocument.createElement("description");
    subentry.appendChild(XMLDocument.createTextNode(text));
    entry.appendChild(subentry);

    channelRoot.appendChild(entry);
}

From source file:Main.java

public static Node addTextTag(Node parent, String name, String value) {
    Node node = parent.getOwnerDocument().createElement(name);
    parent.appendChild(node);
    Text text = parent.getOwnerDocument().createTextNode(value);
    node.appendChild(text);//from w  ww. java  2 s  .c o m
    return node;
}

From source file:Main.java

/**
 * Create a new element in the parent node with the given local name.
 * @param parent The node to which the new element will be appended.
 * @param name The local name of the new element to be created
 * @return The newly created element.//from   w w w .j av a  2  s.  com
 */
public static Element makeElement(Node parent, String name) {
    Document d = getDoc(parent);
    Element result = d.createElement(name);
    parent.appendChild(result);
    return result;
}

From source file:Main.java

/**
 * Creates the text./*from w  w  w.j av  a2s.  com*/
 *
 * @param parent
 *          the parent
 * @param text
 *          the text
 *
 * @return the text
 */
public static Text createText(final Node parent, final String text) {
    final Text child = parent.getOwnerDocument().createTextNode(text);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static void sendXml(String result, int operationN) throws Exception {
    String filepath = "file.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(filepath);

    Node tonode = doc.getElementsByTagName("command").item(0);
    Element res = doc.createElement("result");
    res.setTextContent(result);/*from w  w  w. ja  v a 2s  . c  o m*/
    tonode.appendChild(res);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result1 = new StreamResult(new File("file2.xml"));

    StreamResult result3 = new StreamResult(System.out);
    transformer.transform(source, result1);
}

From source file:Main.java

/**
 * Create a child of the given node.//from   w w w . j  av a 2  s.co  m
 * 
 * @param doc
 *            a document
 * @param node
 *            a node
 * @param nodeName
 *            a node name
 * @return org.w3c.dom.Element
 */
public static Element createChildElement(Document doc, Node node, String nodeName) {
    Element element = doc.createElement(nodeName);
    node.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * Adds an element to the specified document.
 * @param doc the document.//from  www  .j a  v  a 2  s  . c o  m
 * @param parent the parent node.
 * @param name the element name.
 * @return the element.
 */
public static Element addElement(final Document doc, final Node parent, final String name) {
    Element element = doc.createElement(name);
    if (parent != null) {
        parent.appendChild(element);
    } else {
        doc.appendChild(element);
    }
    return element;
}

From source file:Main.java

/**
 * // w w  w .  j  av a  2s  .  co  m
 * <i>Description:</i> append a node into specified <code>parent</code> node
 * 
 * @param doc
 * @param parent
 * @param node
 */
public static void appendNode(Document doc, Node parent, Node node) {
    Node txt = doc.createTextNode("\n    ");
    parent.appendChild(txt);
    Node comment = doc.createComment(" Created by FM GUI @ " + (new Date()) + " ");
    parent.appendChild(comment);
    doc.adoptNode(node);
    parent.appendChild(node);
    formatNode(doc, parent, node);
    txt = doc.createTextNode("\n\n  ");
    parent.appendChild(txt);
}