本文整理汇总了Java中javax.xml.validation.Schema.newValidator方法的典型用法代码示例。如果您正苦于以下问题:Java Schema.newValidator方法的具体用法?Java Schema.newValidator怎么用?Java Schema.newValidator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.validation.Schema
的用法示例。
在下文中一共展示了Schema.newValidator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validation
import javax.xml.validation.Schema; //导入方法依赖的package包/类
/**
* 利用xsd验证xml
* @param xsdFile xsdFile
* @param xmlInput xmlInput
* @throws SAXException SAXException
* @throws IOException IOException
*/
public static void validation(String xsdFile, InputStream xmlInput) throws SAXException, IOException
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL xsdURL = Validation.class.getClassLoader().getResource(xsdFile);
if(xsdURL != null)
{
Schema schema = factory.newSchema(xsdURL);
Validator validator = schema.newValidator();
// validator.setErrorHandler(new AutoErrorHandler());
Source source = new StreamSource(xmlInput);
try(OutputStream resultOut = new FileOutputStream(new File(PathUtil.getRootDir(), xsdFile + ".xml")))
{
Result result = new StreamResult(resultOut);
validator.validate(source, result);
}
}
else
{
throw new FileNotFoundException(String.format("can not found xsd file [%s] from classpath.", xsdFile));
}
}
示例2: validate
import javax.xml.validation.Schema; //导入方法依赖的package包/类
private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));
// Get a Validator which can be used to validate instance document
// against this grammar.
Validator validator = schema.newValidator();
ErrorHandler eh = new ErrorHandlerImpl();
validator.setErrorHandler(eh);
// Validate this instance document against the
// Instance document supplied
validator.validate(src, result);
} catch (Exception ex) {
throw ex;
}
}
示例3: getValidator
import javax.xml.validation.Schema; //导入方法依赖的package包/类
/**
* Helper method that returns a validator for our XSD, or null if the current Java
* implementation can't process XSD schemas.
*
* @param version The version of the XML Schema.
* See {@link SdkStatsConstants#getXsdStream(int)}
*/
private Validator getValidator(int version) throws SAXException {
InputStream xsdStream = SdkStatsConstants.getXsdStream(version);
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
if (factory == null) {
return null;
}
// This may throw a SAX Exception if the schema itself is not a valid XSD
Schema schema = factory.newSchema(new StreamSource(xsdStream));
Validator validator = schema == null ? null : schema.newValidator();
return validator;
} finally {
if (xsdStream != null) {
try {
xsdStream.close();
} catch (IOException ignore) {}
}
}
}
示例4: validateXSD
import javax.xml.validation.Schema; //导入方法依赖的package包/类
protected void validateXSD(Document signedDoc) throws SAXException, IOException {
NodeList nodeList = signedDoc.getElementsByTagNameNS(RedactableXMLSignature.XML_NAMESPACE, "Signature");
assertEquals(1, nodeList.getLength());
Node signature = nodeList.item(0);
NodeList childNodes = signature.getChildNodes();
int actualNodes = 0;
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
++actualNodes;
}
}
assertEquals(3, actualNodes);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("xmlrss_schema.xsd"));
Validator validator = schema.newValidator();
validator.validate(new DOMSource(signature));
}
示例5: testParticlesOptimize
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void testParticlesOptimize() {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String xsdFile = "particlesOptimize.xsd";
Schema schema = sf.newSchema(new File(getClass().getResource(xsdFile).toURI()));
Validator validator = schema.newValidator();
} catch (Exception ex) {
Assert.fail("Parser configuration error not expected since maxOccurs " + "> 5000 but constant-space optimization applies");
}
}
示例6: testStAXWResult
import javax.xml.validation.Schema; //导入方法依赖的package包/类
/**
* workaround before the fix: provide a result
*/
@Test
public final void testStAXWResult() {
try {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
// XMLStreamReader staxReader =
// xmlif.createXMLStreamReader((Source)new
// StreamSource(getClass().getResource("Forum31576.xml").getFile()));
XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
Validator schemaValidator = schemaGrammar.newValidator();
Source staxSrc = new StAXSource(staxReader);
File resultFile = new File(USER_DIR + "gMonths.result.xml");
if (resultFile.exists()) {
resultFile.delete();
}
Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
schemaValidator.validate(staxSrc, xmlResult);
while (staxReader.hasNext()) {
int eventType = staxReader.next();
System.out.println("Event of type: " + eventType);
}
} catch (Exception e) {
Assert.fail(e.getMessage());
e.printStackTrace();
}
}
示例7: validate
import javax.xml.validation.Schema; //导入方法依赖的package包/类
public static void validate (String xmlPath, String xsdPath) throws Exception {
// create a SchemaFactory capable of understanding WXS schemas
prtln ("schemaNS_URI: " + XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Schema schema = null;
/* use schema from file */
schema = factory.newSchema(new File(xsdPath));
/* use schema from web */
// String adnURI = "http://www.dlese.org/Metadata/adn-item/0.6.50/record.xsd";
// schema = factory.newSchema(new URL(adnURI));
/* use schema from instance document */
// schema = factory.newSchema();
/* create a Validator instance, which can be used to validate an instance document */
Validator validator = schema.newValidator();
/* the xml to be validated */
Source mysource = new StreamSource (xmlPath);
// validation errors passed as SAXException message
try {
validator.validate(mysource);
} catch (SAXException e) {
prtln (e.getMessage());
}
}
示例8: test
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source[] sources = getSchemaSources();
Schema schema = sf.newSchema(sources);
Validator validator = schema.newValidator();
}
示例9: testValidateComplexTypeWithSimpleContent
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void testValidateComplexTypeWithSimpleContent() throws IOException, ParserConfigurationException, SAXException {
try {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
dFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
Document document = dBuilder.parse(getClass().getResourceAsStream("Bug6695843.xsd"));
DOMSource domSource = new DOMSource(document);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(domSource);
String xmlFileName = "Bug6695843.xml";
Document document1 = dBuilder.parse(getClass().getResourceAsStream(xmlFileName));
DOMSource domSource1 = new DOMSource(document1);
Validator validator = schema.newValidator();
validator.validate(domSource1);
} catch (Exception e) {
String msg = e.getMessage();
System.out.println(e.getMessage());
if (msg.indexOf("maxLength '20' for type 'null'") > 0) {
Assert.fail("vague error message");
}
}
}
示例10: serialize
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void serialize() throws Exception {
// given
SupplierRevenueShareResult result = supplierShareAssembler.build(
SUPPLIER_KEY, PERIOD_START_TIME, PERIOD_END_TIME);
result.calculateAllShares();
// when
JAXBContext jc = JAXBContext
.newInstance(SupplierRevenueShareResult.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshal(result, bos);
assertNotNull(bos.toByteArray());
final List<String> fragments = new ArrayList<String>();
fragments.add(new String(bos.toByteArray(), "UTF-8"));
byte[] xmlBytes = XMLConverter.combine("RevenueSharesResults",
fragments, SupplierRevenueShareResult.SCHEMA);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
bos1.write(xmlBytes);
printXml(bos1);
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = BillingServiceBean.class.getResource("/"
+ "SupplierRevenueShareResult.xsd");
Schema schema = schemaFactory.newSchema(schemaUrl);
Source xmlFile = new StreamSource(new ByteArrayInputStream(xmlBytes));
Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
示例11: validateXML
import javax.xml.validation.Schema; //导入方法依赖的package包/类
/**
* Validates the XML data against the specified schema.
*
* @param schemaFileURL
* The URL to the schema file.
* @param xmlContent
* The XML data to be validated.
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public static void validateXML(URL schemaFileURL, Document xmlContent)
throws SAXException, IOException, TransformerException {
SchemaFactory factory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(schemaFileURL);
Validator validator = schema.newValidator();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
printDocument(xmlContent, byteArrayOutputStream);
ByteArrayInputStream bis = new ByteArrayInputStream(
byteArrayOutputStream.toByteArray());
validator.validate(new StreamSource(bis));
}
示例12: setUp
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@BeforeMethod
public void setUp() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XSD.getBytes()));
StreamSource xsdSource = new StreamSource(reader);
Schema schema = schemaFactory.newSchema(xsdSource);
this.validatorHandler = schema.newValidatorHandler();
this.validator = schema.newValidator();
}
示例13: testParticlesR005
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void testParticlesR005() {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String xsdFile = "particlesR005.xsd";
Schema schema = sf.newSchema(new File(getClass().getResource(xsdFile).toURI()));
Validator validator = schema.newValidator();
} catch (Exception ex) {
return; // expected
}
Assert.fail("Parser configuration error expected since maxOccurs > 5000 " + "and constant-space optimization does not apply");
}
示例14: test
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void test() throws SAXException, IOException {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(ParticleTest.class.getResourceAsStream("upa01.xsd")));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(ParticleTest.class.getResourceAsStream("upa01.xml")));
}
示例15: testFeatureReset
import javax.xml.validation.Schema; //导入方法依赖的package包/类
@Test
public void testFeatureReset() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = makeSchema(factory, null);
Validator validator = schema.newValidator();
validator.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);
validator.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);
validator.reset();
boolean value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
assertTrue("After reset, value of feature on Validator should be true.", value);
value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
assertFalse("After reset, value of feature on Validator should be false.", value);
}