- public static Element createTextElement(Document document, String name, String content) {
- Element typeElement = document.createElement(name);
- typeElement.appendChild(document.createTextNode(content));
- return typeElement;
- }
-
- public static void addNamespaceAttr(Element root, String namespace) {
- root.setAttribute(XMLNS_ATTRIBUTE_KEY, namespace);
+ public static Element createElement(final Document document, String qName, Optional<String> namespaceURI) {
+ if(namespaceURI.isPresent()) {
+ final Element element = document.createElementNS(namespaceURI.get(), qName);
+ String name = XMLNS_ATTRIBUTE_KEY;
+ if(element.getPrefix() != null) {
+ name += ":" + element.getPrefix();
+ }
+ element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
+ return element;
+ }
+ return document.createElement(qName);