List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:Main.java
public static <T> Document marshal(Object jaxbElement, Class<T> jaxbFactory) throws JAXBException { if (!(jaxbElement instanceof JAXBElement<?>)) { throw new JAXBException("Must be a instance of JAXBElement<?>"); }/*from w ww.ja v a 2 s .com*/ Document doc = null; try { JAXBContext jc = JAXBContext.newInstance(jaxbFactory); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument(); marshaller.marshal(jaxbElement, doc); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
public static <T> Element marshal(JAXBElement<T> jaxbElement, Class<T> cls) { try {//from w ww. j a v a2s . co m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); JAXBContext jaxbContext = JAXBContext.newInstance(cls); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(jaxbElement, doc); return doc.getDocumentElement(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static Document createDocument(String paramString) throws Exception { DocumentBuilderFactory localDocumentBuilderFactory = getDocumentBuilderFactoryInstance(); DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder(); Document localDocument = localDocumentBuilder.newDocument(); Element localElement = localDocument.createElement(paramString); localDocument.appendChild(localElement); return localDocument; }
From source file:Main.java
public static Document documentify(ResultSet rs) throws ParserConfigurationException, SQLException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);//from ww w .j a v a 2 s . c o m ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); Element node = doc.createElement(columnName); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } return doc; }
From source file:Main.java
public static Document parseMetadataMap(Map<String, String> metainfo) throws Exception { DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); DocumentBuilder bd = fact.newDocumentBuilder(); Document doc = bd.newDocument(); Element rt = doc.createElement("ROOT"); doc.appendChild(rt);/*from w w w . jav a 2 s . com*/ for (String key : metainfo.keySet()) { Element e1 = doc.createElement("param"); e1.setAttribute("key", key); String value = metainfo.get(key); e1.setAttribute("value", value); rt.appendChild(e1); } return doc; }
From source file:Main.java
public static String convertResultSetToXML(ResultSet rs) throws SQLException, ParserConfigurationException, TransformerException { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);/*from w ww. ja v a 2s.com*/ while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); if (value != null) { Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_")); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
static public Document getNewDocument() throws ParserConfigurationException { //Create instance of DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Get the DocumentBuilder DocumentBuilder docBuilder = factory.newDocumentBuilder(); //Create blank DOM Document Document doc = docBuilder.newDocument(); return doc;//from ww w .j ava 2 s. c o m }
From source file:Main.java
public static Document newDocument(boolean isNamespaceAware) { try {// w w w .j av a 2 s .c o m DocumentBuilder builder = newDocumentBuilder(null, isNamespaceAware, false); return builder.newDocument(); } catch (SAXException ex) { // should never happen return null; } }
From source file:Main.java
private static String getXmlString(Document document, String tagName) throws ParserConfigurationException, SAXException, IOException, TransformerException { NodeList nodeList = document.getElementsByTagName(tagName); if (nodeList.getLength() < 1) { throw new IllegalArgumentException("no " + tagName + " found"); }/* w w w . j a v a2s. c om*/ Node sub = nodeList.item(0); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document subdoc = db.newDocument(); subdoc.appendChild(sub.cloneNode(true)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(subdoc); StringWriter sw = new StringWriter(); StreamResult xmlResult = new StreamResult(sw); transformer.transform(domSource, xmlResult); sw.flush(); return sw.toString(); }
From source file:Main.java
/** * Creates a black (empty) DOM document//from ww w. jav a 2s . c om * * @return The document * @throws ParserConfigurationException * javax.xml.parsers.ParserConfigurationException */ public static Document createBlankDocument() throws ParserConfigurationException { // Creates a instance of DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Gets the DocumentBuilder DocumentBuilder parser = factory.newDocumentBuilder(); // Creates a blank DOM Document return parser.newDocument(); }