Validate File Format

简介: <p><span style="font-family:KaiTi_GB2312; font-size:14px">介绍</span></p> <p><span style="font-family:KaiTi_GB2312; font-size:14px">    通过Schema来校验文件格式的正确与否。</span></p> <p><span style="font-family

介绍

    通过Schema来校验文件格式的正确与否。


package shuai.study.validator;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class FormatValidator {
	public static boolean isFormatValid(URL urlSchema, Source source) {
		boolean validatorFlag = true;

		SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		try {
			Schema schema = schemaFactory.newSchema(urlSchema);

			Validator validator = schema.newValidator();
			validator.validate(source);
		} catch (SAXException se) {
			validatorFlag = false;
			se.printStackTrace();
		} catch (IOException ioe) {
			validatorFlag = false;
			ioe.printStackTrace();
		}

		return validatorFlag;
	}

	public static void main(String[] args) {
		URL urlSchema = FormatValidator.class.getResource("/schema/TestFileFormat.xsd");

		String sourceFileString = FormatValidator.class.getResource("/xml/TestSourceFile.xml").getPath();
		Source source = new StreamSource(new File(sourceFileString));

		boolean validatorFlag = FormatValidator.isFormatValid(urlSchema, source);

		if (validatorFlag) {
			System.out.println("Analyze " + source.getSystemId() + " successfully");
		} else {
			System.out.println("Fail to analyze " + source.getSystemId());
		}
	}
}


相关文章
|
NoSQL Linux
gdb调试产生code文件以及遇到的“file format not recognized”问题解决
gdb调试产生code文件以及遇到的“file format not recognized”问题解决
1202 0
|
6月前
|
iOS开发
解决Invalid `Podfile` file: no implicit conversion of nil into String
解决Invalid `Podfile` file: no implicit conversion of nil into String
87 0
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
Invalid mapping pattern detected: /download/{{fileName}} ^Not allowed to nest variable c
|
9月前
|
索引
Elasticsearch exception [type=illegal_argument_exception, reason=index [.1] is the write index for data stream [slowlog] and cannot be deleted]
在 Elasticsearch 中,你尝试删除的索引是一个数据流(data stream)的一部分,而且是数据流的写入索引(write index),因此无法直接删除它。为了解决这个问题,你可以按照以下步骤进行操作:
807 0
|
9月前
Parsing error: No Babel config file detected for XXXXX
Parsing error: No Babel config file detected for XXXXX
Newline required at end of file but not found.
Newline required at end of file but not found.
222 0
Newline required at end of file but not found.
perhaps your file is in a different file format and youneed to use a different restore operator?
perhaps your file is in a different file format and youneed to use a different restore operator?
185 0
|
JSON 移动开发 算法框架/工具
成功解决raise ValueError(‘No model found in config file.‘) ValueError: No model found in config file.
成功解决raise ValueError(‘No model found in config file.‘) ValueError: No model found in config file.
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件
Can‘t read file : End of file found 文件:txn_current、current svn无法正常读取文件

热门文章

最新文章