本文整理汇总了Java中javax.xml.parsers.SAXParserFactory.setSchema方法的典型用法代码示例。如果您正苦于以下问题:Java SAXParserFactory.setSchema方法的具体用法?Java SAXParserFactory.setSchema怎么用?Java SAXParserFactory.setSchema使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.SAXParserFactory
的用法示例。
在下文中一共展示了SAXParserFactory.setSchema方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void test() throws SAXException, ParserConfigurationException, IOException {
Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema)));
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setSchema(schema);
// saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace",
// true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new MyContentHandler());
// InputStream input =
// ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml");
InputStream input = getClass().getResourceAsStream("Bug6946312.xml");
System.out.println("Parse InputStream:");
xmlReader.parse(new InputSource(input));
if (!charEvent) {
Assert.fail("missing character event");
}
}
示例2: parse
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Parse the given XML string an create/update the corresponding entities
*
* @param xml
* the XML string
* @return the parse return code
* @throws Exception
*/
public int parse(byte[] xml) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try (InputStream inputStream = ResourceLoader.getResourceAsStream(
getClass(), getSchemaName())) {
Schema schema = sf.newSchema(new StreamSource(inputStream));
spf.setSchema(schema);
}
SAXParser saxParser = spf.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setFeature(Constants.XERCES_FEATURE_PREFIX
+ Constants.DISALLOW_DOCTYPE_DECL_FEATURE, true);
reader.setContentHandler(this);
reader.parse(new InputSource(new ByteArrayInputStream(xml)));
return 0;
}
示例3: verifyXML
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private void verifyXML(byte[] xml) throws IOException, SAXException,
ParserConfigurationException {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
final Schema schema;
try (InputStream inputStream = ResourceLoader.getResourceAsStream(
getClass(), "TechnicalServices.xsd")) {
schema = sf.newSchema(new StreamSource(inputStream));
}
spf.setSchema(schema);
SAXParser saxParser = spf.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
ErrorHandler errorHandler = new MyErrorHandler();
reader.setErrorHandler(errorHandler);
reader.parse(new InputSource(new ByteArrayInputStream(xml)));
}
示例4: createReader
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private XMLReader createReader() throws SAXException {
try {
SAXParserFactory pfactory = SAXParserFactory.newInstance();
pfactory.setValidating(false);
pfactory.setNamespaceAware(true);
// Enable schema validation
SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));
return pfactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new SAXException(ex);
}
}
示例5: createReader
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private XMLReader createReader() throws SAXException {
try {
SAXParserFactory pfactory = SAXParserFactory.newInstance();
pfactory.setValidating(true);
pfactory.setNamespaceAware(true);
// Enable schema validation
SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));
return pfactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new SAXException(ex);
}
}
示例6: testSAX
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testSAX() {
try {
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(_xsd));
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
spf.setSchema(schema);
SAXParser parser = spf.newSAXParser();
MyErrorHandler errorHandler = new MyErrorHandler();
parser.parse(_xml, errorHandler);
if (!errorHandler.errorOccured) {
Assert.fail("should report error");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
示例7: test
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void test() {
try {
Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(testFile));
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setSchema(schema);
// saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace",
// true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new DefaultHandler());
// InputStream input =
// ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml");
InputStream input = getClass().getResourceAsStream("Issue682.xml");
System.out.println("Parse InputStream:");
xmlReader.parse(new InputSource(input));
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.toString());
}
}
示例8: testSAX
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testSAX() throws ParserConfigurationException, SAXException, IOException {
InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml");
// Parse with SAX
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setSchema(schema);
SAXParser saxparser = saxFactory.newSAXParser();
sawIgnorable = false;
saxparser.parse(xmlFile, new MyHandler());
Assert.assertEquals(true, sawIgnorable);
}
示例9: testConformantSAX
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testConformantSAX() throws ParserConfigurationException, SAXException, IOException {
InputStream xmlFile = getClass().getResourceAsStream("Bug6564400.xml");
// Parse with SAX
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setSchema(schema);
saxFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", true);
SAXParser saxparser = saxFactory.newSAXParser();
sawIgnorable = false;
saxparser.parse(xmlFile, new MyHandler());
Assert.assertEquals(false, sawIgnorable);
}