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.git.original.common.config.XMLFileConfigDocument.java

/**
 * ??XML//from w ww  .  j a  v  a2 s .  c  o  m
 * 
 * @param cn
 *            ?
 * @return XML
 */
@SuppressWarnings("unchecked")
static Element convertConfigNode(Document doc, ConfigNode cn) {
    Element elem = doc.createElement(cn.getName());

    Map<String, String> attrMap = cn.attributes();
    if (attrMap != null) {
        for (Entry<String, String> entry : attrMap.entrySet()) {
            elem.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    if (cn.hasChildren()) {
        for (Object child : cn.getAllChildren()) {
            if (child instanceof List) {
                List<ConfigNode> cnList = (List<ConfigNode>) child;
                for (ConfigNode node : cnList) {
                    elem.appendChild(convertConfigNode(doc, node));
                }
            } else {
                elem.appendChild(convertConfigNode(doc, (ConfigNode) child));
            }
        }

    } else if (cn.value != null) {
        elem.setTextContent(cn.value.toString());
    }

    return elem;
}

From source file:Main.java

private static String getValueExceptTags(String xmlContent, String... tags)
        throws TransformerException, SAXException, IOException, ParserConfigurationException {

    Document document = parse(xmlContent, CHARSET_UTF8);
    Element rootElement = document.getDocumentElement();
    replace2TextExceptTags(document, rootElement, tags);

    String rootTag = "xml_root_tag";
    Document newDocument = generate();
    Element newRootElement = newDocument.createElement(rootTag);
    newDocument.appendChild(newRootElement);
    copyNode(newDocument, newRootElement, rootElement);

    String text = write(newDocument);
    int startIndex = text.indexOf(">", text.indexOf("<" + rootTag));
    int lastIndex = text.lastIndexOf("</" + rootTag + ">");

    String result = "";
    if (startIndex > 0 && lastIndex > 0) {
        result = text.substring(startIndex + 1, lastIndex);
    }//from  w  w w.  j  av a  2  s .c  o  m

    return result;
}

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

public static void makeElemStrValue(Document aDocument, Element anElement, String aName, String aValue) {
    Element subElement;//from ww w .j  av  a 2 s . co  m

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

From source file:Main.java

/**
 * Add the breadcrumbs element to the document. This contains the
 * information for navigating back up through the application hierarchy.
 *
 * This creates a breadcrumbs element with child path elements in order
 * (each with a name and path attribute). The breadcrumbs element itself
 * also has a path attribute that specifies the root path.
 *
 * @param document/*from w w  w . j a v  a 2  s .  c  o m*/
 * @param pathPrefix
 * @param relativePath
 */
public static void addBreadcrumbs(Document document, String pathPrefix, String relativePath) {

    if (document != null) {

        Element root = document.getDocumentElement();

        if (root != null) {

            // Create the breadcrumbs element with the associated path being
            // the root path for the set of paths.
            Element element = document.createElement("breadcrumbs");
            element.setAttribute("path", pathPrefix);

            StringBuilder path = new StringBuilder(pathPrefix);
            if (!"".equals(pathPrefix) && !pathPrefix.endsWith("/")) {
                path.append("/");
            }

            boolean first = true;
            for (String term : relativePath.split("/")) {
                if (!"".equals(term) && !".".equals(term)) {
                    if (!first) {
                        path.append("/");
                    }
                    path.append(term);

                    Element pathElement = document.createElement("crumb");
                    pathElement.setAttribute("name", term);
                    pathElement.setAttribute("path", path.toString());
                    element.appendChild(pathElement);

                    first = false;
                }
            }

            root.appendChild(element);
        }
    }
}

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

public static void makeEscElemStrValue(Document aDocument, Element anElement, String aName, String aValue) {
    Element subElement;/*  www .j  ava  2 s. c o m*/

    if ((StringUtils.isNotEmpty(aName)) && (StringUtils.isNotEmpty(aValue))) {
        subElement = aDocument.createElement(aName);
        if (isEscapeNeeded(aValue))
            aValue = escapeElemStrValue(aValue);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Element setChild(Document doc, Element parent, String name) {
    if (parent == null) {
        return null;
    }/*from   ww  w.  j  ava  2s. c o  m*/
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        parent.appendChild(child);
    }
    return child;
}

From source file:com.googlecode.jgenhtml.JGenHtmlUtils.java

public static Element getHitElement(final Document document, final String name, final int hits) {
    Element result = document.createElement("hit");
    result.setAttribute("name", name);
    result.setAttribute("count", String.valueOf(hits));
    return result;
}

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  www  .  java2 s.  c o  m*/
    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;//ww  w .  j  a v a 2  s .co 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:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

/**
 * Transforms a collection of {@link FormField} objects into a simple XML document
 * /* w  w  w . j  a v  a2 s . com*/
 * @param fields
 *            a collection of form fields
 * @return an xml document representing the form fields, or null if the passed collection is null or empty, or all
 *         form fields have an empty value
 */
public static Node formFieldsToMetadata(final Collection<? extends FormField> fields) {
    if (fields != null && !fields.isEmpty()) {
        try {
            final Document xml;

            synchronized (documentBuilderFactory) {
                xml = documentBuilderFactory.newDocumentBuilder().newDocument();
            }

            xml.setXmlStandalone(true);
            final Element metaData = xml.createElement("metadata");
            int completedFieldCount = 0;

            for (final FormField field : fields) {
                if (!StringUtils.isEmpty(field.getValue())) {
                    completedFieldCount++;
                    final Element metaDataEntry = xml.createElement("metadataentry");
                    final Element entryName = xml.createElement("entryname");
                    final Element entryValue = xml.createElement("entryvalue");

                    entryName.setTextContent(field.getDisplayName());
                    entryValue.setTextContent(field.getValue());

                    metaDataEntry.appendChild(entryName);
                    metaDataEntry.appendChild(entryValue);
                    metaData.appendChild(metaDataEntry);
                }
            }

            if (completedFieldCount > 0) {
                xml.appendChild(metaData);
                return xml;
            } else {
                return null;
            }

        } catch (final ParserConfigurationException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return null;
}