1 | package es.ciudadescolar.cveSAX; |
2 | |
3 | import java.io.File; |
4 | |
5 | import java.io.FileWriter; |
6 | import java.io.IOException; |
7 | import java.io.PrintWriter; |
8 | import java.util.List; |
9 | |
10 | import javax.xml.XMLConstants; |
11 | import javax.xml.parsers.ParserConfigurationException; |
12 | import javax.xml.parsers.SAXParser; |
13 | import javax.xml.parsers.SAXParserFactory; |
14 | import javax.xml.validation.Schema; |
15 | import javax.xml.validation.SchemaFactory; |
16 | |
17 | import org.slf4j.Logger; |
18 | import org.slf4j.LoggerFactory; |
19 | import org.xml.sax.SAXException; |
20 | |
21 | public class Principal { |
22 | |
23 | private static final Logger LOGGER = LoggerFactory.getLogger(Principal.class); |
24 | |
25 | public static void main(String[] args) { |
26 | |
27 | File xsdFile = new File("cve_1.0.xsd"); |
28 | File xmlFile = new File("cve_list_20231022.xml"); |
29 | LOGGER.debug(("El fichero xml a parsear es: "+xmlFile)); |
30 | LOGGER.debug(("El fichero xsd contra el que validar el xml es: "+xsdFile)); |
31 | |
32 | SAXParserFactory saxParserFactory =SAXParserFactory.newInstance(); |
33 | LOGGER.debug(("Nueva instanciar del SAXParserFactory")); |
34 | |
35 | //opcion validadr schema |
36 | |
37 | SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
38 | |
39 | Schema schema; |
40 | try { |
41 | schema = schemaFactory.newSchema(xsdFile); |
42 | saxParserFactory.setSchema(schema); //Debe validar el xml contra el esquam (xsd) proporcionado |
43 | LOGGER.debug(("AƱadido schema al SAXParserFactory")); |
44 | saxParserFactory.setNamespaceAware(true); //Mantener namespaces |
45 | |
46 | SAXParser saxParser = saxParserFactory.newSAXParser(); |
47 | LOGGER.debug(("Nueva instancia del SAXParserFactory")); |
48 | |
49 | |
50 | ItemDefaultHandler defaultHandler = new ItemDefaultHandler(); |
51 | LOGGER.debug(("Nueva instancia del defaultHandler con el que parsear el XML")); |
52 | |
53 | saxParser.parse(xmlFile,defaultHandler);//DOnde esta elficheroXml y como analizarlo |
54 | LOGGER.debug(("Fichero XML parseado")); |
55 | |
56 | List<Item>items=defaultHandler.getItems(); |
57 | LOGGER.debug(("Se han recuperado["+items.size()+"] items del xml")); |
58 | |
59 | if(!writeItemsToFile(items,new File("cve_list.txt"))) { |
60 | LOGGER.error("Error detectado durante la conversion a txt de la coleccion"); |
61 | } |
62 | |
63 | }catch(SAXException e) { |
64 | e.printStackTrace(); |
65 | } catch (ParserConfigurationException e) { |
66 | // TODO Auto-generated catch block |
67 | e.printStackTrace(); |
68 | } catch (IOException e) { |
69 | // TODO Auto-generated catch block |
70 | e.printStackTrace(); |
71 | } |
72 | |
73 | |
74 | } |
75 | private static boolean writeItemsToFile (List<Item> listaItems, File ficheroSalida) |
76 | { |
77 | FileWriter ficheroEscribible = null; |
78 | PrintWriter pt = null; |
79 | try |
80 | { |
81 | ficheroEscribible = new FileWriter(ficheroSalida); |
82 | pt = new PrintWriter(ficheroEscribible); |
83 | |
84 | for (Item itemCve:listaItems) |
85 | pt.println(itemCve); |
86 | } |
87 | catch (IOException e) { return false; } |
88 | finally |
89 | { |
90 | if (pt != null) |
91 | { |
92 | pt.flush(); |
93 | pt.close(); |
94 | } |
95 | } |
96 | return true; |
97 | } |
98 | |
99 | |
100 | } |