List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:Main.java
/** * Applies a stylesheet (that receives parameters) to a given xml document. * //from www . jav a 2 s . c o m * @param xmlDocument * the xml document to be transformed * @param parameters * the hashtable with the parameters to be passed to the * stylesheet * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static Document transformDocument(Document xmlDocument, Map<String, String> parameters, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); // set transformation parameters if (parameters != null) { for (Map.Entry<String, String> param : parameters.entrySet()) { transformer.setParameter(param.getKey(), param.getValue()); } } // Create an empty DOMResult object for the output. DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); Document dstDocument = dBuilder.newDocument(); DOMResult domResult = new DOMResult(dstDocument); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), domResult); // Now you can get the output Node from the DOMResult. return dstDocument; }
From source file:Main.java
/** * 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. */// w w w. j a v a2 s . c om 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; } }
From source file:Main.java
public static void createFile(String file_path) { try {//from ww w .j av a2 s . co m DocumentBuilderFactory doc_Factory = DocumentBuilderFactory.newInstance(); DocumentBuilder doc_builder = doc_Factory.newDocumentBuilder(); // root elements Document doc = doc_builder.newDocument(); Element root_element = doc.createElement("Log"); doc.appendChild(root_element); saveFile(file_path, doc); } catch (ParserConfigurationException e) { e.printStackTrace(); } }
From source file:Main.java
public static void hashMapToXML222(String xmlFile, String xpath, HashMap hashmap) { try {//from w w w . j a va 2 s . c o m DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootNode = document.createElement(xpath); document.appendChild(rootNode); Set set = hashmap.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); Element em = document.createElement(me.getKey().toString()); em.appendChild(document.createTextNode(me.getValue().toString())); rootNode.appendChild(em); // System.out.println("write " + // me.getKey().toString() + "=" // + me.getValue().toString()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static Document createXMLResult(String rootName, Map<String, String> map) { try {/*w ww . jav a2s .c o m*/ DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element root = doc.createElement(rootName); doc.appendChild(root); for (String elementName : map.keySet()) { Element child = doc.createElement(elementName); Text text = doc.createTextNode(map.get(elementName)); child.appendChild(text); root.appendChild(child); } return doc; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id) throws Exception { DocumentBuilder b = documentBuilder(); Document doc = b.newDocument(); String str = null;/*from w w w . j ava2 s. c o m*/ SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); ByteArrayOutputStream out = new ByteArrayOutputStream(); Element root = doc.createElement(root_elem_id); doc.appendChild(root); // Now, add all the children for (Entry<String, String> e : content.entrySet()) { Element item = doc.createElement(item_elem_id); item.setAttribute("id", e.getKey()); CDATASection data = doc.createCDATASection(e.getValue()); item.appendChild(data); root.appendChild(item); } try { DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(out); TransformerHandler th = tf.newTransformerHandler(); th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Properties format = new Properties(); format.put(OutputKeys.METHOD, "xml"); // format.put("{http://xml. customer .org/xslt}indent-amount", "4"); // format.put("indent-amount", "4"); // format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd"); format.put(OutputKeys.ENCODING, "UTF-8"); format.put(OutputKeys.INDENT, "yes"); th.getTransformer().setOutputProperties(format); th.setResult(sr); th.getTransformer().transform(ds, sr); str = out.toString(); } catch (Exception e) { e.printStackTrace(); } return str; }
From source file:Main.java
/** * Create document./*w w w . ja v a 2 s.c om*/ * * @return the document * @throws Exception * on error */ public static Document createDocument() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document document; try { builder = factory.newDocumentBuilder(); document = builder.newDocument(); } catch (Exception e) { throw e; } return document; }
From source file:Main.java
/** * Applies a stylesheet (that receives parameters) to a given xml document. * * @param xmlDocument the xml document to be transformed * @param parameters the hashtable with the parameters to be passed to the * stylesheet/*from w w w. j a v a 2s . c o m*/ * @param xsltFilename the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static Document transformDocument(Document xmlDocument, Map<String, String> parameters, String xsltFilename) throws TransformerException, ParserConfigurationException { File xslFile = new File(xsltFilename); if (xslFile.exists()) { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(xsltFilename)); if (transformer != null) { // set transformation parameters if (parameters != null) { for (Map.Entry<String, String> param : parameters.entrySet()) { transformer.setParameter(param.getKey(), param.getValue()); } } // Create an empty DOMResult object for the output. DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); Document dstDocument = dBuilder.newDocument(); DOMResult domResult = new DOMResult(dstDocument); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), domResult); // Now you can get the output Node from the DOMResult. return dstDocument; } } return null; }
From source file:Main.java
public static Document getDocument() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; Document doc = null;/* w w w .j a v a2 s . c om*/ try { builder = factory.newDocumentBuilder(); doc = builder.newDocument(); } catch (ParserConfigurationException e) { System.out.println("Error : XmlUtil.getDocument"); e.printStackTrace(); } finally { } return doc; }
From source file:Main.java
/** * Creates and returns a new empty DOM document. * /*from www . j a v a 2 s. co m*/ * @return a newly created DOM document * @throws ParserConfigurationException */ public static Document newDocument() throws Exception { DocumentBuilder builder = getDocumentBuilder(true); return builder.newDocument(); }