Advertisement
Guest User

sss

a guest
Apr 19th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package n12;
  2.  
  3. import java.io.*;
  4.  
  5. import javax.xml.parsers.*;
  6.  
  7. import org.xml.sax.*;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10. import javax.xml.transform.Source;
  11. import javax.xml.transform.stream.StreamSource;
  12. import javax.xml.validation.SchemaFactory;
  13.  
  14.  
  15. public class ue61 {
  16.  
  17.     public static void main(String[] args) throws SAXException {
  18.  
  19.         SAXParserFactory factory = SAXParserFactory.newInstance();
  20.         factory.setValidating(false); //not checking a DTD
  21.  
  22.         SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  23.  
  24.         factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource("ue61.xsd")}));
  25.         SAXParser saxParser;
  26.         try {
  27.             saxParser = factory.newSAXParser();
  28.             // parse …
  29.             File file = new File("ue61.xml");
  30.             saxParser.parse(file, new PrintElementsHandler());
  31.         } catch (ParserConfigurationException e1) {
  32.             System.out.println("ParserConfigurationException: "+e1.getMessage());
  33.         } catch (SAXException e1) {
  34.             System.out.println("SAXException: "+e1.getMessage());
  35.         } catch (IOException e) {
  36.             System.out.println("IOException: "+e.getMessage());
  37.         }
  38.     }
  39. }
  40.  
  41. class PrintElementsHandler extends DefaultHandler {
  42.     int biggestNumber = 0;
  43.  
  44.     StringBuffer blanks = new StringBuffer(); // leading blanks
  45.  
  46.     int depth = 0; // depth of node
  47.  
  48.     public void startDocument() throws SAXException {
  49.         System.out.println("start document -----------------------------");
  50.     }
  51.    
  52.     /*  namespaceURI: Namensraum-Bezeichner oder null
  53.     localName: lokaler Elementname ohne Präfix
  54.     qName: Elementname mit Namensraumpräfix (falls vorhanden)
  55.     atts: Menge aller Attribute des Elements (Start-Tags)*/
  56.     public void startElement(String uri, String localName, String qName,
  57.             Attributes attributes) throws SAXException {
  58.         blanks.append("   "); // add 3 blanks
  59.         depth++; // increase depth
  60.         System.out.print(blanks.toString() + depth + " - " + qName + ": ");
  61.         for (int i = 0; i < attributes.getLength(); i++) {
  62.             System.out.print(attributes.getQName(i) + " = "
  63.                     + attributes.getValue(i) + "  ");
  64.         }
  65.         System.out.println();
  66.     }
  67.    
  68.     public void characters(char[] ch, int start, int length)
  69.             throws SAXException {
  70.         String s = new String(ch, start, length);
  71.         System.out.println(blanks.toString() + "   " + (depth + 1) + " - " + s);
  72.         if(s.matches("[0-9]+")) // regex string check if number
  73.         {
  74.             int temp = Integer.parseInt(s);
  75.             if(biggestNumber < temp)
  76.             {
  77.                 biggestNumber = temp;
  78.             }
  79.         }
  80.     }
  81.    
  82.     /*  namespaceURI: Namensraum-Bezeichner oder null
  83.         localName: lokaler Elementname ohne Präfix
  84.         qName: Elementname mit Präfix*/
  85.     public void endElement(String uri, String localName, String qName)
  86.             throws SAXException {
  87.         blanks.delete(0, 3); // delete 3 blanks
  88.         depth--; // decrease depth
  89.     }
  90.    
  91.     public void endDocument() throws SAXException {
  92.         System.out.println("Biggest Number in all value-Elements: "+biggestNumber);
  93.         System.out.println("end document ------------------------------");
  94.     }
  95.    
  96.    
  97.     //
  98.     // ErrorHandler methods
  99.     //
  100.    
  101.     /** Warning. */
  102.     @Override
  103.     public void warning(SAXParseException ex) throws SAXException {
  104.         printError("Warning", ex);
  105.     } // warning(SAXParseException)
  106.    
  107.     /** Error. */
  108.     @Override
  109.     public void error(SAXParseException ex) throws SAXException {
  110.         printError("Error", ex);
  111.     } // error(SAXParseException)
  112.    
  113.     /** Fatal error. */
  114.     @Override
  115.     public void fatalError(SAXParseException ex) throws SAXException {
  116.         printError("Fatal Error", ex);
  117.         // throw ex;
  118.     } // fatalError(SAXParseException)
  119.    
  120.     //
  121.     // Protected methods
  122.     //
  123.    
  124.     /** Prints the error message. */
  125.     protected void printError(String type, SAXParseException ex) {
  126.    
  127.         System.err.print("[");
  128.         System.err.print(type);
  129.         System.err.print("] ");
  130.         if (ex == null) {
  131.             System.err.println("!!!");
  132.         }
  133.         String systemId = ex.getSystemId();
  134.         if (systemId != null) {
  135.             int index = systemId.lastIndexOf('/');
  136.             if (index != -1)
  137.                 systemId = systemId.substring(index + 1);
  138.             System.err.print(systemId);
  139.         }
  140.         System.err.print(':');
  141.         System.err.print(ex.getLineNumber());
  142.         System.err.print(':');
  143.         System.err.print(ex.getColumnNumber());
  144.         System.err.print(": ");
  145.         System.err.print(ex.getMessage());
  146.         System.err.println();
  147.         System.err.flush();
  148.    
  149.     } // printError(String,SAXParseException)
  150.    
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement