本文整理汇总了Java中javax.xml.parsers.SAXParserFactory.newSAXParser方法的典型用法代码示例。如果您正苦于以下问题:Java SAXParserFactory.newSAXParser方法的具体用法?Java SAXParserFactory.newSAXParser怎么用?Java SAXParserFactory.newSAXParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.SAXParserFactory
的用法示例。
在下文中一共展示了SAXParserFactory.newSAXParser方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSAXParser
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private static SAXParser getSAXParser(
boolean validating,
JspDocumentParser jspDocParser)
throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
// Preserve xmlns attributes
factory.setFeature(
"http://xml.org/sax/features/namespace-prefixes",
true);
factory.setValidating(validating);
//factory.setFeature(
// "http://xml.org/sax/features/validation",
// validating);
// Configure the parser
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setProperty(LEXICAL_HANDLER_PROPERTY, jspDocParser);
xmlReader.setErrorHandler(jspDocParser);
return saxParser;
}
示例2: newSAXParser
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Create a <code>SAXParser</code> configured to support XML Schema and DTD
* @param properties parser specific properties/features
* @return an XML Schema/DTD enabled <code>SAXParser</code>
*/
public static SAXParser newSAXParser(Properties properties)
throws ParserConfigurationException,
SAXException,
SAXNotRecognizedException{
SAXParserFactory factory =
(SAXParserFactory)properties.get("SAXParserFactory");
SAXParser parser = factory.newSAXParser();
String schemaLocation = (String)properties.get("schemaLocation");
String schemaLanguage = (String)properties.get("schemaLanguage");
try{
if (schemaLocation != null) {
parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
}
} catch (SAXNotRecognizedException e){
log.info(parser.getClass().getName() + ": "
+ e.getMessage() + " not supported.");
}
return parser;
}
示例3: main
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
File inputFile = new File(args[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Logger logger = Logger.getAnonymousLogger();
logger.setLevel(Level.FINE);
YateaHandler handler = new YateaHandler(logger, docBuilder);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLUtils.ignoreDTD(spf);
SAXParser saxParser = spf.newSAXParser();
saxParser.parse(inputFile, handler);
Map<String,TermCandidate> candidates = handler.getTermCandidates();
for (TermCandidate cand : candidates.values()) {
System.out.println(cand.toString() + ", HEAD:" + cand.getHead());
}
}
示例4: testSaxParserConref
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* <p><b>Description:</b> Test the conrefs are handled by the SAXParser.</p>
* <p><b>Bug ID:</b> #15</p>
*
* @author adrian_sorop
*
* @throws Exception
*/
public void testSaxParserConref() throws Exception {
File ditaFile = new File(TestUtil.getPath("issue-15_1/topics"),"topic2.dita");
assertTrue("UNABLE TO LOAD FILE", ditaFile.exists());
URL url = URLUtil.correct(ditaFile);
SAXParserFactory factory = SAXParserFactory.newInstance();
// Ignore the DTD declaration
factory.setValidating(false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
SAXParser parser = factory.newSAXParser();
SaxContentHandler handler= new SaxContentHandler(url);
parser.parse(ditaFile, handler);
List<ReferencedResource> referredFiles = new ArrayList<ReferencedResource>();
referredFiles.addAll(handler.getDitaMapHrefs());
assertEquals("Two files should have been referred.", 2, referredFiles.size());
assertTrue("Should be a content reference to topicConref.dita but was" + referredFiles.get(1).getLocation(),
referredFiles.get(1).getLocation().toString().contains("issue-15_1/topics/topicConref.dita"));
assertTrue("Should be a xref to topic3.dita",
referredFiles.get(0).getLocation().toString().contains("issue-15_1/topics/topic3.dita"));
}
开发者ID:oxygenxml,项目名称:oxygen-dita-translation-package-builder,代码行数:34,代码来源:AttributesCollectorUsingSaxTest.java
示例5: getFromFile
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@NonNull
public static RepoDetails getFromFile(InputStream inputStream, int pushRequests) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
RepoDetails repoDetails = new RepoDetails();
MockRepo mockRepo = new MockRepo(100, pushRequests);
RepoXMLHandler handler = new RepoXMLHandler(mockRepo, repoDetails);
reader.setContentHandler(handler);
InputSource is = new InputSource(new BufferedInputStream(inputStream));
reader.parse(is);
return repoDetails;
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
fail();
// Satisfies the compiler, but fail() will always throw a runtime exception so we never
// reach this return statement.
return null;
}
}
示例6: testPath
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testPath() throws Exception {
if (isWindows) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser jaxpParser = factory.newSAXParser();
XMLReader reader = jaxpParser.getXMLReader();
reader.parse("C:/space error/x.xml");
System.exit(0);
} catch (Exception e) {
System.out.println(e.getMessage());
if (e.getMessage().equalsIgnoreCase("unknown protocol: c")) {
Assert.fail("jdk5 allowed the above form");
} else if (e.getMessage().indexOf("(The system cannot find the path specified)") > 0) {
// expected
}
}
}
}
示例7: test
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
SAXParserFactory fac = SAXParserFactory.newInstance();
fac.setNamespaceAware(true);
SAXParser saxParser = fac.newSAXParser();
StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml"));
InputSource is = SAXSource.sourceToInputSource(sr);
RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser);
SAXSource src = new SAXSource(rf, is);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
transformer.transform(src, result);
Document doc = (Document) result.getNode();
System.out.println("Name" + doc.getDocumentElement().getLocalName());
String id = "XWSSGID-11605791027261938254268";
Element selement = doc.getElementById(id);
if (selement == null) {
System.out.println("getElementById returned null");
}
}
示例8: testValidMaxOccurLimitWithOutSecureProcessing
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public void testValidMaxOccurLimitWithOutSecureProcessing() {
if (isSecureMode())
return; // jaxp secure feature can not be turned off when security
// manager is present
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
spf.setValidating(true);
// Set the properties for Schema Validation
String SCHEMA_LANG = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
String SCHEMA_TYPE = "http://www.w3.org/2001/XMLSchema";
// Get the Schema location as a File object
File schemaFile = new File(this.getClass().getResource("toys3002.xsd").toURI());
// Get the parser
SAXParser parser = spf.newSAXParser();
parser.setProperty(SCHEMA_LANG, SCHEMA_TYPE);
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaFile);
InputStream is = this.getClass().getResourceAsStream("toys.xml");
MyErrorHandler eh = new MyErrorHandler();
parser.parse(is, eh);
Assert.assertFalse(eh.errorOccured, "Expected Error as maxOccurLimit is exceeded");
} catch (Exception e) {
Assert.fail("Exception occured: " + e.getMessage());
}
}
示例9: testWithTrueFalse
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Namespace processing is enabled. Hence namespace-prefix is
* expected to be automatically off. So it is a True-False combination.
* The test is to test XMLReader with these conditions.
*
* @throws Exception If any errors occur.
*/
public void testWithTrueFalse() throws Exception {
String outputFile = USER_DIR + "XRNSTableTF.out";
String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
try (FileInputStream fis = new FileInputStream(xmlFile);
MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(fis));
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例10: readXml
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* readXml
*
* @param inStream inStream
* @param nodeName nodeName
* @param form form
* @return maps
* @throws Exception e
*/
public static List<Map<String, String>> readXml(InputStream inStream, String nodeName, String[] form)
throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
ApkXmlParser handler = new ApkXmlParser(nodeName, form);
saxParser.parse(inStream, handler);
inStream.close();
return handler.getList();
}
示例11: parseAndCheckReset
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private void parseAndCheckReset(boolean setFeature, boolean value) throws Exception {
// Expected result based on system property and feature
boolean resetExpected = setFeature && value;
// Indicates if system property is set
boolean spSet = runWithAllPerm(() -> System.getProperty(RESET_FEATURE)) != null;
// Dummy xml input for parser
String input = "<dummy>Test</dummy>";
// Check if system property is set only when feature setting is not requested
// and estimate if reset of symbol table is expected
if (!setFeature && spSet) {
resetExpected = runWithAllPerm(() -> Boolean.getBoolean(RESET_FEATURE));
}
// Create SAXParser and set feature if it is requested
SAXParserFactory spf = SAXParserFactory.newInstance();
if (setFeature) {
spf.setFeature(RESET_FEATURE, value);
}
SAXParser p = spf.newSAXParser();
// First parse iteration
p.parse(new InputSource(new StringReader(input)), new DefaultHandler());
// Get first symbol table reference
Object symTable1 = p.getProperty(SYMBOL_TABLE_PROPERTY);
// reset parser
p.reset();
// Second parse iteration
p.parse(new InputSource(new StringReader(input)), new DefaultHandler());
// Get second symbol table reference
Object symTable2 = p.getProperty(SYMBOL_TABLE_PROPERTY);
// Check symbol table references after two subsequent parse operations
if (resetExpected) {
Assert.assertNotSame(symTable1, symTable2, "Symbol table references");
} else {
Assert.assertSame(symTable1, symTable2, "Symbol table references");
}
}
示例12: testEntityExpansionSAXNeg
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
* Setting the EntityExpansion Limit to 2 and checks if the XML
* document that has more than two levels of entity expansion is parsed or
* not. Previous system property was changed to jdk.xml.entityExpansionLimit
* see http://docs.oracle.com/javase/tutorial/jaxp/limits/limits.html.
*
* @throws Exception If any errors occur.
*/
@Test(expectedExceptions = SAXParseException.class)
public void testEntityExpansionSAXNeg() throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
// Secure processing will limit XML processing to conform to
// implementation limits.
factory.setFeature(FEATURE_SECURE_PROCESSING, true);
// Set entityExpansionLimit as 2 should expect SAXParseException.
setSystemProperty(SP_ENTITY_EXPANSION_LIMIT, String.valueOf(2));
SAXParser parser = factory.newSAXParser();
MyErrorHandler fatalHandler = new MyErrorHandler();
parser.parse(new File(ENTITY_XML), fatalHandler);
}
示例13: testPBImageXmlWriter
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testPBImageXmlWriter() throws IOException, SAXException,
ParserConfigurationException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream o = new PrintStream(output);
PBImageXmlWriter v = new PBImageXmlWriter(new Configuration(), o);
v.visit(new RandomAccessFile(originalFsimage, "r"));
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
final String xml = output.toString();
parser.parse(new InputSource(new StringReader(xml)), new DefaultHandler());
}
示例14: BlastXmlOutputParser
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public BlastXmlOutputParser (String filePath) throws ParserConfigurationException, SAXException {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
SAXBlastHandler handler = new SAXBlastHandler();
try {
parser.parse(new FileInputStream(filePath), handler);
for (SAXBlastResult result : handler.results) {
LOGGER.debug(result.toString());
String[] definitionFields = result.hitDefinition.split("\\|");
numMatches.add(result.numMatches);
double pcnt = 100.0 * (double) result.numMatches / result.numPositions;
pcntMatches.add(pcnt);
matchLines.add(result.matchLine);
medianMismatchPositions.add(medianMisMatch(result.matchLine));
subtypes.add(definitionFields[0]);
countries.add(definitionFields[1]);
authorYrs.add(definitionFields[2]);
genBankAccessions.add(definitionFields[4]);
startNAs.add(Integer.valueOf(definitionFields[6]));
endNAs.add(Integer.valueOf(definitionFields[7]));
}
tabularOutput = createTabularOutput();
LOGGER.debug("\n" + tabularOutput);
} catch (IOException e) {
e.printStackTrace();
}
}
示例15: testException
import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@Test
public void testException() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new ByteArrayInputStream(textXML.getBytes()), new TestFilter());
} catch (Throwable t) {
t.printStackTrace();
}
}