List of usage examples for org.jdom2 Document setContent
public Document setContent(Content child)
From source file:de.sixtyfourktec.mirrorhub.modules.NationalRailModule.java
License:Open Source License
private NationalRailModule() { /* Create the soap envelope document */ Document doc = new Document(); Namespace soap = Namespace.getNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); Namespace ldb = Namespace.getNamespace("ldb", "http://thalesgroup.com/RTTI/2014-02-20/ldb/"); Namespace typ = Namespace.getNamespace("typ", "http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes"); Element env = new Element("Envelope", soap); /* Header *///from w ww.j a va2s. co m env.addContent(new Element("Header", soap).addContent(new Element("AccessToken", typ) .addContent(new Element("TokenValue", typ).addContent(BuildConfig.NATIONALRAIL_API_KEY)))); /* Body */ Element dep_request = new Element("GetDepartureBoardRequest", ldb) .addContent(new Element("crs", ldb).addContent(BuildConfig.NATIONALRAIL_CRS)) .addContent(new Element("numRows", ldb).addContent("5")); if (!BuildConfig.NATIONALRAIL_FILTER_CRS.isEmpty()) { dep_request.addContent(new Element("filterCrs", ldb).addContent(BuildConfig.NATIONALRAIL_FILTER_CRS)) .addContent(new Element("filterType", ldb).addContent(BuildConfig.NATIONALRAIL_FILTER_TYPE)); } env.addContent(new Element("Body", soap).addContent(dep_request)); doc.setContent(env); String xml = new XMLOutputter(Format.getPrettyFormat()).outputString(doc); if (showXml) { Log.i(TAG, xml); } /* Create a static request object we can reuse */ RequestBody body = RequestBody.create(MediaType.parse("text/xml; charset=utf-8"), xml); request = new Request.Builder().url("https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb6.asmx") .post(body).build(); }
From source file:interfacermi.Traza.java
public void insertarTraza(String hora, String actor, String accion) { Document document = null; Element root = null;/* ww w. ja v a 2 s. c om*/ File xmlFile = new File("Traza.xml"); //Se comprueba si el archivo XML ya existe o no. if (xmlFile.exists()) { FileInputStream fis; try { fis = new FileInputStream(xmlFile); SAXBuilder sb = new SAXBuilder(); document = sb.build(fis); //Si existe se obtiene su nodo raiz. root = document.getRootElement(); fis.close(); } catch (JDOMException | IOException e) { e.printStackTrace(); } } else { //Si no existe se crea su nodo raz. document = new Document(); root = new Element("Traza"); } //Se crea un nodo Hecho para insertar la informacin. Element nodohecho = new Element("Hecho"); //Se crea un nodo hora para insertar la informacin correspondiente a la hora. Element nodohora = new Element("Hora"); //Se crea un nodo actor para insertar la informacin correspondiente al actor. Element nodoactor = new Element("Actor"); //Se crea un nodo accion para insertar la informacin correspondiente a la accin. Element nodoaccion = new Element("Accion"); //Se asignan los valores enviados para cada nodo. nodohora.setText(hora); nodoactor.setText(actor); nodoaccion.setText(accion); //Se aade el contenido al nodo Hecho. nodohecho.addContent(nodohora); nodohecho.addContent(nodoactor); nodohecho.addContent(nodoaccion); //Se aade el nodo Hecho al nodo raz. root.addContent(nodohecho); document.setContent(root); //Se procede a exportar el nuevo o actualizado archivo de traza XML XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); try { xmlOutput.output(document, new FileWriter("Traza.xml")); } catch (IOException e) { e.printStackTrace(); } }
From source file:it.isislab.floasys.core.security.users.UserAccountXMLParser.java
License:Open Source License
/** * Write another account to the XML file. * @param is/*from w w w. ja va2s. co m*/ * @param os * @param account * @throws CannotParseException */ public void write(InputStream is, OutputStream os, UserAccount account) throws CannotParseException { try { //Read the existing file. Document doc = new SAXBuilder().build(is); Element rootNode = doc.getRootElement(); //Create a new user element. Element elAccount = new Element(EL_USERACCOUNT); Element elUsername = new Element(EL_USERNAME); Element elPassword = new Element(EL_PASSWORD); Element elActivationCode = new Element(EL_ACTIVATIONCODE); Element elActivated = new Element(EL_ACTIVATED); elAccount.addContent(elUsername); elAccount.addContent(elPassword); elAccount.addContent(elActivationCode); elAccount.addContent(elActivated); //Add the account to the root element. rootNode.addContent(elAccount); //Set the root for document. doc.setContent(rootNode); //Write the file content. XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); outputter.output(doc, os); } catch (Exception ex) { throw new CannotParseException(ex); } }
From source file:org.mycore.common.content.transformer.MCRXSL2XMLTransformer.java
License:Open Source License
private Document getDocument(JDOMResult result) { Document resultDoc = result.getDocument(); if (resultDoc == null) { //Sometimes a transformation produces whitespace strings //JDOM would produce a empty document if it detects those //So we remove them, if they exists. List<Content> transformResult = result.getResult(); int origSize = transformResult.size(); Iterator<Content> iterator = transformResult.iterator(); while (iterator.hasNext()) { Content content = iterator.next(); if (content instanceof Text) { String trimmedText = ((Text) content).getTextTrim(); if (trimmedText.length() == 0) { iterator.remove();/*from w w w . j a v a 2s. com*/ } } } if (transformResult.size() < origSize) { JDOMFactory f = result.getFactory(); if (f == null) { f = new DefaultJDOMFactory(); } resultDoc = f.document(null); resultDoc.setContent(transformResult); } } return resultDoc; }