Java XML Element Create createElement(Document doc, String expr, Element parentElement, String elementName, boolean firstChild, Map attributes)

Here you can find the source of createElement(Document doc, String expr, Element parentElement, String elementName, boolean firstChild, Map attributes)

Description

Generic API to update or create DOM elements

License

Open Source License

Declaration

public static Element createElement(Document doc, String expr, Element parentElement, String elementName,
        boolean firstChild, Map<String, String> attributes) throws Exception 

Method Source Code

//package com.java2s;
/**/*  w w  w  . j a  v  a2 s.  com*/
 * Copyright (c) Microsoft Corporation
 *
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;

import javax.xml.xpath.XPathFactory;

import java.util.Map;

public class Main {
    public static final String INVALID_ARG = "Invalid argument.";

    /** Generic API to update or create DOM elements */
    public static Element createElement(Document doc, String expr, Element parentElement, String elementName,
            boolean firstChild, Map<String, String> attributes) throws Exception {

        if (doc == null) {
            throw new IllegalArgumentException(INVALID_ARG);
        } else {
            XPath xPath = XPathFactory.newInstance().newXPath();
            Element element = null;
            if (expr != null)
                element = (Element) xPath.evaluate(expr, doc, XPathConstants.NODE);

            // If element doesn't exist create one
            if (element == null) {
                element = doc.createElement(elementName);
                if (firstChild) {
                    parentElement.insertBefore(element,
                            parentElement != null ? parentElement.getFirstChild() : null);
                } else {
                    parentElement.appendChild(element);
                }
            }

            if (attributes != null && !attributes.isEmpty()) {
                for (Map.Entry<String, String> attribute : attributes.entrySet()) {
                    element.setAttribute(attribute.getKey(), attribute.getValue());
                }
            }
            return element;
        }
    }
}

Related

  1. createElement(Document d, String name, String value, boolean isCDATA)
  2. createElement(Document d, String tagName)
  3. createElement(Document doc, Node parent, int depth, String name, String contents)
  4. createElement(Document doc, Node parent, String tagName)
  5. createElement(Document doc, String elementNamespace, String elementName)
  6. createElement(Document doc, String name, Map attributes)
  7. createElement(Document doc, String name, Object text, Map attributes)
  8. createElement(Document doc, String name, String prefix, String namespaceURI)
  9. createElement(Document doc, String tag, String data)