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
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