Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package n12;
- import java.io.*;
- import javax.xml.parsers.*;
- import org.xml.sax.*;
- import org.xml.sax.helpers.DefaultHandler;
- import javax.xml.transform.Source;
- import javax.xml.transform.stream.StreamSource;
- import javax.xml.validation.SchemaFactory;
- public class ue61 {
- public static void main(String[] args) throws SAXException {
- SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setValidating(false); //not checking a DTD
- SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
- factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource("ue61.xsd")}));
- SAXParser saxParser;
- try {
- saxParser = factory.newSAXParser();
- // parse …
- File file = new File("ue61.xml");
- saxParser.parse(file, new PrintElementsHandler());
- } catch (ParserConfigurationException e1) {
- System.out.println("ParserConfigurationException: "+e1.getMessage());
- } catch (SAXException e1) {
- System.out.println("SAXException: "+e1.getMessage());
- } catch (IOException e) {
- System.out.println("IOException: "+e.getMessage());
- }
- }
- }
- class PrintElementsHandler extends DefaultHandler {
- int biggestNumber = 0;
- StringBuffer blanks = new StringBuffer(); // leading blanks
- int depth = 0; // depth of node
- public void startDocument() throws SAXException {
- System.out.println("start document -----------------------------");
- }
- /* namespaceURI: Namensraum-Bezeichner oder null
- localName: lokaler Elementname ohne Präfix
- qName: Elementname mit Namensraumpräfix (falls vorhanden)
- atts: Menge aller Attribute des Elements (Start-Tags)*/
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- blanks.append(" "); // add 3 blanks
- depth++; // increase depth
- System.out.print(blanks.toString() + depth + " - " + qName + ": ");
- for (int i = 0; i < attributes.getLength(); i++) {
- System.out.print(attributes.getQName(i) + " = "
- + attributes.getValue(i) + " ");
- }
- System.out.println();
- }
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- String s = new String(ch, start, length);
- System.out.println(blanks.toString() + " " + (depth + 1) + " - " + s);
- if(s.matches("[0-9]+")) // regex string check if number
- {
- int temp = Integer.parseInt(s);
- if(biggestNumber < temp)
- {
- biggestNumber = temp;
- }
- }
- }
- /* namespaceURI: Namensraum-Bezeichner oder null
- localName: lokaler Elementname ohne Präfix
- qName: Elementname mit Präfix*/
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- blanks.delete(0, 3); // delete 3 blanks
- depth--; // decrease depth
- }
- public void endDocument() throws SAXException {
- System.out.println("Biggest Number in all value-Elements: "+biggestNumber);
- System.out.println("end document ------------------------------");
- }
- //
- // ErrorHandler methods
- //
- /** Warning. */
- @Override
- public void warning(SAXParseException ex) throws SAXException {
- printError("Warning", ex);
- } // warning(SAXParseException)
- /** Error. */
- @Override
- public void error(SAXParseException ex) throws SAXException {
- printError("Error", ex);
- } // error(SAXParseException)
- /** Fatal error. */
- @Override
- public void fatalError(SAXParseException ex) throws SAXException {
- printError("Fatal Error", ex);
- // throw ex;
- } // fatalError(SAXParseException)
- //
- // Protected methods
- //
- /** Prints the error message. */
- protected void printError(String type, SAXParseException ex) {
- System.err.print("[");
- System.err.print(type);
- System.err.print("] ");
- if (ex == null) {
- System.err.println("!!!");
- }
- String systemId = ex.getSystemId();
- if (systemId != null) {
- int index = systemId.lastIndexOf('/');
- if (index != -1)
- systemId = systemId.substring(index + 1);
- System.err.print(systemId);
- }
- System.err.print(':');
- System.err.print(ex.getLineNumber());
- System.err.print(':');
- System.err.print(ex.getColumnNumber());
- System.err.print(": ");
- System.err.print(ex.getMessage());
- System.err.println();
- System.err.flush();
- } // printError(String,SAXParseException)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement