Ad esempio questo che ho scritto io:Originariamente inviato da vespaslight125
Cerco un esmpio di parser Dom...se hai un link a cui riferirsi ti ringrazio...altrimenti vedo di arrangiarmi, almeno per ora...![]()
Se hai un documento come questo:codice:import java.lang.*; import java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class XmlSaxTester extends DefaultHandler { private File file; private int level; public XmlSaxTester (String filename) { file = new File (filename); } public void load () { try { SAXParserFactory factory = SAXParserFactory.newInstance (); SAXParser saxParser = factory.newSAXParser (); level = 0; saxParser.parse (file, this); } catch (SAXException sxe) { Exception x = sxe; if (sxe.getException () != null) x = sxe.getException (); x.printStackTrace (); } catch (ParserConfigurationException pce) { pce.printStackTrace (); } catch (IOException ioe) { ioe.printStackTrace (); } } public void startDocument () throws SAXException { System.out.println ("---- Start of document ----"); } public void endDocument () throws SAXException { System.out.println ("---- End of document ----"); } public void startElement (String namespaceURI, String lName, String qName, Attributes attrs) throws SAXException { printIndentation (); System.out.println ("Start of element \"" + qName + "\""); for (int a = 0; a < attrs.getLength (); a++) { String attrName = attrs.getQName (a); String attrValue = attrs.getValue (a); /* Print attribute name/value */ printIndentation (); System.out.println ("Attribute \"" + attrName + "\"=\"" + attrValue + "\""); } level++; } public void endElement (String namespaceURI, String lName, String qName) throws SAXException { level--; printIndentation (); System.out.println ("End of element \"" + qName + "\""); } public void characters (char[] buf, int offset, int len) throws SAXException { boolean quote = false; printIndentation (); System.out.print ("Characters "); while (len-- > 0) { if (Character.isWhitespace (buf[offset])) { if (quote) { quote = false; System.out.print ("\" "); } System.out.print ("0x" + Integer.toString (buf[offset], 16) + " "); } else { if (!quote) { quote = true; System.out.print ("\""); } System.out.print (buf[offset]); } offset++; } if (quote) System.out.print ("\""); System.out.println (); } private void printIndentation () { for (int l = 0; l < level; l++) System.out.print (" "); } public static void main (String[] args) { if (args.length == 1) { XmlSaxTester tester = new XmlSaxTester (args[0]); tester.load (); } else { System.out.println ("Usage: XmlSaxTester <xml file>"); } } }
ti stampa:codice:<?xml version="1.0"?> <example> <title lang="en"> Hello world! </title> </example>
È sicuramente utile per capire cosa succede e cosa fornisce il parser SAX.codice:---- Start of document ---- Start of element "example" Characters 0xa 0x20 0x20 Start of element "title" Attribute "lang"="en" Characters 0xa 0x20 0x20 0x20 0x20 "Hello" 0x20 "world!" 0xa 0x20 0x20 End of element "title" Characters 0xa End of element "example" ---- End of document ----

Rispondi quotando