PDA

View Full Version : xml sax


texerasmo
08-04-2004, 13:40
Sono in crisi...
Qualcuno di voi sa dirmi come posso lanciare una query su un xml utilizzando il sax?


public class CountTag extends DefaultHandler {

public static void main(String[] arguments) {
if (arguments.length > 1) {
try {
CountTag ct = new CountTag(arguments[0], arguments[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("errore nel file:" + e.getMessage());
}
} else {
System.out.println("Attenzione arguments non trovato");
}

}

/**
* @param xmlFile
* @param tagName
*/
public CountTag(String xmlFile, String tagName) throws IOException {
File input = new File(xmlFile);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
try {
SAXParser sax = factory.newSAXParser();

CountTagHandler cth = new CountTagHandler(tagName);
sax.parse(input, cth);
System.out.println(
"IL tag richiesto: "
+ cth.tag
+ " è stato trovato n: "
+ cth.count
+ " volte ");
//System.out.println("to string"+cth.hashCode());

} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("errore:" + e1.getMessage());

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("errore:" + e.getMessage());
}

// TODO Auto-generated constructor stub
}
public class CountTagHandler extends DefaultHandler {
String tag;
int count = 0;
int count1 = 0;
CountTagHandler(String tagName) {
super();
tag = tagName;
}
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) {
if (qName.equals(tag))
count++;

}

/* public void characters(char[] text, int first, int length) {
String data = new String(text, first, length);

if (data.equals("ciao")) {
System.out.println("nvolte ciao"+ count2++);

}
}
*/
public void startDocument() {

System.out.println("IL tag iniziales");
}
public void setDocumentLocator(Locator loc){
System.out.println("url del file xml: " + loc.getSystemId());

}

}

}

A questa classe gli passo due par uno il nome del file xml e l'altro il tag da cercare.
Ora io volevo fare un query sul contenuto del tag..
Sapete dirmi come posso fare?

Grazie

texerasmo
08-04-2004, 17:26
per chi deve fare una cosa simile..

public class CountTag extends DefaultHandler {
int count3 = 0;
boolean boolPadre;
boolean boolFiglio1;
boolean boolFiglio2;
boolean boolFiglio3;
long inizio, fine;
public static void main(String[] arguments) {
if (arguments.length > 1) {
try {
CountTag ct = new CountTag(arguments[0], arguments[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("errore nel file:" + e.getMessage());
}
} else {
System.out.println("Attenzione arguments non trovato");
}

}

/**
* @param xmlFile
* @param tagName
*/
public CountTag(String xmlFile, String tagName) throws IOException {
File input = new File(xmlFile);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
try {
SAXParser sax = factory.newSAXParser();

CountTagHandler cth = new CountTagHandler(tagName);
sax.parse(input, cth);
System.out.println(
"IL tag richiesto: "
+ cth.tag
+ " è stato trovato n: "
+ cth.count
+ " volte ");
System.out.println("risultato query: " + count3);
//System.out.println("to string"+cth.hashCode());

} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("errore:" + e1.getMessage());

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("errore:" + e.getMessage());
}

// TODO Auto-generated constructor stub
}
public class CountTagHandler extends DefaultHandler {
String tag;
int count = 0;
int count1 = 0;
CountTagHandler(String tagName) {
super();
tag = tagName;
}
public void startElement(
String uri,
String localName,
String qName,
Attributes attributes) {
String testo = null;

if (qName.equals(tag))
{
boolPadre = true;
count++;
}
else if (qName.equals("figlio1")) {

boolFiglio1 = true;
} else if (qName.equals("figlio2")) {

boolFiglio2 = true;
} else if (qName.equals("figlio3")) {

boolFiglio3 = true;
}
// System.out.println("qName: " + qName);
// System.out.println("Padre: " + boolPadre);
// System.out.println("Filgio1 :" + boolFiglio1);
// System.out.println("Filgio2 :" + boolFiglio2);
// System.out.println("Filgio3 :" + boolFiglio3);

//testo = attributes.getValue(0);
// if (testo!=null && testo.equals("pippone")) {
// count3++;
// System.out.println("Valore teso" + testo);
// }

}

public void endElement(String name,String name2,String name3) {

//System.out.println("tag in end element: " + name3);

if (name3.equals(tag)) {
if (boolPadre)
count3++;
boolPadre = false;
} else if (name3.equals("figlio1")) {
boolFiglio1 = false;
} else if (name3.equals("figlio2")) {
boolFiglio2 = false;
} else if (name3.equals("figlio3")) {
boolFiglio3 = false;
}

}


public void characters(char[] text, int first, int length) {
String data = new String(text, first, length);

/*if (bool && (data.startsWith("ciao ") || data.endsWith(" ciao") || data.equalsIgnoreCase("ciao"))) {
count3++;
}
*/
if (boolPadre) {

if (boolFiglio1) {

if (!data.equals("MINOnontifidi"))
boolPadre = false;
} else if (boolFiglio2) {

if (!data.equals("ERASMO"))
boolPadre = false;
} else if (boolFiglio3) {

if (!data.equals("RAFF"))
boolPadre = false;
//else count3++;
}

}
// System.out.println("dato: " + data + " boolPadre: " + boolPadre);

}

public void startDocument() {

System.out.println("IL tag iniziales");
inizio = System.currentTimeMillis();

}

public void endDocument() {

fine = System.currentTimeMillis();
System.out.println("Tempo Attesa risposta in milli secondi: " + (fine - inizio));
}

public void setDocumentLocator(Locator loc) {
System.out.println("url del file xml: " + loc.getSystemId());

}

}

}
Questa classe mi fa le seguenti cose:
a) Stampa il tempo di esecuzione
b) Stampa il numero delle volete che trova il tag passato da paarmetro
c) Stampa il numero delle volte dove egue la seguente query:

avendo un xml fatto in questo modo
<root>
<padre>
<figlio1>
<figlio2>
<figlio3>


Lanciano la seguente query
dove avendo un tag padre e il filgio1 = MINO e figlio2=ERASMO e figlio3=RAFF
restituisce il numero della query corrispondente.

ciao a tutti