Categorie
Winzipedia Uso dell'wiki |
Java /
JDOM-EsempioJava.JDOM-Esempio VersioniMostra le modifiche minori - Mostra le modifiche 27/06/2006 ore 00:27 CEST
di - Classe che permette di leggere e scrivere file XML usando hashtable
Aggiunte le linee 1-232:
!Titolo dell'articolo '''Autori:''' Abaddon\\ '''Hanno contribuito:''' ->'''Sommario''' !!Sezione 1 - File XML E' composto da 3 blocchi principali, identificati dal tag "oggetto", che si chiamano database, amministrazione, stampa. Questi blocchi contengono le informazioni che servono per il corretto funzionameto dei 3 oggetti all'interno del programma e sono delimitate dal tag "var".\\ <?xml version="1.0" encoding="UTF-8"?> <!-- Document : config.xml Created on : 29 marzo 2006, 16.53 Author : abaddon Description: Purpose of the document follows. --> <!DOCTYPE config SYSTEM "config.dtd"> <config> <oggetto nome="database"> <var tipo="database">postgresql</var> <var tipo="url">127.0.0.1</var> <var tipo="nomedb">iper2</var> <var tipo="utente">postgres</var> <var tipo="password"></var> </oggetto> <oggetto nome="amministratore"> <var tipo="password">adminPASS</var> </oggetto> <oggetto nome="stampa"> <var tipo="destPdfBolla">./bolla</var> <var tipo="destPdfCentro">./centro</var> <var tipo="destPdfCliente">./cliente</var> <var tipo="destPdfDDT">./ddt</var> <var tipo="destPdfElencoBolle">./elenco/bolle</var> <var tipo="destPdfelencoClienti">./elenco/bolle</var> <var tipo="destPdfElencoCentri">./elenco/centro</var> </oggetto> </config> !!Sezione 2 - Classe java Codice sorgente: /* * Parser.java * * Created on 26 giugno 2006, 23.11 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ import java.io.File;\\ import java.io.FileWriter;\\ import java.io.IOException;\\ import java.io.Writer;\\ import java.util.Hashtable;\\ import java.util.Iterator;\\ import java.util.List;\\ import org.jdom.Document;\\ import org.jdom.Element;\\ import org.jdom.JDOMException;\\ import org.jdom.input.SAXBuilder;\\ import org.jdom.output.XMLOutputter;\\ import org.jdom.transform.JDOMSource;\\ /** * * @author abaddon */ public class Parser { private static Parser istanza=null; private Element root=null; private Document doc=null; private SAXBuilder builder=null; private Hashtable<String,String> databaseConfig=null; private Hashtable<String,String> amministrazioneConfig=null; private Hashtable<String,String> stampaConfig=null; /** Creates a new instance of Parser */ private Parser() { databaseConfig=new Hashtable<String,String>(); amministrazioneConfig=new Hashtable<String,String>(); stampaConfig=new Hashtable<String,String>(); readFileXml(); listChildren(root,"root"); } public static Parser getIstanza(){ if(istanza==null) istanza=new Parser(); return istanza; } private void readFileXml(){ try { builder = new SAXBuilder(); doc = builder.build("./config.xml"); root = doc.getRootElement(); } catch (IOException ex) { Log.getIstanza().sendException(ex.getMessage()); } catch (JDOMException ex) { Log.getIstanza().sendException(ex.getMessage()); } } public void listChildren(Element current, String nodo) { if(nodo.equals("database")){ setDatabaseHash(current); }else if(nodo.equals("amministratore")){ setAmministratoreHash(current); }else if(nodo.equals("stampa")){ setStampaHash(current); } List children = current.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { Element child = (Element) iterator.next(); if(current.getAttributeValue("nome")!=null) listChildren(child, current.getAttributeValue("nome")); else listChildren(child, "root"); } } private void setDatabaseHash(Element current) { databaseConfig.put(current.getAttributeValue("tipo"),current.getValue()); } private void setStampaHash(Element current) { stampaConfig.put(current.getAttributeValue("tipo"),current.getValue()); } private void setAmministratoreHash(Element current) { amministrazioneConfig.put(current.getAttributeValue("tipo"),current.getValue()); } public Hashtable<String,String> getDatabaseConfig(){ return databaseConfig; } public Hashtable<String,String> getAmministrazioneConfig(){ return amministrazioneConfig; } public Hashtable<String,String> getStampaConfig(){ return stampaConfig; } public void updateXml(){ writeFileXml(root,"root"); XMLOutputter serializer = new XMLOutputter(); try { Writer wout=new FileWriter(new File("./config.xml")); serializer.output(doc, wout); } catch (IOException ex) { ex.printStackTrace(); } } private void writeFileXml(Element current, String nodo) { if(nodo.equals("database")){ setDatabaseXml(current); }else if(nodo.equals("amministratore")){ setAmministratoreXml(current); }else if(nodo.equals("stampa")){ setStampaXml(current); } List children = current.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { Element child = (Element) iterator.next(); if(current.getAttributeValue("nome")!=null) writeFileXml(child, current.getAttributeValue("nome")); else writeFileXml(child, "root"); } } private void setDatabaseXml(Element current) { System.out.println("UPDATE "+current.getAttributeValue("tipo")); current.setText(databaseConfig.get(current.getAttributeValue("tipo"))); } private void setAmministratoreXml(Element current) { current.setText(amministrazioneConfig.get(current.getAttributeValue("tipo"))); } private void setStampaXml(Element current) { current.setText(stampaConfig.get(current.getAttributeValue("tipo"))); } } !!Sezione 3 - Esempi - Lettura di un parametro:\\ Parser.getIstanza.getDatabaseConfig().get("database") // ritorna "postresql" - Modifica di un parametro solo in memoria:\\ Parser.getIstanza().getDatabaseConfig().put("database","mysql"); - Rilettura del parametro:\\ Parser.getIstanza.getDatabaseConfig().get("database") // ritorna "mysql" - Scrittura delle modifiche anche sul file XML\\ Parser.getIstanza().updateXml(); !!Note |