public static Document createDocument() { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); return doc; } catch (Exception e) { throw new RuntimeException(e); } }
public CatalogWriter() { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(false); builderFactory.setValidating(false); document = builderFactory.newDocumentBuilder().newDocument(); catalog = document.createElement("catalog"); document.appendChild(catalog); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Creates a fresh empty DOM document and adds nodes under this document. */ public SAX2DOMEx(DocumentBuilderFactory f) throws ParserConfigurationException { f.setValidating(false); document = f.newDocumentBuilder().newDocument(); node = document; nodeStack.push(document); }
private static Document makeErr(String msg) { Document d=XmlUtils.getNewDocumentBuilder().newDocument(); Element span = d.createElement("span"); span.setAttribute("style", "color:red;"); d.appendChild(span); Text err = d.createTextNode( msg ); span.appendChild(err); return d; }
/** * Test method for {@link XmlUtils#changeNamespace(Node, String, String)}. */ @Test public void testChangeNamespaceWithNoRootNamespace() throws Exception { Document doc = XmlUtils.getDocumentBuilder().newDocument(); Element root = doc.createElement("root"); root = (Element) doc.appendChild(root); final Element child = doc.createElement("child"); root.appendChild(child); final String ns2 = "urn:namespace2"; final Element childWithNamespace = doc.createElementNS(ns2, "childWithNamespace"); root.appendChild(childWithNamespace); root.appendChild(doc.createTextNode("some text")); assertEquals(null, root.getNamespaceURI()); assertEquals(null, child.getNamespaceURI()); assertEquals(ns2, childWithNamespace.getNamespaceURI()); final String ns1 = "urn:namespace1"; root = (Element) XmlUtils.changeNamespace(root, XMLConstants.NULL_NS_URI, ns1); assertEquals(ns1, root.getNamespaceURI()); assertEquals(ns1, root.getElementsByTagName("child").item(0).getNamespaceURI()); assertEquals(ns2, root.getElementsByTagName("childWithNamespace").item(0).getNamespaceURI()); }
/** * Output the properties as xml output. * @param props the properties to save * @param os the output stream to write to (Note this gets closed) * @throws IOException on error in writing to the stream */ protected void xmlSaveProperties(Properties props, OutputStream os) throws IOException { // create XML document Document doc = getDocumentBuilder().newDocument(); Element rootElement = doc.createElement(PROPERTIES); List<Tuple> sorted = sortProperties(props); // output properties for (Tuple tuple : sorted) { Element propElement = doc.createElement(PROPERTY); propElement.setAttribute(ATTR_NAME, tuple.key); propElement.setAttribute(ATTR_VALUE, tuple.value); rootElement.appendChild(propElement); } try (Writer wri = new OutputStreamWriter(os, "UTF8")) { wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); new DOMElementWriter().write(rootElement, wri, 0, "\t"); wri.flush(); } catch (IOException ioe) { throw new BuildException("Unable to write XML file", ioe); } }
@Test public void testChangeNamespaceWithExistingRootNamespace() throws Exception { Document doc = XmlUtils.getDocumentBuilder().newDocument(); final String ns0 = "urn:namespace0"; Element root = doc.createElementNS(ns0, "root"); root = (Element) doc.appendChild(root); final Element child = doc.createElementNS(ns0, "child"); root.appendChild(child); final String ns2 = "urn:namespace2"; final Element childWithNamespace = doc.createElementNS(ns2, "childWithNamespace"); root.appendChild(childWithNamespace); root.appendChild(doc.createTextNode("some text")); assertEquals(ns0, root.getNamespaceURI()); assertEquals(ns0, child.getNamespaceURI()); assertEquals(ns2, childWithNamespace.getNamespaceURI()); final String ns1 = "urn:namespace1"; root = (Element) XmlUtils.changeNamespace(root, ns0, ns1); assertEquals(ns1, root.getNamespaceURI()); assertEquals(ns1, root.getElementsByTagName("child").item(0).getNamespaceURI()); assertEquals(ns2, root.getElementsByTagName("childWithNamespace").item(0).getNamespaceURI()); }
/** * A customised XSLT might just want the <style> element, because it customises * the rest of the <head> element. * * @param conversionContext * @return */ public static DocumentFragment appendStyleElement(HTMLConversionContext conversionContext) { // Create a DOM document to take the results Document document = XmlUtils.getNewDocumentBuilder().newDocument(); StringBuilder buffer = new StringBuilder(10240); // <style.. Element element = createStyleElement(conversionContext, document, buffer); if (element == null) { return null; } document.appendChild(element); DocumentFragment docfrag = document.createDocumentFragment(); docfrag.appendChild(document.getDocumentElement()); return docfrag; }
public Document createMetadataDocument() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); Document d = factory.newDocumentBuilder().newDocument(); Element rootElement = d.createElementNS(DIDLContent.DESC_WRAPPER_NAMESPACE_URI, "desc-wrapper"); d.appendChild(rootElement); return d; } catch (Exception ex) { throw new RuntimeException(ex); } } }
public static Document newDocument() { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); return dbf.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } }
Document document = XmlUtils.getNewDocumentBuilder().newDocument(); Element imageElement = document.createElement("img"); Element linkElement = document.createElement("a"); document.appendChild(imageElement);
/** * Creates a fresh empty DOM document and adds nodes under this document. * @deprecated */ public SAX2DOMEx() throws ParserConfigurationException { DocumentBuilderFactory factory = XmlFactory.createDocumentBuilderFactory(false); factory.setValidating(false); document = factory.newDocumentBuilder().newDocument(); node = document; nodeStack.push(document); }
@Override public Node toNode(WordprocessingMLPackage wmlPackage, SdtPr sdtPr, HashMap<String, String> tagMap, NodeIterator childResults) throws TransformerException { try { // Create a DOM builder and parse the fragment Document document = XmlUtils.getNewDocumentBuilder().newDocument(); DocumentFragment docfrag = document.createDocumentFragment(); if (tagMap.get("HTML_ELEMENT") == null) { // don't add a list return attachContents(docfrag, docfrag, childResults); } else if (tagMap.get("HTML_ELEMENT").equals("OL")) { Element xhtmlDiv = document.createElement("ol"); docfrag.appendChild(xhtmlDiv); return attachContents(docfrag, xhtmlDiv, childResults); } else { // if (tagMap.get("HTML_ELEMENT").equals("UL")) { Element xhtmlDiv = document.createElement("ul"); docfrag.appendChild(xhtmlDiv); return attachContents(docfrag, xhtmlDiv, childResults); } } catch (Exception e) { throw new TransformerException(e); } }
/** * A customised XSLT might just want the <script> element, because it customises * the rest of the <head> element. * * @param conversionContext * @return */ public static DocumentFragment appendScriptElement(HTMLConversionContext conversionContext) { // This method is necessary, since it is how the customised XSLT // actually invokes ConversionHTMLScriptElementHandler // Create a DOM document to take the results Document document = XmlUtils.getNewDocumentBuilder().newDocument(); StringBuilder buffer = new StringBuilder(10240); // <script.. Element element = createScriptElement(conversionContext, document, buffer); if (element == null) { return null; } document.appendChild(element); DocumentFragment docfrag = document.createDocumentFragment(); docfrag.appendChild(document.getDocumentElement()); return docfrag; }
private Document initDocument() throws IOException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IOException("Failed to create settings document builder", e); } Document document = documentBuilder.newDocument(); Element settingsElement = document.createElement(SCHEMA_DOCUMENT_ELEMENT); settingsElement.setAttribute(SCHEMA_MODEL_VERSION, "" + getRevisionNumber()); document.appendChild(settingsElement); return document; }
private Document createDocument() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = factory.newDocumentBuilder(); return parser.newDocument(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } }
Document document = XmlUtils.getNewDocumentBuilder().newDocument(); Element headEl = document.createElement("head"); Element meta = document.createElement("meta"); Element element = null; StringBuilder buffer = new StringBuilder(10240); document.appendChild(headEl);
try { Document document = XmlUtils.getNewDocumentBuilder().newDocument(); DocumentFragment docfrag = document.createDocumentFragment(); Element xhtmlDiv = document.createElement("ol"); docfrag.appendChild(xhtmlDiv); return attachContents(docfrag, xhtmlDiv, resultSoFar); Element xhtmlDiv = document.createElement("ul"); docfrag.appendChild(xhtmlDiv); return attachContents(docfrag, xhtmlDiv, resultSoFar);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); dom = db.newDocument(); Element rootEle = dom.createElement("roles"); e = dom.createElement("role1"); e.appendChild(dom.createTextNode(role1)); rootEle.appendChild(e); e = dom.createElement("role2"); e.appendChild(dom.createTextNode(role2)); rootEle.appendChild(e); rootEle.appendChild(e); dom.appendChild(rootEle); tr.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(xml)));
public SAX2DOM() throws ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); _document = factory.newDocumentBuilder().newDocument(); _root = _document; }