F@lkland§
28-06-2008, 13:55
Ciao ragazzi!
Allora mi serve una classe che mi legga un file XML, ho scritto questo ma ho un errore nel main, perņ non riesco a capire cosa c'č che non va, posto il codice.
Spero riusciate ad aiutarmi.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
public class LeggereXml extends DefaultHandler
{
static public String file="foo.xml";
static public String nodo;
public static void main(String args[])
{
DefaultHandler handler = new LeggereXml();
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
out = new OutputStreamWriter(System.out, "UTF8");
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(file, handler);
}
catch (Throwable t) { t.printStackTrace(); }
}
static private Writer out;
public void startElement(String namespaceURI,String lName,String
qName,Attributes attrs)throws SAXException{
String name = lName;
if ("".equals(name)) {nodo=qName;}
}
public void endElement(String namespaceURI,String sName,String qName) throws
SAXException{
String name = sName;
if ("".equals(name)) {nodo=qName;}
}
public void characters(char buf[], int offset, int len) throws SAXException{
String s = new String(buf, offset, len);
if(s.length()>1){
if(nodo=="autore") System.out.println(nodo+"-> "+s);
if(nodo=="titolo") System.out.println(nodo+"-> "+s);
if(nodo=="costo") System.out.println(nodo+"-> "+s);
}
}
}
Allora mi serve una classe che mi legga un file XML, ho scritto questo ma ho un errore nel main, perņ non riesco a capire cosa c'č che non va, posto il codice.
Spero riusciate ad aiutarmi.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
public class LeggereXml extends DefaultHandler
{
static public String file="foo.xml";
static public String nodo;
public static void main(String args[])
{
DefaultHandler handler = new LeggereXml();
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
out = new OutputStreamWriter(System.out, "UTF8");
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(file, handler);
}
catch (Throwable t) { t.printStackTrace(); }
}
static private Writer out;
public void startElement(String namespaceURI,String lName,String
qName,Attributes attrs)throws SAXException{
String name = lName;
if ("".equals(name)) {nodo=qName;}
}
public void endElement(String namespaceURI,String sName,String qName) throws
SAXException{
String name = sName;
if ("".equals(name)) {nodo=qName;}
}
public void characters(char buf[], int offset, int len) throws SAXException{
String s = new String(buf, offset, len);
if(s.length()>1){
if(nodo=="autore") System.out.println(nodo+"-> "+s);
if(nodo=="titolo") System.out.println(nodo+"-> "+s);
if(nodo=="costo") System.out.println(nodo+"-> "+s);
}
}
}