Java tutorial
//package com.java2s; /* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ import com.google.common.base.Optional; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main { public static final String XMLNS_ATTRIBUTE_KEY = "xmlns"; public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; public static Element createTextElement(final Document document, final String qName, final String content, final Optional<String> namespaceURI) { Element typeElement = createElement(document, qName, namespaceURI); typeElement.appendChild(document.createTextNode(content)); return typeElement; } public static Element createElement(final Document document, final String qName, final 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); } }