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:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that correctly handles setting the text content
 * for a given tag in a DOM <code>Document</code>
 * //from  w  ww.  jav a  2s .  c  om
 * @param tagName   DOM tag to create and add to the parent node.
 * @param value      The text value to be assigned to the tag
 * @param parent   The parent Node that the tag/text will be added to
 */
final static void documentSetTextContentOfNode(final String tagName, final String value, final Node parent) {
    final Document dom = parent.getOwnerDocument();
    final Node node = dom.createElement(tagName);
    final Text nodeVal = dom.createTextNode(value);
    node.appendChild(nodeVal);
    parent.appendChild(node);
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * This is a workaround for very strange behaviour of xerces-1.4.2 DOM importNode.
 *//*from  w  w w  .j av a2s  . c  o  m*/
public static Node importNode(Document document, Node toImport) {
    if (toImport != null) {
        Node root = toImport.cloneNode(false); // no deep cloning!

        root = document.importNode(root, false);

        for (Node n = toImport.getFirstChild(); n != null; n = n.getNextSibling()) {
            root.appendChild(document.importNode(n, true));
        }

        return root;
    }

    return null;
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Receive scoEntrynode to modify it/*from ww  w .  j a  va2 s.  co  m*/
 * 
 * @param node
 * @param document
 */
public static void changeScoEntry(Node node, org.w3c.dom.Document document) {
    if (node.getNodeName().equals("scoEntry")) {
        /**
         * for each scoEntry must create scoEntryAddress node
         * 
         */
        // adding the title...scoEntry/scoEntryTitle
        if (node.getChildNodes().item(1) != null
                && node.getChildNodes().item(1).getNodeName().equals("scoEntryTitle")) {
            Node scoEntryAddress = document.createElement("scoEntryAddress");

            // create the scoEntryAddress node
            Node scoEntryCode = document.createElement("scoEntryCode");

            ((Element) scoEntryCode).setAttribute("modelIdentCode", modelic);
            ((Element) scoEntryCode).setAttribute("scormContentPackageNumber", PackageNumber);
            ((Element) scoEntryCode).setAttribute("scormContentPackageIssuer", PackageIssuer);
            ((Element) scoEntryCode).setAttribute("scormContentPackageVolume", PackageVolume);
            scoEntryAddress.appendChild(scoEntryCode);

            // create the scoEntryStatus node
            Node scoEntryStatus = document.createElement("scoEntryStatus");
            // status
            Node status = document.createElement("security");
            ((Element) status).setAttribute("securityClassification", securityClassification);
            // qualityAssurance

            Node qualityAssuranceNode = qualityAssurance.cloneNode(true);
            scoEntryStatus.appendChild(qualityAssuranceNode);

            // adding address and status
            node.insertBefore(scoEntryStatus, node.getChildNodes().item(1));
            node.insertBefore(scoEntryAddress, node.getChildNodes().item(1));

            // move the title under the scoEntryAddress section
            scoEntryAddress.appendChild(node.getChildNodes().item(3));
        }
    }
}

From source file:cn.itcast.fredck.FCKeditor.connector.ConnectorServlet.java

private void getFiles(File dir, Node root, Document doc) {
    Element files = doc.createElement("Files");
    root.appendChild(files);
    File[] fileList = dir.listFiles();
    for (int i = 0; i < fileList.length; ++i) {
        if (fileList[i].isFile()) {
            Element myEl = doc.createElement("File");
            myEl.setAttribute("name", fileList[i].getName());
            myEl.setAttribute("size", "" + fileList[i].length() / 1024);
            files.appendChild(myEl);//w  w  w  .j a v a  2  s. co m
        }
    }
}

From source file:cn.itcast.fredck.FCKeditor.connector.ConnectorServlet.java

private void getFolders(File dir, Node root, Document doc) {
    Element folders = doc.createElement("Folders");
    root.appendChild(folders);
    File[] fileList = dir.listFiles();
    for (int i = 0; i < fileList.length; ++i) {
        if (fileList[i].isDirectory()) {
            Element myEl = doc.createElement("Folder");
            myEl.setAttribute("name", fileList[i].getName());
            folders.appendChild(myEl);/*from   w ww. j a v  a  2 s  .c  om*/
        }
    }
}

From source file:Sax2Dom.java

public void characters(char[] ch, int start, int length) {
    final Node last = (Node) _nodeStk.peek();

    // No text nodes can be children of root (DOM006 exception)
    if (last != _document) {
        final String text = new String(ch, start, length);
        last.appendChild(_document.createTextNode(text));
    }/* www. j a va2s  .c o  m*/
}

From source file:it.unibas.spicy.persistence.xml.operators.ExportXSD.java

public void visitMetadataNode(MetadataNode node) {
    if (logger.isDebugEnabled())
        logger.debug(" metadata: " + node.getLabel());
    Element attributeTag = document.createElement(ExportXSD.PREFIX + "attribute");
    checkLeafNode(node, attributeTag);//from  ww w.j  av  a 2s.c o m
    Element peekElement = stackOfElements.peek();
    Node parentPeekElement = peekElement.getParentNode();
    parentPeekElement.appendChild(attributeTag);
    if (logger.isDebugEnabled())
        logger.debug(" ElementTag Father of the Metadata Node: \n" + parentPeekElement.getNodeName());
}

From source file:cn.itcast.fredck.FCKeditor.connector.ConnectorServlet.java

private void setCreateFolderResponse(String retValue, Node root, Document doc) {
    Element myEl = doc.createElement("Error");
    myEl.setAttribute("number", retValue);
    root.appendChild(myEl);
}

From source file:Main.java

/**
 * Convenience method to copy the contents of the old {@link Node} into the
 * new one./*from w  w  w .j  av a 2  s.co  m*/
 * 
 * @param newDoc
 * @param newNode
 * @param oldParent
 */
public static void copyContents(Document newDoc, Node newNode, Node oldNode) {
    // FIXME we should be able to achieve this with much less code (e.g.
    // the code commented out) but for some reason there are
    // incompatibility issues being spat out by the tests.
    //      final NodeList childNodes = oldNode.getChildNodes();
    //        for (int i = 0; i < childNodes.getLength(); i++) {
    //           final Node child = newDoc.importNode(childNodes.item(i), true);
    //           newNode.appendChild(child);
    //        }

    final NodeList childs = oldNode.getChildNodes();

    for (int i = 0; i < childs.getLength(); i++) {
        final Node child = childs.item(i);
        Element newElem = null;
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            //Get all the attributes of an element in a map
            final NamedNodeMap attrs = child.getAttributes();

            // Process each attribute
            newElem = newDoc.createElement(child.getNodeName());
            for (int j = 0; j < attrs.getLength(); j++) {
                final Attr attr = (Attr) attrs.item(j);
                // add attribute name and value to the new element
                newElem.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            newNode.appendChild(newElem);
            break;

        case Node.TEXT_NODE:
            newNode.appendChild(newDoc.createTextNode(getString(child)));
        }
        copyContents(newDoc, newElem, child);
    }
}

From source file:net.sf.joost.emitter.DOMEmitter.java

private void insertNode(Node newNode) {
    Node lastNode = (Node) stack.peek();
    if (stack.size() == 1 && nextSiblingOfRootNodes != null) {
        lastNode.insertBefore(newNode, nextSiblingOfRootNodes);
    } else {//from  ww w . j a  v a  2 s.c  om
        lastNode.appendChild(newNode);
    }
}