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 Element getAddressTypeWithText(Document doc, String street, String city, String state,
        String zip) {/* w  w w . jav a  2 s.co  m*/

    Element elem_address = doc.createElement("Address");

    Element elem_street = getElementWithText(doc, "Street", street);
    Element elem_city = getElementWithText(doc, "City", city);
    Element elem_state = getElementWithText(doc, "State", state);
    Element elem_zip = getElementWithText(doc, "Zip", zip);

    elem_address.appendChild(elem_street);
    elem_address.appendChild(elem_city);
    elem_address.appendChild(elem_state);
    elem_address.appendChild(elem_zip);

    return elem_address;
}

From source file:Main.java

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);
    }/*  www  .j  a v a2s  .c  o m*/

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Serialize a schema into an Output stream
 * //from w  ww  .ja v  a 2 s  .co  m
 * @param schema
 *            Schema concerned by the serialization processing
 * @param os
 *            Stream to save serialized schema
 * @throws TransformException
 *             When couldn't parse data to XML/RDF
 */
public static void serialize(XMPSchema schema, OutputStream os) throws TransformException {
    try {
        Document doc = XMLUtil.newDocument();
        Element rdf = doc.createElementNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:RDF");
        Node schemContent = schema.getElement().cloneNode(true);
        doc.adoptNode(schemContent);
        rdf.appendChild(schemContent);
        XMLUtil.save(rdf, os, "UTF-8");
    } catch (TransformerException e) {
        throw new TransformException("Failed to parse defined XMP");
    } catch (IOException e) {
        throw new TransformException("Failed to create Document to contain Schema representation ",
                e.getCause());
    }

}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemIntValue(Document aDocument, Element anElement, String aName, int aValue) {
    Integer intObject;/*from  ww  w .  j av a  2  s . com*/
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        intObject = aValue;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(intObject.toString()));
        anElement.appendChild(subElement);
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemLongValue(Document aDocument, Element anElement, String aName, long aValue) {
    Long longObject;//from   w  ww . j a v a 2s .  c o m
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        longObject = aValue;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(longObject.toString()));
        anElement.appendChild(subElement);
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemStrValue(Document aDocument, Element anElement, String aName, String aValue) {
    Element subElement;

    if ((StringUtils.isNotEmpty(aName)) && (StringUtils.isNotEmpty(aValue))) {
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }/*from w  w  w .  jav  a 2 s  . c  om*/
}

From source file:Main.java

/**
 * adds an Element with two attributs to a DOM-tree.
 * /*www.  j  av a  2 s .c  o m*/
 * @param document
 *            Document: the DOM-tree to add to
 * @param father
 *            Element: the new element will be inserted directly under this
 *            Element in the tree
 * @param name
 *            String: the name of the new element
 * @param attOneName
 *            String: the name of the 1st attribut
 * @param attOneValue
 *            String: the value of the 1st attribut
 * @param attTwoName
 *            String: the name of the 2nd attribut
 * @param attTwoValue
 *            String: the value of the 2nd attribut
 */
public static void addElement(Document document, Element father, String name, String attOneName,
        String attOneValue, String attTwoName, String attTwoValue) {
    Element element = document.createElement(name);
    element.setAttribute(attOneName, attOneValue);
    element.setAttribute(attTwoName, attTwoValue);
    father.appendChild(element);
}

From source file:com.viettel.ws.client.JDBCUtil.java

public static Element createRowElement(Object obj, Document doc) {
    Element row = doc.createElement("Row");
    Class cls = obj.getClass();/*ww  w .j  a v a2  s .  c o m*/
    Field[] fieldArr = cls.getDeclaredFields();
    SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy");

    for (int i = 0; i < fieldArr.length; i++) {
        try {
            String fieldName = fieldArr[i].getName();
            String getMethodName = "get" + UpcaseFirst(fieldName);
            Method getMethod = cls.getMethod(getMethodName);

            Object value = getMethod.invoke(obj);
            if (fieldArr[i].getType().equals(Date.class)) {
                value = fm.format(value);
            }

            Element node = doc.createElement(convertToOrigin(fieldName));
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);

        } catch (Exception ex) {
            LogUtil.addLog(ex);//binhnt sonar a160901
            continue;
        }
    }
    return row;
}

From source file:com.twentyn.patentExtractor.PatentDocumentFeatures.java

/**
 * Extracts sentence nodes from a POS-tagger XML document.  These sentences are intended to provide some notion of
 * locality for identified chemical entities.
 *
 * @param docBuilder A document builder to use when producing single-sentence XML documents.
 * @param doc        The POS-tagger XML document from which to extract sentences.
 * @return A list of single-sentence documents.
 * @throws ParserConfigurationException//from  w  w w. ja v a 2 s .c o m
 * @throws XPathExpressionException
 */
private static List<Document> findSentences(DocumentBuilder docBuilder, Document doc)
        throws ParserConfigurationException, XPathExpressionException {
    if (doc != null) {
        // TODO: is there a more efficient yet still safe way to do this?
        XPath xpath = Util.getXPathFactory().newXPath();
        // TODO: get rid of this inline xpath compilation, run during setup.
        NodeList nodes = (NodeList) xpath.evaluate(SENTENCE_PATH, doc, XPathConstants.NODESET);

        List<Document> docList = new ArrayList<>(nodes.getLength());
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);

            /* With help from:
             * http://examples.javacodegeeks.com/core-java/xml/dom/copy-nodes-subtree-from-one-dom-document-to-another/ */
            org.w3c.dom.Document newDoc = docBuilder.newDocument();
            Element rootElement = newDoc.createElement(SENTENCE_DOC_HEADER);
            Node newNode = newDoc.importNode(n, true);
            rootElement.appendChild(newNode);
            newDoc.appendChild(rootElement);
            docList.add(newDoc);
        }
        return docList;
    } else {
        // TODO: log here.
        return new ArrayList<>(0);
    }
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static Element createElementAndText(Element parent, String elementName, String text) {
    if (text == null) {
        throw new NullPointerException("null text");
    }//from www.  j a v  a 2  s.  co  m
    Element el = parent.getOwnerDocument().createElement(elementName);
    parent.appendChild(el);
    Text txt = el.getOwnerDocument().createTextNode(text);
    el.appendChild(txt);
    return el;
}