Java Utililty Methods XML Element Set

List of utility methods to do XML Element Set

Description

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

Method

voidsetTextContent(Element element, String content)
set Text Content
Document document = element.getOwnerDocument();
element.appendChild(document.createTextNode(content));
voidsetTextContent(Element element, String text)
Removes any existing child content and inserts a text node with the given text
Node child;
while ((child = element.getFirstChild()) != null) {
    element.removeChild(child);
if (text != null && text.length() > 0) {
    Text textNode = element.getOwnerDocument().createTextNode(text);
    element.appendChild(textNode);
voidsetTextContent(Element node, String text)
This method is useful when DOM Level 3 "setTextContent" is not implemented
final NodeList children = node.getChildNodes();
for (int i = (children.getLength() - 1); i > -1; i--) {
    node.removeChild(children.item(i));
node.appendChild(node.getOwnerDocument().createTextNode(text));
voidsetTextContent(Element node, String text)
set Text Content
final NodeList children = node.getChildNodes();
for (int i = (children.getLength() - 1); i > -1; i--) {
    node.removeChild(children.item(i));
node.appendChild(node.getOwnerDocument().createTextNode(text));
TextsetTextField(Element node, String subElement, String text)
set Text Field
NodeList nl = node.getElementsByTagName(subElement);
if (nl == null || nl.getLength() == 0) {
    if (text == null)
        return null;
    Document d = node.getOwnerDocument();
    Element e = d.createElement(subElement);
    Text ans = d.createTextNode(text);
    e.appendChild(ans);
...
voidsetTextValue(Element element, String text)
Replace the first text element of the given element with a new text element containing the given text or just create a new text element if there is none to replace.
Node newTextNode = element.getOwnerDocument().createTextNode(text);
NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    Node node = childs.item(i);
    if (Node.TEXT_NODE == node.getNodeType()) {
        element.replaceChild(newTextNode, node);
        return;
element.appendChild(newTextNode);
voidsetTextValue(Element element, String value)
set Text Value
removeAllChildNodes(element);
if (value != null) {
    Node text = element.getOwnerDocument().createTextNode(value);
    element.appendChild(text);
voidsetToElements(Element rootElem, String name, Set s)
set To Elements
iteratorToElements(rootElem, name, s.iterator());
voidsetValue(Element element, String value)
Set value to the specified element
removeAllChilds(element);
addValue(element, value);