@Test
public void test() throws SAXException, ParserConfigurationException, IOException {
SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(XML_DIR + "shiporder11.xsd"));
validatorHandler = schema.newValidatorHandler();
MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
validatorHandler.setContentHandler(myDefaultHandler);
InputSource is = new InputSource(filenameToURL(XML_DIR + "shiporder11.xml"));
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(validatorHandler);
xmlReader.parse(is);
}
java类javax.xml.parsers.SAXParserFactory的实例源码
TypeInfoProviderTest.java 文件源码
项目:openjdk-jdk10
阅读 23
收藏 0
点赞 0
评论 0
AttributesTest.java 文件源码
项目:openjdk-jdk10
阅读 31
收藏 0
点赞 0
评论 0
/**
* Unit test for Attributes interface. Prints all attributes into output
* file. Check it with golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase01() throws Exception {
String outputFile = USER_DIR + "Attributes.out";
String goldFile = GOLDEN_DIR + "AttributesGF.out";
String xmlFile = XML_DIR + "family.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
true);
spf.setValidating(true);
SAXParser saxParser = spf.newSAXParser();
MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile);
saxParser.parse(new File(xmlFile), myAttrCHandler);
myAttrCHandler.flushAndClose();
assertTrue(compareWithGold(goldFile, outputFile));
}
Parser.java 文件源码
项目:openjdk-jdk10
阅读 25
收藏 0
点赞 0
评论 0
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);
}
}
EHFatalTest.java 文件源码
项目:openjdk-jdk10
阅读 23
收藏 0
点赞 0
评论 0
/**
* Error Handler to capture all error events to output file. Verifies the
* output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testEHFatal() throws Exception {
String outputFile = USER_DIR + "EHFatal.out";
String goldFile = GOLDEN_DIR + "EHFatalGF.out";
String xmlFile = XML_DIR + "invalid.xml";
try(MyErrorHandler eHandler = new MyErrorHandler(outputFile);
FileInputStream instream = new FileInputStream(xmlFile)) {
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setErrorHandler(eHandler);
InputSource is = new InputSource(instream);
xmlReader.parse(is);
fail("Parse should throw SAXException");
} catch (SAXException expected) {
// This is expected.
}
// Need close the output file before we compare it with golden file.
assertTrue(compareWithGold(goldFile, outputFile));
}
Bug6509668.java 文件源码
项目:openjdk-jdk10
阅读 20
收藏 0
点赞 0
评论 0
private XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
if (!parserFactory.isNamespaceAware()) {
parserFactory.setNamespaceAware(true);
}
return parserFactory.newSAXParser().getXMLReader();
}
SaxActionParser.java 文件源码
项目:UIAutomatorAdapter
阅读 21
收藏 0
点赞 0
评论 0
@Override
public List<App> parse(InputStream is) throws Exception {
// get SAXParserFactory instance
SAXParserFactory factory = SAXParserFactory.newInstance();
// get SAXParser instance from SAXParserFactory instance
SAXParser parser = factory.newSAXParser();
// new appHandler
appHandler handler = new appHandler();
// parse is with handler
parser.parse(is, handler);
return handler.getApps();
}
SAXHandlerAndroidManifest.java 文件源码
项目:DELDroid
阅读 19
收藏 0
点赞 0
评论 0
public static void main(String[] args) {
// System.out.println("SAXHandlerAndroidManifest ...");
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
File xmlFile = new File(ANDROID_FRAMEWORK_MANIFEST_PATH);
SimpleSAXHandler handler = new SAXHandlerAndroidManifest();
InputStream is = new FileInputStream(xmlFile);
saxParser.parse(is, handler);
System.out.println(componentsMap.size());
} catch (Exception e) {
e.printStackTrace();
}
}
CheckHelpSets.java 文件源码
项目:incubator-netbeans
阅读 26
收藏 0
点赞 0
评论 0
@Override
public void processMapRef(HelpSet hs, @SuppressWarnings("rawtypes") Hashtable attrs) {
try {
URL map = new URL(hs.getHelpSetURL(), (String)attrs.get("location"));
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
parser.parse(new InputSource(map.toExternalForm()), new Handler(map.getFile()));
} catch (Exception e) {
e.printStackTrace();
}
}
SAXParserTest03.java 文件源码
项目:openjdk-jdk10
阅读 22
收藏 0
点赞 0
评论 0
/**
* invalidns.xml holds an invalid document with XML namespaces in it. This
* method tests the validating parser with namespace processing on. It
* should throw validation error.
*
* @param spf a Parser factory.
* @param handler an error handler for capturing events.
* @throws Exception If any errors occur.
*/
@Test(dataProvider = "input-provider")
public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler)
throws Exception {
try {
spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser();
saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler);
fail("Expecting SAXException here");
} catch (SAXException e) {
assertTrue(handler.isErrorOccured());
}
}
XmlSaxTask.java 文件源码
项目:oma-riista-android
阅读 21
收藏 0
点赞 0
评论 0
@Override
protected final void onAsyncStream(InputStream stream) throws Exception
{
TaskXMLHandler handler = new TaskXMLHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputSource is = new InputSource(stream);
if (mEncoding != null) {
is.setEncoding(mEncoding);
}
parser.parse(is, handler);
}