Java tutorial
//package com.java2s; /** * Copyright (C) 2012-2014 Blake Dickie * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; public class Main { public static final String xmlnsURI = "http://www.w3.org/2000/xmlns/"; public static final String schemaInstanceNS = "http://www.w3.org/2001/XMLSchema-instance"; /** * Creates a new DOM Tree with a root element containing the schema attributes. * * @param rootName The name of the root element. * @param namespaceURI The uri of the namespace of the document. * @param namespacePrefix The prefix to use for the namespace (ie. the part * before the ':' in the root element. * @param namespaceXSD The name of the xsd file used to validate the file. * Should be given relative to xsdBase. */ public static Document newXMLTree(String rootName, String namespaceURI, String namespacePrefix, String xsdBase, String namespaceXSD) { try { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); DocumentBuilder b = f.newDocumentBuilder(); Document doc = b.newDocument(); Element rootNode; if (namespaceURI == null) { rootNode = doc.createElement(rootName); } else { rootNode = doc.createElementNS(namespaceURI, rootName); if (namespacePrefix != null) { rootNode.setPrefix(namespacePrefix); } rootNode.setAttributeNS(xmlnsURI, "xmlns:xsi", schemaInstanceNS); if (namespacePrefix == null || namespacePrefix.isEmpty()) { rootNode.setAttributeNS(xmlnsURI, "xmlns", namespaceURI); } else { rootNode.setAttributeNS(xmlnsURI, "xmlns:" + namespacePrefix, namespaceURI); } if (xsdBase != null && namespaceXSD != null) { rootNode.setAttributeNS(schemaInstanceNS, "xsi:schemaLocation", namespaceURI + " " + xsdBase + namespaceXSD); } } doc.appendChild(rootNode); return doc; } catch (Exception e) { return null; } } /** * Creates a new DOM Tree with a root element containing the schema attributes. * * @param rootName The name of the root element. * @param namespaceURI The uri of the namespace of the document. * @param namespacePrefix The prefix to use for the namespace (ie. the part * before the ':' in the root element. * @param namespaceXSD The name of the xsd file used to validate the file. * Should be given relative to xsdBase. */ public static Document newXMLTree(String rootName) { return newXMLTree(rootName, null, null, null, null); } }