List of usage examples for org.dom4j Document setRootElement
void setRootElement(Element rootElement);
From source file:com.zimbra.common.soap.Element.java
License:Open Source License
public org.dom4j.Element toXML() { org.dom4j.Document doc = new org.dom4j.tree.DefaultDocument(); doc.setRootElement(toXML(null)); return doc.getRootElement(); }
From source file:delphsim.model.Epidemia.java
License:Open Source License
/** * Guarda la informacin de esta epidemia en el archivo pasado como * parmetro, siguiendo el esquema del .xsd de la aplicacin. * @param archivoDestino El archivo destino donde se guardar el modelo. * @throws java.io.IOException Si hay problemas al escribir en disco. * @throws org.dom4j.DocumentException Si hay problemas al crear el objeto de tipo rbol. * @throws java.lang.Exception Si se produce algn otro problema. *///from w ww.ja va 2 s. co m public void guardarXML(File archivoDestino) throws IOException, DocumentException, Exception { // Primero crear el documento dom4j con la informacin del modelo Document documento = DocumentHelper.createDocument(); // Elemento raz epidemia Element elementoEpidemia = new DefaultElement("epidemia"); documento.setRootElement(elementoEpidemia); elementoEpidemia.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); elementoEpidemia.addAttribute("xsi:noNamespaceSchemaLocation", "DelphSim1.18.xsd"); elementoEpidemia.addAttribute("unidadTiempo", this.unidadTiempo); // Elementos parmetros if (this.parametros != null) { for (Parametro param : this.parametros) { Element elementoParametro = param.volcarAXML(); elementoEpidemia.add(elementoParametro); } } // Elementos procesos if (this.procesos != null) { for (Proceso proc : this.procesos) { Element elementoProceso = proc.volcarAXML(); elementoEpidemia.add(elementoProceso); } } // Elemento poblacin Element elementoPoblacion = this.poblacion.volcarAXML(); elementoEpidemia.add(elementoPoblacion); // Elementos compartimentos for (Compartimento comp : this.compartimentos) { Element elementoCompartimento = comp.volcarAXML(); elementoEpidemia.add(elementoCompartimento); } // Luego crear el formato, stream y escritor de la salida OutputFormat formato = OutputFormat.createPrettyPrint(); formato.setEncoding("UTF-16"); formato.setIndent("\t"); formato.setNewLineAfterDeclaration(false); formato.setPadText(false); formato.setTrimText(true); formato.setXHTML(true); java.io.OutputStreamWriter salida = new java.io.OutputStreamWriter( new java.io.FileOutputStream(archivoDestino), "UTF-16"); XMLWriter escritor = new XMLWriter(salida, formato); // Y escribir escritor.write(documento); escritor.close(); }
From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java
private Element createRdfRootElement() { // setup document rdf root element Document doc = new DocumentFactory().createDocument(); Element rdf = addElement(doc, "RDF", new Namespace("rdf", rdfNS)); doc.setRootElement(rdf); rdf.add(new Namespace("mads", madsNS)); rdf.add(new Namespace("rdf", rdfNS)); rdf.add(new Namespace("dams", prNS)); return rdf;//from w w w . j a va2s. c om }
From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java
public static Document toXML(Map m) { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("response"); doc.setRootElement(root); Iterator keys = m.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); Object val = m.get(key); Element e = root.addElement(key); if (val instanceof String) { e.setText(val.toString()); } else if (val instanceof Collection) { Collection col = (Collection) val; for (Iterator it = col.iterator(); it.hasNext();) { Object o = it.next(); Element sub = e.addElement("value"); if (o instanceof Map) { Map valmap = (Map) o; Iterator fields = valmap.keySet().iterator(); while (fields.hasNext()) { String field = (String) fields.next(); Element sub2 = sub.addElement(field); sub2.setText(valmap.get(field).toString()); }//ww w . ja va 2 s . c o m } else { sub.setText(o.toString()); } } } else if (val instanceof Map) { Map m2 = (Map) val; for (Iterator it = m2.keySet().iterator(); it.hasNext();) { String k2 = (String) it.next(); String v2 = (String) m2.get(k2); Element sub = e.addElement("value"); sub.addAttribute("key", k2); sub.setText(v2); } } else if (val instanceof Exception) { Exception ex = (Exception) val; e.addElement("p").setText(ex.toString()); StackTraceElement[] elem = ex.getStackTrace(); for (int i = 0; i < elem.length; i++) { e.addElement("p").setText(elem[i].toString()); } } else { e.setText(String.valueOf(val)); } } return doc; }
From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java
public static String toHTML(Map m) { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("html"); doc.setRootElement(root); Element body = root.addElement("body"); Element table = body.addElement("table"); Iterator keys = m.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); Object val = m.get(key); Element row = table.addElement("tr"); Element keyCell = row.addElement("td"); keyCell.setText(key);/*from w ww . j a v a 2 s . com*/ Element valCell = row.addElement("td"); if (val instanceof String) { valCell.setText(val.toString()); } else if (val instanceof Collection) { Collection col = (Collection) val; for (Iterator it = col.iterator(); it.hasNext();) { Element p = valCell.addElement("p"); Object o = it.next(); if (o instanceof Map) { Map valmap = (Map) o; Iterator fields = valmap.keySet().iterator(); while (fields.hasNext()) { String field = (String) fields.next(); String value = (String) valmap.get(field); p.addText(field + ": " + value); if (fields.hasNext()) { p.addElement("br"); } } } else { p.setText(o.toString()); } } } else if (val instanceof Map) { Map m2 = (Map) val; for (Iterator it = m2.keySet().iterator(); it.hasNext();) { String k2 = (String) it.next(); String v2 = (String) m2.get(k2); Element div = valCell.addElement("div"); div.setText(k2 + ": " + v2); } } else if (val instanceof Exception) { Exception ex = (Exception) val; valCell.addElement("p").setText(ex.toString()); StackTraceElement[] elem = ex.getStackTrace(); for (int i = 0; i < elem.length; i++) { valCell.addText(elem[i].toString()); valCell.addElement("br"); } } } return doc.asXML(); }
From source file:mesquite.lib.MesquiteXMLPreferencesModule.java
License:Open Source License
/** * Ideally this should not be static but since we don't always have control over the superclass, * make it available for classes that might otherwise not be able to use it due to inheritance * constraints/* w w w. j a v a 2 s .c o m*/ * @param provider * @param versionInt * @return */ public static String preparePreferencesForXML(PropertyNamesProvider provider, int versionInt) { String[] propertyNames = provider.getPreferencePropertyNames(); Element rootElement = DocumentHelper.createElement("mesquite"); Document preferencesDoc = DocumentHelper.createDocument(rootElement); preferencesDoc.setRootElement(rootElement); Element classElement = DocumentHelper.createElement(getShortClassName(provider.getClass())); rootElement.add(classElement); Element versionElement = DocumentHelper.createElement(VERSION); versionElement.setText("" + versionInt); classElement.add(versionElement); for (int i = 0; i < propertyNames.length; i++) { String currentPropertyName = propertyNames[i]; try { Object prefsContent = PropertyUtils.read(provider, currentPropertyName); if (prefsContent != null) { Element nextPrefsElement = DocumentHelper.createElement(PREFERENCE); nextPrefsElement.addAttribute(KEY, currentPropertyName); nextPrefsElement.add(DocumentHelper.createCDATA(prefsContent.toString())); classElement.add(nextPrefsElement); } } catch (Exception e) { MesquiteMessage.warnProgrammer("Could not read property value " + currentPropertyName + " for writing xml preferences on module: " + provider); } //nextPrefsElement.addContent(new CDATA()) } return XMLUtil.getDocumentAsXMLString(preferencesDoc); }
From source file:org.apache.archiva.repository.metadata.RepositoryMetadataWriter.java
License:Apache License
public static void write(ArchivaRepositoryMetadata metadata, Writer writer) throws RepositoryMetadataException { Document doc = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("metadata"); doc.setRootElement(root); addOptionalElementText(root, "groupId", metadata.getGroupId()); addOptionalElementText(root, "artifactId", metadata.getArtifactId()); addOptionalElementText(root, "version", metadata.getVersion()); if (CollectionUtils.isNotEmpty(metadata.getPlugins())) { Element plugins = root.addElement("plugins"); List<Plugin> pluginList = metadata.getPlugins(); Collections.sort(pluginList, PluginComparator.INSTANCE); for (Plugin plugin : metadata.getPlugins()) { Element p = plugins.addElement("plugin"); p.addElement("prefix").setText(plugin.getPrefix()); p.addElement("artifactId").setText(plugin.getArtifactId()); addOptionalElementText(p, "name", plugin.getName()); }//from ww w. j av a2 s .co m } if (CollectionUtils.isNotEmpty(metadata.getAvailableVersions()) // || StringUtils.isNotBlank(metadata.getReleasedVersion()) // || StringUtils.isNotBlank(metadata.getLatestVersion()) // || StringUtils.isNotBlank(metadata.getLastUpdated()) // || (metadata.getSnapshotVersion() != null)) { Element versioning = root.addElement("versioning"); addOptionalElementText(versioning, "latest", metadata.getLatestVersion()); addOptionalElementText(versioning, "release", metadata.getReleasedVersion()); if (metadata.getSnapshotVersion() != null) { Element snapshot = versioning.addElement("snapshot"); String bnum = String.valueOf(metadata.getSnapshotVersion().getBuildNumber()); addOptionalElementText(snapshot, "buildNumber", bnum); addOptionalElementText(snapshot, "timestamp", metadata.getSnapshotVersion().getTimestamp()); } if (CollectionUtils.isNotEmpty(metadata.getAvailableVersions())) { Element versions = versioning.addElement("versions"); Iterator<String> it = metadata.getAvailableVersions().iterator(); while (it.hasNext()) { String version = it.next(); versions.addElement("version").setText(version); } } addOptionalElementText(versioning, "lastUpdated", metadata.getLastUpdated()); } try { XMLWriter.write(doc, writer); } catch (XMLException e) { throw new RepositoryMetadataException("Unable to write xml contents to writer: " + e.getMessage(), e); } }
From source file:org.apache.taglibs.xtags.xpath.ReplaceTag.java
License:Apache License
public int doEndTag() throws JspException { Object context = TagHelper.getInputNodes(pageContext, this, false); if (context == null) { logInfo("No current node to replace"); return EVAL_PAGE; }//from ww w. j a v a2 s . co m try { String xmlFragment = null; if (bodyContent != null) { xmlFragment = bodyContent.getString(); } if (context instanceof List) { List els = (List) context; if (els.size() > 1) { throw new JspException("Current context contains more than one node"); } if (els.size() == 1) { context = els.get(0); } } if (context instanceof Document) { if (xmlFragment == null) { throw new JspException("Cannot replace document with empty body"); } Document sourceDoc = (Document) context; Document newDoc = DocumentHelper.parseText(xmlFragment); // clear source doc contents sourceDoc.clearContent(); for (int i = 0, size = newDoc.nodeCount(); i < size; i++) { Node node = newDoc.node(i); // detach from new doc node.detach(); // add to source sourceDoc.add(node); } } else { if (!(context instanceof Element)) { throw new JspException("Current node is not an Element: " + context.getClass().getName()); } Element element = (Element) context; SAXReader reader = new SAXReader(); if (element.isRootElement()) { if (xmlFragment == null) { throw new JspException("Cannot replace root element with empty body"); } Document newDoc = DocumentHelper.parseText(xmlFragment); Document sourceDoc = element.getDocument(); Element newRoot = newDoc.getRootElement(); newRoot.detach(); sourceDoc.setRootElement(newRoot); } else { Element parent = element.getParent(); List parentContent = parent.content(); int index = parentContent.indexOf(element); parentContent.remove(index); if (xmlFragment != null) { Document newDoc = DocumentHelper.parseText("<dummy>" + xmlFragment + "</dummy>"); parentContent.addAll(index, newDoc.getRootElement().content()); } } } } catch (DocumentException e) { handleException(e); } return EVAL_PAGE; }
From source file:org.codehaus.cargo.container.weblogic.WebLogic9xStandaloneLocalConfiguration.java
License:Apache License
/** * Create a blank datasource file with correct namespace. * /*from w ww .java 2 s. c o m*/ * @param path where to create the base file. */ protected void createBlankDataSourceFile(String path) { Document document = DocumentHelper.createDocument(); Element dataSource = document.addElement("jdbc-data-source"); document.setRootElement(dataSource); dataSource.addNamespace("", "http://www.bea.com/ns/weblogic/90"); xmlTool.saveXml(document, path); }
From source file:org.fireflow.engine.modules.persistence.hibernate.VariableHeaderType.java
License:Open Source License
public static String map2XmlString(Properties arg0) { if (arg0 == null) return null; Properties map = arg0;/*from w w w . j a va2 s .co m*/ Document document = documentFactory.createDocument(); Element root = documentFactory.createElement("map"); document.setRootElement(root); Iterator<Object> keys = map.keySet().iterator(); while (keys.hasNext()) { String key = (String) keys.next(); String value = (String) map.get(key); Element entry = documentFactory.createElement("entry"); root.add(entry); Element keyElm = documentFactory.createElement("key"); entry.add(keyElm); keyElm.setText(key); Element valueElm = documentFactory.createElement("value"); entry.add(valueElm); valueElm.add(documentFactory.createCDATA(value)); } try { StringWriter writer = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); String jvmEncoding = Charset.defaultCharset().name(); format.setEncoding(jvmEncoding); XMLWriter xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); return writer.getBuffer().toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return document.toString(); }