HttpWriter.java
5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.focasoftware.deboinventario;
import android.os.Environment;
import org.w3c.dom.Document;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
* Clase para convertir un documento en forma de DOM a un archivo XML para su posterior
* uso o envo
* @author GuillermoR
*
*/
public class HttpWriter{
/**
* Convierte un documento DOM en un archivo XML a la direccion y nombre
* indicados en el url
* <p>1 Creacion del fuente del DOM
* <p>2 Creacion del fichero de salida
* <p>3 Configuracion del transformador de documento
* <p>4 Realizamos la transformacion
*
*/
public static void transformerXml(Document document, String urlArchivo){
try {
//1 Creacion del fuente del DOM
Source source = new DOMSource(document);
//2 Creacion del fichero de salida
//urlArchivo="/mnt/sdcard/data/data/com.foca.deboInventario";
File archivo = new File(urlArchivo);
File parent = new File(archivo.getParent());
if (!parent.exists()) {
archivo.mkdirs();
}
//Result resultat = new StreamResult(archivo);
//3 Configuracion del transformador de documento
TransformerFactory fabricaTransformacion = TransformerFactory.newInstance();
Transformer transformador = fabricaTransformacion.newTransformer();
transformador.setOutputProperty(OutputKeys.INDENT, "yes");
transformador.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
StreamResult resultat = new StreamResult(new File(parent, "/deboInventarioExport.xml").getPath());
//4 Realizamos la transformacion
transformador.transform(source, resultat);
}catch(Exception e){
e.printStackTrace();
}
}
// public static void transformerXml(Document document, String urlArchivo) {
// try {
// //1 Creacion del fuente del DOM
// Source source = new DOMSource(document);
//
// //2 Creacion del fichero de salida
// File archivo = new File(urlArchivo);
// File parent = new File(archivo.getParent());
// if (!parent.exists()) {
// archivo.mkdirs();
// }
//
//
// //3 Configuracion del transformador de documento
// TransformerFactory fabricaTransformacion = TransformerFactory.newInstance();
// Transformer transformador = fabricaTransformacion.newTransformer();
// transformador.setOutputProperty(OutputKeys.INDENT, "yes");
// transformador.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
// StreamResult resultat = new StreamResult(new File(parent+"/", "deboInventarioExport.xml").getPath());
// //4 Realizamos la transformacion
// transformador.transform(source, resultat);
//
// }catch(Exception e){
// e.printStackTrace();
// }
// }
/*
@Override
public void onCreate(Bundle savedInstanceState)
{
// Por fin creamos la pgina:
super.onCreate(savedInstanceState);
try{
hash1.put("ID_MED","1");
hash1.put("PER","2011/07");
hash1.put("LEAN", "1000");
hash1.put("LEAC","2000");
hash1.put("VAL","123456");
hash1.put("FECHA_TOMA", "2011/08/15 11:30:25");
hash1.put("ID_ERROR","0");
hash1.put("OBSERVACION","");
hash1.put("ID_OPE", "1");
listaTodasMediciones.put("1",hash1);
//listaTodasMediciones.put("2",hash2);
//listaTodasMediciones.put("3",hash3);
// Cration d'un nouveau DOM
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
DocumentBuilder constructeur = fabrique.newDocumentBuilder();
Document document = constructeur.newDocument();
// Proprits du DOM
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
// Cration de l'arborescence du DOM
//Element raiz = document.createElement("WEBs");
Element titulo = document.createElement("VI_AGUA_FROM_TABLET");
//raiz.appendChild(titulo);
Enumeration<String> enumeration = listaTodasMediciones.keys();
while (enumeration.hasMoreElements() == true) {
String key = enumeration.nextElement();
Hashtable<String,String> hash = listaTodasMediciones.get(key);
Element medicion = document.createElement("MEDICION");
Enumeration<String> e = hash.keys();
while (e.hasMoreElements() == true) {
String key2 = e.nextElement();
String value2 = hash.get(key2);
Element elemento = document.createElement(key2);
elemento.setTextContent(value2);
medicion.appendChild(elemento);
}
titulo.appendChild(medicion);
}
//raiz.appendChild(titulo);
document.appendChild(titulo); //raiz);
//Sauvegarde du DOM dans un fichier XML
transformerXml(document, ParametrosAgua.URL_DOCUMENTO_XML);
HttpSender sender = new HttpSender(Parametros.CODIGO_SOFT_DEBOAGUA);
sender.send_xml(ParametrosAgua.URL_DOCUMENTO_XML);
}catch(Exception e){
e.printStackTrace();
}
}
*/
}