ITeye博客

使用SAX验证XML格式--DTD验证

wenhai_zhang · 2010-01-05 23:57 · xml · 编程语言 · XML · F# · Android · IDEA · Markdown

刚写了一个DOM验证XML格式的,现在来一个SAX验证。并附上两个源码

package com;

import java.io.File; import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler;

class SAXValidator { public static void main(String[] args) { try { String fileName = "conf/invalid_dtd.xml"; File x = new File(fileName); SAXParserFactory f = SAXParserFactory.newInstance(); f.setValidating(true); // Default is false SAXParser p = f.newSAXParser(); DefaultHandler h = new MyErrorHandler(); p.parse(x, h); } catch (ParserConfigurationException e) { System.out.println(e.toString()); } catch (SAXException e) { System.out.println(e.toString()); } catch (IOException e) { System.out.println(e.toString()); } }

private static class MyErrorHandler extends DefaultHandler {
	public void warning(SAXParseException e) throws SAXException {
		System.out.println("Warning: ");
		printInfo(e);
	}

	public void error(SAXParseException e) throws SAXException {
		System.out.println("Error: ");
		printInfo(e);
	}

	public void fatalError(SAXParseException e) throws SAXException {
		System.out.println("Fattal error: ");
		printInfo(e);
	}

	private void printInfo(SAXParseException e) {
		System.out.println("   Public ID: " + e.getPublicId());
		System.out.println("   System ID: " + e.getSystemId());
		System.out.println("   Line number: " + e.getLineNumber());
		System.out.println("   Column number: " + e.getColumnNumber());
		System.out.println("   Message: " + e.getMessage());
	}
}

}

评论(4)

  1. #4 楼 · wenhai_zhang · 2011-03-01 00:01

    wenhai_zhang writes:

    [quote=zwwspace]不错,正好有个需求,参考参考 不过我的是android上的[/quote] 这个很久写过一个示例,后面就没搞了。呵呵

    ---指Android

  2. #3 楼 · wenhai_zhang · 2011-03-01 00:00

    zwwspace writes:

    不错,正好有个需求,参考参考 不过我的是android上的

    这个很久写过一个示例,后面就没搞了。呵呵

  3. #2 楼 · zwwspace · 2011-02-28 17:51

    不错,正好有个需求,参考参考 不过我的是android上的

  4. #1 楼 · jpgtama · 2011-02-18 22:31

    恩,我跑了楼主的代码,完全可以。 :idea:

    但一处的错误可能会出现几个Error Message。

    看看语法高亮:

    package com;
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    
    class SAXValidator {
    public static void main(String[] args) {
    try {
    String fileName = "conf/invalid_dtd.xml";
    File x = new File(fileName);
    SAXParserFactory f = SAXParserFactory.newInstance();
    f.setValidating(true); // Default is false
    SAXParser p = f.newSAXParser();
    DefaultHandler h = new MyErrorHandler();
    p.parse(x, h);
    } catch (ParserConfigurationException e) {
    System.out.println(e.toString());
    } catch (SAXException e) {
    System.out.println(e.toString());
    } catch (IOException e) {
    System.out.println(e.toString());
    }
    }
    
    private static class MyErrorHandler extends DefaultHandler {
    public void warning(SAXParseException e) throws SAXException {
    System.out.println("Warning: ");
    printInfo(e);
    }
    
    public void error(SAXParseException e) throws SAXException {
    System.out.println("Error: ");
    printInfo(e);
    }
    
    public void fatalError(SAXParseException e) throws SAXException {
    System.out.println("Fattal error: ");
    printInfo(e);
    }
    
    private void printInfo(SAXParseException e) {
    System.out.println("   Public ID: " + e.getPublicId());
    System.out.println("   System ID: " + e.getSystemId());
    System.out.println("   Line number: " + e.getLineNumber());
    System.out.println("   Column number: " + e.getColumnNumber());
    System.out.println("   Message: " + e.getMessage());
    }
    }
    } 
    

评论功能维护中,当前仅展示历史评论。