Java Utililty Methods XML Element Create

List of utility methods to do XML Element Create

Description

The list of methods to do XML Element Create are organized into topic(s).

Method

ElementcreateElement(String tag, String textContent, Document xml)
Creates and returns an element with the specified tag and text content
Element element = xml.createElement(tag);
element.setTextContent(textContent);
return element;
ElementcreateElement(String tagName, String text, Document doc)
create Element
Element elem = doc.createElement(tagName);
if ((text != null) && !text.equals(""))
    elem.appendChild(doc.createTextNode(text));
return elem;
ElementcreateElementAndText(Element parent, String elementName, String text)
create Element And Text
if (text == null) {
    throw new NullPointerException("null text");
Element el = parent.getOwnerDocument().createElement(elementName);
parent.appendChild(el);
Text txt = el.getOwnerDocument().createTextNode(text);
el.appendChild(txt);
return el;
...
ElementcreateElementInSameNamespace(Element parent, String localName)
create a new element in the same namespace

String qName = localName;
String prefix = parent.getPrefix();
if (prefix != null && prefix.length() > 0) {
    qName = prefix + ":" + localName;
String nsUri = parent.getNamespaceURI();
return parent.getOwnerDocument().createElementNS(nsUri, qName);
ElementcreateElementInTempDocument(String name, String prefix, String namespaceURI)
create Element In Temp Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
try {
    Document doc = dbf.newDocumentBuilder().newDocument();
    return createElement(doc, name, prefix, namespaceURI);
} catch (ParserConfigurationException ex) {
    return null;
ElementcreateElementLn(Document d, String name, String value, boolean isCDATA)
Creates a DOM element node with the given name and contents.
Element e = createElement(d, name);
if (isCDATA) {
    e.appendChild(createTextLn(d));
    e.appendChild(createCDATA(d, value));
    e.appendChild(createTextLn(d));
} else {
    e.appendChild(createTextLn(d, "\n" + value));
return e;
MapcreateElementMapping(final Document doc1, final Document doc2)
Creates the element mapping for the specified documents.
final Map<Node, Node> mapping = new HashMap<>();
createElementMappingForNodes(doc1.getDocumentElement(), doc2.getDocumentElement(), mapping);
return mapping;
voidcreateElementMappingForNodes(final Node n1, final Node n2, final Map mapping)
Creates the element mapping for the specified nodes and all their child nodes.
mapping.put(n1, n2);
final NodeList childNodes1 = n1.getChildNodes();
final NodeList childNodes2 = n2.getChildNodes();
final int count = Math.min(childNodes1.getLength(), childNodes2.getLength());
for (int i = 0; i < count; i++) {
    createElementMappingForNodes(childNodes1.item(i), childNodes2.item(i), mapping);
ElementcreateElementNS(Document doc, String namespaceURI, String prefix, String nodeName)
Creates and returns a new XML element node with the given name and namespace URI.
String fullName = (prefix == null) ? nodeName : prefix + ":" + nodeName;
Element e = doc.createElementNS(namespaceURI, fullName);
if (printXMLNamespaces) {
    String attrName = (prefix == null) ? "xmlns" : "xmlns:" + prefix;
    e.setAttribute(attrName, namespaceURI);
return e;
ElementcreateElementNS(Document root, String namespaceURI, String qualifiedName)
create Element NS
return root.createElementNS(namespaceURI, qualifiedName);