List of usage examples for org.w3c.dom Document appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Main.java
public static Document documentFromNode(Element element) { try {//from w w w . ja v a 2 s . c o m DocumentBuilder dBuilder = getDocumentBuilder(); Document newDocument = dBuilder.newDocument(); Node importedNode = newDocument.importNode(element, true); newDocument.appendChild(importedNode); return newDocument; } catch (Exception e) { throw new RuntimeException(e); } }
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); 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);/*from www. j av a 2 s.co m*/ } return doc; }
From source file:Main.java
public static Document createXMLResult(String rootName, Map<String, String> map, Document d) { try {// w w w .j ava 2 s . c o m Element r = d.createElement(rootName); d.appendChild(r); for (String elementName : map.keySet()) { Element eltName = d.createElement(elementName); eltName.appendChild(d.createTextNode(map.get(elementName))); r.appendChild(eltName); } d.normalizeDocument(); return d; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Utils.java
/** * Start a new XML Document.// w w w. j a v a 2 s .com * @param rootName The name of the Document root Element (created here) * @return the Document * @throws DomException */ public static Document createXmlDocument(String rootName) { Document document = getXmlDocumentBuilder().newDocument(); Element root = document.createElement(rootName); document.appendChild(root); return document; }
From source file:Main.java
/** * Creates a XML document with the given root element and the given {@link NodeList} as content. * @param root The root element of the XML document. * @param content Content of the XML document. * @return The created XML document.//from ww w. j a v a 2 s . com */ public static Document createDocument(String root, NodeList content) { DocumentBuilder docBuilder = createDocumentBuilder(); Document document = docBuilder.newDocument(); Element rootElement = document.createElement(root); document.appendChild(rootElement); for (int i = 0; i < content.getLength(); i++) { Node item = content.item(i); item = document.adoptNode(item.cloneNode(true)); rootElement.appendChild(item); } return document; }
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); ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row);//from www. j a va2s. c o m 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 Node createDocument(String rootElementName) { try {//from w ww . j a v a2 s . c om DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(rootElementName); doc.appendChild(rootElement); return rootElement; } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Document createDocument(String mainType, String customType) throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); document.setXmlStandalone(false);//w w w .ja v a2s . c om Element sync = document.createElement("Sync"); document.appendChild(sync); Element entity = document.createElement("Entity"); entity.setAttribute("mainType", mainType); if (customType.length() > 0) entity.setAttribute("customType", customType); document.getElementsByTagName("Sync").item(0).appendChild(entity); return document; }
From source file:Main.java
public static void createXML222(String xmlFile, String xpath, String element, String data) { try {/* w w w .j a v a 2 s.co m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(xpath); document.appendChild(rootElement); for (int i = 1; i <= 1; i++) { Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } 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 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); while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row);//from w w w . j a va 2 s . co m 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; }