List of usage examples for javax.xml.parsers DocumentBuilder newDocument
public abstract Document newDocument();
From source file:com.viettel.ws.client.JDBCUtil.java
/** * Create document using DOM api//from www. j av a2 s.c o m * * @param rs a result set * @return A document of a result set * @throws ParserConfigurationException - If error when parse string * @throws SQLException - If error when read data from database */ public static Document toDocument(ResultSet rs) throws ParserConfigurationException, SQLException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(FEATURE_GENERAL_ENTITIES, false); factory.setFeature(FEATURE_PARAMETER_ENTITIES, false); factory.setXIncludeAware(false); factory.setExpandEntityReferences(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results); 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:com.twentyn.patentExtractor.Util.java
public static Document nodeToDocument(DocumentBuilder docBuilder, String documentContainer, Node n) { /* With help from://www.j a va 2 s.co m * http://examples.javacodegeeks.com/core-java/xml/dom/copy-nodes-subtree-from-one-dom-document-to-another/ */ org.w3c.dom.Document newDoc = docBuilder.newDocument(); Element rootElement = newDoc.createElement(documentContainer); Node newNode = newDoc.importNode(n, true); rootElement.appendChild(newNode); newDoc.appendChild(rootElement); return newDoc; }
From source file:Main.java
public static void vectorToXML222(String xmlFile, String xpath, String parentNodeName, Vector<HashMap> vector) { File file = new File(xmlFile); try {// w w w . java 2 s .c om DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document; Element rootNode; if (file.exists()) { document = documentBuilder.parse(new File(xmlFile)); rootNode = document.getDocumentElement(); } else { document = documentBuilder.newDocument(); rootNode = document.createElement(xpath); document.appendChild(rootNode); } for (int x = 0; x < vector.size(); x++) { Element parentNode = document.createElement(parentNodeName); rootNode.appendChild(parentNode); HashMap hashmap = vector.get(x); Set set = hashmap.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); // System.out.println("key=" + // me.getKey().toString()); Element em = document.createElement(me.getKey().toString()); em.appendChild(document.createTextNode(me.getValue().toString())); parentNode.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) { file.delete(); ex.printStackTrace(); } }
From source file:hoot.services.utils.XmlDocumentBuilder.java
/** * Creates a new XML DOM/*from ww w .j a v a2s. c o m*/ * * @return XML document * @throws IOException */ public static Document create() throws IOException { DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = dBF.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IOException("Error creating document builder. (" + e.getMessage() + ")"); } return builder.newDocument(); }
From source file:de.ingrid.interfaces.csw.domain.encoding.impl.XMLEncoding.java
protected static Document extractFromDocument(Node node) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);// w ww .j a va 2s. c o m DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.newDocument(); Node copy = node.cloneNode(true); Node adopted = doc.adoptNode(copy); doc.appendChild(adopted); return doc; }
From source file:com.moviejukebox.tools.DOMHelper.java
/** * Create a blank Document/*from w w w. ja va 2s . c o m*/ * * @return a Document * @throws ParserConfigurationException */ public static Document createDocument() throws ParserConfigurationException { DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); return docBuilder.newDocument(); }
From source file:Main.java
private static Document generate() throws ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); return document; }
From source file:Main.java
/** * Create empty document TO BE DEBUGGED!. *///from w ww .ja v a 2 s. c o m public static Document createDocument() { DocumentBuilder documentBuilder = null; // System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); try { documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException pce) { warn("ParserConfigurationException: " + pce); return null; } return documentBuilder.newDocument(); }
From source file:com.bluexml.side.Integration.alfresco.xforms.webscript.XmlBuilder.java
public static Document buildEntry(QName type, String objectId, Map<QName, Serializable> properties, List<AssociationBean> list) { // Document entry = new Document(); DocumentBuilder documentBuilder = null; try {/*from w w w. ja va 2 s. c om*/ documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); throw new RuntimeException("Couldn't create a document builder."); } Document doc = documentBuilder.newDocument(); Element root = doc.createElement(ENTRY_ROOTNODE); root.setAttribute(ENTRY_QUALIFIEDNAME, type.getLocalName()); root.setAttribute(ENTRY_ID, objectId); Element attributesE = buildAttributes(doc, properties); Element associationsE = buildAssociations(doc, list); root.appendChild(attributesE); root.appendChild(associationsE); doc.appendChild(root); return doc; }
From source file:apps.ParsedPost.java
private static String createYahooAnswersQuestion(ParsedPost parentPost, ParsedPost post) throws ParserConfigurationException, TransformerException { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("document"); doc.appendChild(rootElement);/* ww w. j av a2 s .c o m*/ Element uri = doc.createElement("uri"); uri.setTextContent(parentPost.mId); rootElement.appendChild(uri); Element subject = doc.createElement("subject"); subject.setTextContent(parentPost.mTitle); rootElement.appendChild(subject); Element content = doc.createElement("content"); content.setTextContent(parentPost.mBody); rootElement.appendChild(content); Element bestanswer = doc.createElement("bestanswer"); bestanswer.setTextContent(post.mBody); rootElement.appendChild(bestanswer); Element answer_item = doc.createElement("answer_item"); answer_item.setTextContent(post.mBody); Element nbestanswers = doc.createElement("nbestanswers"); nbestanswers.appendChild(answer_item); rootElement.appendChild(nbestanswers); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return "<vespaadd>" + xhlp.removeHeader(sw.toString()).replace("&", "&") + "</vespaadd>\n"; }