Example usage for org.w3c.dom Document createElement

List of usage examples for org.w3c.dom Document createElement

Introduction

In this page you can find the example usage for org.w3c.dom Document createElement.

Prototype

public Element createElement(String tagName) throws DOMException;

Source Link

Document

Creates an element of the type specified.

Usage

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

public static Document buildEntry(QName type, String objectId, Map<QName, Serializable> properties,
        List<AssociationBean> list) {
    // Document entry = new Document();
    DocumentBuilder documentBuilder = null;
    try {/*from w w w  .  ja v  a  2  s.  c  o  m*/
        documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException("Couldn't create a document builder.");
    }
    Document doc = documentBuilder.newDocument();

    Element root = doc.createElement(ENTRY_ROOTNODE);
    root.setAttribute(ENTRY_QUALIFIEDNAME, type.getLocalName());
    root.setAttribute(ENTRY_ID, objectId);
    Element attributesE = buildAttributes(doc, properties);
    Element associationsE = buildAssociations(doc, list);
    root.appendChild(attributesE);
    root.appendChild(associationsE);

    doc.appendChild(root);
    return doc;
}

From source file:Main.java

/**
 * Creates a new DOM Tree with a root element containing the schema attributes.
 *
 * @param rootName        The name of the root element.
 * @param namespaceURI    The uri of the namespace of the document.
 * @param namespacePrefix The prefix to use for the namespace (ie. the part
 *                        before the ':' in the root element.
 * @param namespaceXSD    The name of the xsd file used to validate the file.
 *                        Should be given relative to xsdBase.
 *//*from  w  w  w. jav a 2s .  c o m*/
public static Document newXMLTree(String rootName, String namespaceURI, String namespacePrefix, String xsdBase,
        String namespaceXSD) {
    try {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();

        DocumentBuilder b = f.newDocumentBuilder();
        Document doc = b.newDocument();
        Element rootNode;
        if (namespaceURI == null) {
            rootNode = doc.createElement(rootName);
        } else {
            rootNode = doc.createElementNS(namespaceURI, rootName);

            if (namespacePrefix != null) {
                rootNode.setPrefix(namespacePrefix);
            }

            rootNode.setAttributeNS(xmlnsURI, "xmlns:xsi", schemaInstanceNS);
            if (namespacePrefix == null || namespacePrefix.isEmpty()) {
                rootNode.setAttributeNS(xmlnsURI, "xmlns", namespaceURI);
            } else {
                rootNode.setAttributeNS(xmlnsURI, "xmlns:" + namespacePrefix, namespaceURI);
            }

            if (xsdBase != null && namespaceXSD != null) {
                rootNode.setAttributeNS(schemaInstanceNS, "xsi:schemaLocation",
                        namespaceURI + " " + xsdBase + namespaceXSD);
            }
        }
        doc.appendChild(rootNode);
        return doc;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAttributeCollection(Document doc, String localName, Collection<?> values) {
    Element attribute = doc.createElement(ENTRY_ATTRIBUTE_NODE);
    attribute.setAttribute(ENTRY_QUALIFIEDNAME, localName);
    for (Object value : values) {
        Element valueElement = doc.createElement(ENTRY_ATTRIBUTE_VALUE);
        valueElement.setTextContent(value.toString());
        attribute.appendChild(valueElement);
    }//w  w w. j av  a  2s.c o m
    return attribute;
}

From source file:Main.java

public static void addParameter(Document outDoc, Element succFail, String name, String value) {
    //System.out.println("value:" + value);
    Element parameters = (Element) succFail.getElementsByTagName("parameters").item(0);
    Element parameter = outDoc.createElement(name);
    parameter.setAttribute("value", value);
    parameters.appendChild(parameter);//  ww  w  .ja v a2  s  . co m
}

From source file:DOMEdit.java

public static void append(Document doc, String name, String phone, String email) {
        Element personNode = doc.createElement("person");

        Element nameNode = doc.createElement("name");
        personNode.appendChild(nameNode);
        Text nametextNode = doc.createTextNode(name);
        nameNode.appendChild(nametextNode);

        Element phoneNode = doc.createElement("phone");
        personNode.appendChild(phoneNode);
        Text phonetextNode = doc.createTextNode(phone);
        phoneNode.appendChild(phonetextNode);

        Element emailNode = doc.createElement("email");
        personNode.appendChild(emailNode);
        Text emailtextNode = doc.createTextNode(email);
        emailNode.appendChild(emailtextNode);

        Element root = doc.getDocumentElement();
        root.appendChild(personNode);//from   w  ww.j  a v a  2 s  .  c o  m
    }

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

private static Element buildAttribute(Document doc, String attributeName, String attributeValue) {
    Element attribute = doc.createElement(ENTRY_ATTRIBUTE_NODE);
    attribute.setAttribute(ENTRY_QUALIFIEDNAME, attributeName);
    Element value = doc.createElement(ENTRY_ATTRIBUTE_VALUE);
    value.setTextContent(attributeValue);
    attribute.appendChild(value);/*from   w w w .  j  ava2 s  .c  om*/
    return attribute;
}

From source file:Main.java

public static Element createElement(Node parent, String tagName) {
    Document doc;
    if (parent instanceof Document) {
        doc = (Document) parent;//from  ww  w.  j  a  va  2s  .  com
    } else {
        doc = parent.getOwnerDocument();
    }
    Element e = doc.createElement(tagName);
    parent.appendChild(e);
    return e;
}

From source file:Main.java

public static Element createElement(Node node, String name, String value, Map<String, Object> attributes) {
    Document doc = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
    Element element = doc.createElement(name);
    element.setTextContent(value);//from w  w w.  ja va2  s  .  com
    addAttributes(element, attributes);
    return element;
}

From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java

@SuppressWarnings("unchecked")
private static Element buildAttributes(Document doc, Map<QName, Serializable> properties) {
    Element attributesE = doc.createElement(ENTRY_ATTRIBUTES_NODE);
    for (Entry<QName, Serializable> entry : properties.entrySet()) {
        Serializable value = entry.getValue();
        if (value != null) {
            Element att = null;/* ww  w .j  av  a  2  s .  c om*/
            if (value instanceof Collection) {
                att = buildAttributeCollection(doc, entry.getKey().getLocalName(), (Collection<?>) value);
            } else {
                att = buildAttribute(doc, entry.getKey().getLocalName(), value.toString());
            }
            attributesE.appendChild(att);
        }
    }
    return attributesE;
}

From source file:Main.java

public static Element createElement(Document document, String name, String value) {
    if (value != null) {
        Element element = (Element) document.createElement(name);
        element.appendChild(document.createTextNode(value));
        return element;
    }//w  w  w .  ja v a2s .  c om
    throw new IllegalArgumentException(
            "XMLDocumentUtils.createElement: value of " + name + " element can't be null.");
}