xsd通过java验证科学计数法

是在不容易,javascript中和java中验证数字包含科学计数法,都很容易通过,而且写法很多,随便google或baidu以下就出来,但是使用java通过xsd验证xml文件中的科学计数法内容,怎么写都不同,经过N次试验,被我试验出来了,写法如下:
<xs:simpleType name="intType">
<xs:restriction base="xs:string">
<xs:pattern value="((-)?\d*(\.\d*)?([Ee]?(-)?\d*)?)/>
</xs:restriction>
  </xs:simpleType>

Java验证是使用apache的common包axiom,验证如下:
package com.sunrise.jnjp.test;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.XMLConstants;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
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.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.xml.sax.SAXException;

public class AxiomTest {

	public static boolean validate(String schemaLocaltion, OMElement request)
			throws SAXException, IOException {
		// 获取Schema工厂类
		// 这里的XMLConstants.W3C_XML_SCHEMA_NS_URI的值就是://http://www.w3.org/2001/XMLSchema
		SchemaFactory factory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		// Schema实例
		Schema schema = null;
		// 获取XSD文件,以流的方式读取到Source中
		// XSD文件的位置相对于类文件位置
		InputStream inp = AxiomTest.class
		.getClassLoader().getResourceAsStream(schemaLocaltion);
		Source schemaSource = new StreamSource(inp);
		// 实例化Schema对象
		schema = factory.newSchema(schemaSource);
		// 这里是将一个DOM树对象转换成流对象,以便对DOM树对象验证
		// 如果是对XML文件进行验证,用FileInputStream即可
		String input = request.toString();
		ByteArrayInputStream bais = new ByteArrayInputStream(input
				.getBytes("UTF-8"));
		// 获取验证器,验证器的XML Schema源就是之前创建的Schema
		Validator validator = schema.newValidator();
		Source source = new StreamSource(bais);
		// 执行验证
		try {
			validator.validate(source);
			return true;
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			return false;
		}
	}

	/**
	 * get OMElement soap request from specified XML file.
	 * 
	 * @param request
	 * @return
	 * @throws FileNotFoundException
	 * @throws XMLStreamException
	 * @throws FactoryConfigurationError
	 */
	public static OMElement getRequest(String filePath)
			throws FileNotFoundException, XMLStreamException,
			FactoryConfigurationError {
		XMLStreamReader reader = XMLInputFactory.newInstance()
				.createXMLStreamReader(AxiomTest.class.getResourceAsStream(filePath));
		StAXOMBuilder builder = new StAXOMBuilder(reader);
		OMElement requestMessage = builder.getDocumentElement();
		return requestMessage;
	}
	
	public static OMElement getRequest(InputStream is)
		throws FileNotFoundException, XMLStreamException,
		FactoryConfigurationError {
	XMLStreamReader reader = XMLInputFactory.newInstance()
			.createXMLStreamReader(is);
	StAXOMBuilder builder = new StAXOMBuilder(reader);
	OMElement requestMessage = builder.getDocumentElement();
	return requestMessage;
	}

	public static void main(String[] args) // throws SAXException, IOException,
	{
		try {
			if (validate("xsd/Common.xsd", getRequest("test.xml"))) {
				System.out.println("customer_err.xml格式良好");
			} else {
				System.out.println("customer_err.xml格式有误,请检查!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("文件格式有误,请检查!");
		}

	}



}

xsd文件在附件中

你可能感兴趣的:(JavaScript,java,apache,xml,SOAP)