第一步: java code
package com.tr.geda.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
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.struts2.ServletActionContext;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
//$Id: XMLValidator.java $Version: v1.0 Oct 6, 2011 lester.hao$
/**
* @ClassName: XMLValidator
* @Description: XMLValidator is used for XML well formed checking and schema based validation
* @author cyong
* @date May 6, 2012 1:08:57 PM
*/
public class XMLValidator {
/**
* @param validateType: validateType, isWellformed or isValidated
* @param xmlFilePath: xmlFilePath
* @param schemaFilePathList: only for validateType=isValidated,
* If there are multiple schema files, schemaFilePathList could be comma split
*/
public static void main(String[] args) {
//String CONTEXT_PATH = ServletActionContext.getServletContext().getRealPath("");
String CONTEXT_PATH ="D:/work/QA_branch/20120528_Bus/JAVA_Application/JAVA/GEDA/WebContent";
String schemaList = CONTEXT_PATH + "/WEB-INF/schema/dds/OTFC_Schema.xsd";
String xmlFilePath = "D:\\work\\QA_Dev_Branch\\QA_Automation\\QA_Auto_Test\\JAVA_IFFM\\OTFCDDS\\TestData\\VALIDATEXSD.xml";
String validateType = "isValidated";
validateXML(validateType,xmlFilePath,schemaList);
}
public static void validateXML(String validateType,String xmlFilePath,String schemaFilePathList)
{
long startTime = System.currentTimeMillis();
/* args: "isValidated" or "isWellformed"
"D:\Projects\XML\test\50_add.xml"
"D:\Projects\XML\test\OTFC_Schema.xsd"*/
if (validateType.equals("isWellformed")) {
boolean isWellformed = isWellformed(xmlFilePath);
if (isWellformed) {
System.out.println("XML document is well formed");
System.out.println((System.currentTimeMillis() - startTime)
/ 1000.0 + " s");
System.exit(0);
} else {
System.out.println("XML document is not well formed, see log file "
+ xmlFilePath + ".log");
System.out.println((System.currentTimeMillis() - startTime)
/ 1000.0 + " s");
System.exit(1);
}
} else if (validateType.equals("isValidated")) {
// firstly check if XML file is well formed
boolean isValidated = isWellformed(xmlFilePath);
// validate XML file based on given schema files
if(isValidated){
isValidated = isValidated(xmlFilePath, schemaFilePathList, 50);
}
if (isValidated) {
System.out.println("XML document is valid");
System.out.println((System.currentTimeMillis() - startTime)
/ 1000.0 + " s");
System.exit(0);
} else {
System.out.println("XML document is invalid, see log file "
+ xmlFilePath + ".log");
System.out.println((System.currentTimeMillis() - startTime)
/ 1000.0 + " s");
System.exit(1);
}
}
}
/**
*
* @param xmlFilePath
* @return boolean
*/
static public boolean isWellformed(String xmlFilePath) {
boolean isWellformed = true;
String line = "";
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = null;
saxParser.parse(new File(xmlFilePath), handler);
} catch (IOException e) {
isWellformed = false;
line = "IOException: " + e.getMessage();
} catch (SAXException e) {
isWellformed = false;
line = "SAXException: " + e.getMessage();
} catch (Exception e) {
isWellformed = false;
line = "Exception: " + e.getMessage();
}
if (!isWellformed) {
BufferedWriter errorLogBW;
try {
errorLogBW = new BufferedWriter(new FileWriter(xmlFilePath
+ ".log"));
errorLogBW.write(line);
errorLogBW.newLine();
errorLogBW.close();
} catch (IOException e) {
// System.out.println("IOException: " + e);
}
}
return isWellformed;
}
/**
*
* @param xmlFilePath
* @param schemaFilePathList, it is comma split
* @param maxErrorLineNum
* @return boolean
*/
public static boolean isValidated(String xmlFilePath,
String schemaFilePathList, final int maxErrorLineNum) {
final List<String> errorLines = new ArrayList<String>();
boolean isValid = true;
String[] schemaDirNames = schemaFilePathList.split(",");
if (schemaDirNames == null || schemaDirNames.length == 0) {
return false;
}
SchemaFactory factory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
Source schemas[] = new Source[schemaDirNames.length];
for (int i = 0; i < schemaDirNames.length; i++) {
schemas[i] = new StreamSource(schemaDirNames[i]);
}
try {
Schema schema = factory.newSchema(schemas);
Validator validator = schema.newValidator();
validator.setErrorHandler(new ErrorHandler() {
public void error(SAXParseException e) throws SAXException {
if (errorLines.size() < maxErrorLineNum) {
errorLines.add("Line[" + e.getLineNumber() + "]" + " "
+ "ERROR: " + e.getMessage());
} else {
throw new SAXException("[ERROR]: too many errors", e);
}
}
public void fatalError(SAXParseException e) throws SAXException {
// System.out.println("[FATAL]: " + e);
throw new SAXException("[FATAL]: Line[" + e.getLineNumber()
+ "] " + e);
}
public void warning(SAXParseException e) {
// System.out.println("[WARN]: " + e);
}
});
validator.validate(new StreamSource(xmlFilePath));
if (errorLines.size() != 0) {
isValid = false;
}
} catch (SAXException e) {
errorLines.add("SAXException: " + e.getMessage());
isValid = false;
} catch (FileNotFoundException e) {
errorLines.add("FileNotFoundException: " + e.getMessage());
isValid = false;
} catch (IOException e) {
errorLines.add("IOException: " + e.getMessage());
isValid = false;
}
if (!isValid) {
BufferedWriter errorLogBW;
try {
errorLogBW = new BufferedWriter(new FileWriter(xmlFilePath
+ ".log"));
for (String line : errorLines) {
errorLogBW.write(line);
errorLogBW.newLine();
}
errorLogBW.close();
} catch (IOException e) {
// System.out.println("IOException: " + e);
}
}
return isValid;
}
}
第二步: 写 xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Create Date:2012-05-24 -->
<!-- Create By:cyong-->
<!-- Last Update Date:2012-05-24 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dm="http://tempuri.org"
xmlns="DbFieldsSchema"
targetNamespace="DbFieldsSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="1.5">
<xs:simpleType name="T_RIC">
<xs:restriction base="xs:string">
<xs:pattern value="[!|#-%|*-:|<-?|A-}]{0,32}" />
<xs:maxLength value="32" />
</xs:restriction>
</xs:simpleType>
<xs:element name="RIC" type="T_RIC" nillable="false" dm:editable="false" dm:extractable="true"/>
<xs:group name="AllFieldsGroup">
<xs:sequence>
<xs:any maxOccurs="unbounded" minOccurs="0"/>
<xs:element ref="RIC" maxOccurs="1" minOccurs="1" />
<xs:any maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:group>
<xs:complexType name="OtfcFieldsType">
<xs:all>
<xs:element ref="RIC" maxOccurs="1" minOccurs="1" />
</xs:all>
</xs:complexType>
<xs:annotation>
<xs:appinfo>On the Fly Creation Schema Version1.6</xs:appinfo>
<xs:documentation>OTFC Schema Copyright 2010 ThomsonReuters Limited </xs:documentation>
</xs:annotation>
<xs:element name="FEED">
<xs:complexType>
<xs:sequence>
<!--XML document info-->
<xs:element name="NAME" type="xs:string"/>
<xs:element name="VERSION" type="xs:float"/>
<xs:element name="INSTRUMENT" type="OtfcFieldsType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
第三步,写出要校验的xml file
<?xml version="1.0" encoding="UTF-8"?>
<FEED xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="DbFieldsSchema" xsi:schemaLocation="DbFieldsSchema OTFC_Schema.xsd">
<NAME>RAM_OPRA_TZH1.xml</NAME>
<VERSION>1.5</VERSION>
<INSTRUMENT>
<DSPLY_NAME>AAPL 1Mr9 10.0 C</DSPLY_NAME>
<EXCHANGE>AO</EXCHANGE>
<IMSOUT>TRUE</IMSOUT>
<ISSUTYPE>111</ISSUTYPE>
<PROV_SYMB>AAPLC9999900001</PROV_SYMB>
<RIC>AAPLC99999001</RIC>
<SERVICE>NYSE</SERVICE>
<SYMBOL>AAPLC9999900001</SYMBOL>
<DDS_SYMBOL>AAPLC9999900001</DDS_SYMBOL>
<UP_LH_ID>SI1</UP_LH_ID>
<UP_FM_ACTION_TYPE>0</UP_FM_ACTION_TYPE>
<UP_OTFC_TPL_ID>NYSE%OTFC_TPL_ONLYDDS</UP_OTFC_TPL_ID>
<UP_OTFC_TYPE>FIRST_TICK</UP_OTFC_TYPE>
<UP_KEY_RENAME>FALSE</UP_KEY_RENAME>
<UP_SYMBOL_RENAME>FALSE</UP_SYMBOL_RENAME>
<UP_OLD_RIC>AAPLC011801001</UP_OLD_RIC>
<UP_OLD_SYMBOL>AAPLC01180100001</UP_OLD_SYMBOL>
<UP_OLD_DDS_SYMBOL>AAPLC01180100001</UP_OLD_DDS_SYMBOL>
</INSTRUMENT>
<INSTRUMENT>
<DSPLY_NAME>AAPL 1Mr9 10.0 C</DSPLY_NAME>
<EXCHANGE>AO</EXCHANGE>
<IMSOUT>TRUE</IMSOUT>
<ISSUTYPE>111</ISSUTYPE>
<PROV_SYMB>AAPLC9999900001</PROV_SYMB>
<RIC>AAPLC99999001</RIC>
<SERVICE>NYSE</SERVICE>
<SYMBOL>AAPLC9999900001</SYMBOL>
<DDS_SYMBOL>AAPLC9999900001</DDS_SYMBOL>
<UP_LH_ID>SI1</UP_LH_ID>
<UP_FM_ACTION_TYPE>0</UP_FM_ACTION_TYPE>
<UP_OTFC_TPL_ID>NYSE%OTFC_TPL_PARALLELRUN</UP_OTFC_TPL_ID>
<UP_OTFC_TYPE>FIRST_TICK</UP_OTFC_TYPE>
<UP_KEY_RENAME>FALSE</UP_KEY_RENAME>
<UP_SYMBOL_RENAME>FALSE</UP_SYMBOL_RENAME>
<UP_OLD_RIC>AAPLC011801001</UP_OLD_RIC>
<UP_OLD_SYMBOL>AAPLC01180100001</UP_OLD_SYMBOL>
<UP_OLD_DDS_SYMBOL>AAPLC01180100001</UP_OLD_DDS_SYMBOL>
</INSTRUMENT>
</FEED>