本文整理匯總了Java中javax.xml.parsers.SAXParser類的典型用法代碼示例。如果您正苦於以下問題:Java SAXParser類的具體用法?Java SAXParser怎麽用?Java SAXParser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SAXParser類屬於javax.xml.parsers包,在下文中一共展示了SAXParser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test
import javax.xml.parsers.SAXParser; //導入依賴的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: getSAXParser
import javax.xml.parsers.SAXParser; //導入依賴的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;
}
示例3: testMaxOccurLimitPos
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Testing set MaxOccursLimit to 10000 in the secure processing enabled for
* SAXParserFactory.
*
* @throws Exception If any errors occur.
*/
@Test
public void testMaxOccurLimitPos() throws Exception {
String schema_file = XML_DIR + "toys.xsd";
String xml_file = XML_DIR + "toys.xml";
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setFeature(FEATURE_SECURE_PROCESSING, true);
setSystemProperty(SP_MAX_OCCUR_LIMIT, String.valueOf(10000));
SAXParser parser = factory.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
parser.setProperty(JAXP_SCHEMA_SOURCE, new File(schema_file));
try (InputStream is = new FileInputStream(xml_file)) {
MyErrorHandler eh = new MyErrorHandler();
parser.parse(is, eh);
assertFalse(eh.isAnyError());
}
}
示例4: configureOldXerces
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Configure schema validation as recommended by the JAXP 1.2 spec.
* The <code>properties</code> object may contains information about
* the schema local and language.
* @param properties parser optional info
*/
private static void configureOldXerces(SAXParser parser,
Properties properties)
throws ParserConfigurationException,
SAXNotSupportedException {
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.");
}
}
示例5: newSAXParser
import javax.xml.parsers.SAXParser; //導入依賴的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;
}
示例6: testDefaultHandler
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Test default handler that transverses XML and print all visited node.
*
* @throws Exception If any errors occur.
*/
@Test
public void testDefaultHandler() throws Exception {
String outputFile = USER_DIR + "DefaultHandler.out";
String goldFile = GOLDEN_DIR + "DefaultHandlerGF.out";
String xmlFile = XML_DIR + "namespace1.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxparser = spf.newSAXParser();
MyDefaultHandler handler = new MyDefaultHandler(outputFile);
File file = new File(xmlFile);
String Absolutepath = file.getAbsolutePath();
String newAbsolutePath = Absolutepath;
if (File.separatorChar == '\\')
newAbsolutePath = Absolutepath.replace('\\', '/');
saxparser.parse("file:///" + newAbsolutePath, handler);
assertTrue(compareWithGold(goldFile, outputFile));
}
示例7: testSaxParserConref
import javax.xml.parsers.SAXParser; //導入依賴的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
示例8: testParse25
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Test with valid input source, parser should parse the XML document
* successfully.
*
* @param saxparser a SAXParser instance.
* @throws Exception If any errors occur.
*/
@Test(dataProvider = "parser-provider")
public void testParse25(SAXParser saxparser) throws Exception {
try (FileInputStream instream = new FileInputStream(
new File(XML_DIR, "parsertest.xml"))) {
saxparser.parse(instream, new DefaultHandler(),
new File(XML_DIR).toURI().toASCIIString());
}
}
示例9: parse
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
documentSerializer.setOutputStream(finf);
SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
reader.setContentHandler(saxTostax);
if (workingDirectory != null) {
reader.setEntityResolver(createRelativePathResolver(workingDirectory));
}
reader.parse(new InputSource(xml));
xml.close();
finf.close();
}
示例10: allowFileAccess
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
private static SAXParser allowFileAccess(SAXParser saxParser, boolean disableSecureProcessing) throws SAXException {
// if feature secure processing enabled, nothing to do, file is allowed,
// or user is able to control access by standard JAXP mechanisms
if (disableSecureProcessing) {
return saxParser;
}
try {
saxParser.setProperty(ACCESS_EXTERNAL_SCHEMA, "file");
LOGGER.log(Level.FINE, Messages.format(Messages.JAXP_SUPPORTED_PROPERTY, ACCESS_EXTERNAL_SCHEMA));
} catch (SAXException ignored) {
// nothing to do; support depends on version JDK or SAX implementation
LOGGER.log(Level.CONFIG, Messages.format(Messages.JAXP_UNSUPPORTED_PROPERTY, ACCESS_EXTERNAL_SCHEMA), ignored);
}
return saxParser;
}
示例11: parseXML
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**Parses the XML to fetch parameters.
* @param inputFile, source XML
* @return true, if XML is successfully parsed.
* @throws Exception
*/
public boolean parseXML(File inputFile,UIComponentRepo componentRepo) throws ParserConfigurationException, SAXException, IOException{
LOGGER.debug("Parsing target XML for separating Parameters");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser;
try {
factory.setFeature(Constants.DISALLOW_DOCTYPE_DECLARATION,true);
saxParser = factory.newSAXParser();
XMLHandler xmlhandler = new XMLHandler(componentRepo);
saxParser.parse(inputFile, xmlhandler);
return true;
} catch (ParserConfigurationException | SAXException | IOException exception) {
LOGGER.error("Parsing failed...",exception);
throw exception;
}
}
示例12: readCategoryDatasetFromXML
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Reads a {@link CategoryDataset} from a stream.
*
* @param in the stream.
*
* @return A dataset.
*
* @throws IOException if there is a problem reading the file.
*/
public static CategoryDataset readCategoryDatasetFromXML(final InputStream in) throws IOException {
CategoryDataset result = null;
final SAXParserFactory factory = SAXParserFactory.newInstance();
try {
final SAXParser parser = factory.newSAXParser();
final CategoryDatasetHandler handler = new CategoryDatasetHandler();
parser.parse(in, handler);
result = handler.getDataset();
}
catch (SAXException e) {
System.out.println(e.getMessage());
}
catch (ParserConfigurationException e2) {
System.out.println(e2.getMessage());
}
return result;
}
示例13: testInstance
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
@Test
public void testInstance() throws ParserConfigurationException, SAXException, IOException {
System.out.println(Bug6963468Test.class.getResource("Bug6963468.xsd").getPath());
File schemaFile = new File(Bug6963468Test.class.getResource("Bug6963468.xsd").getPath());
SAXParser parser = createParser(schemaFile);
try {
parser.parse(Bug6963468Test.class.getResource("Bug6963468.xml").getPath(), new DefaultHandler());
} catch (SAXException e) {
e.printStackTrace();
Assert.fail("Fatal Error: " + strException(e));
}
}
示例14: createParser
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
protected static SAXParser createParser() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
SAXParser parser = spf.newSAXParser();
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
return parser;
}
示例15: findLine
import javax.xml.parsers.SAXParser; //導入依賴的package包/類
/**
* Find the line number of a target in an Ant script, or some other line in an XML file.
* Able to find a certain element with a certain attribute matching a given value.
* See also AntTargetNode.TargetOpenCookie.
* @param file an Ant script or other XML file
* @param match the attribute value to match (e.g. target name)
* @param elementLocalName the (local) name of the element to look for
* @param elementAttributeName the name of the attribute to match on
* @return the line number (0-based), or -1 if not found
*/
static final int findLine(FileObject file, final String match, final String elementLocalName, final String elementAttributeName) throws IOException, SAXException, ParserConfigurationException {
InputSource in = new InputSource(file.getURL().toString());
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
final int[] line = new int[] {-1};
class Handler extends DefaultHandler {
private Locator locator;
public void setDocumentLocator(Locator l) {
locator = l;
}
public void startElement(String uri, String localname, String qname, Attributes attr) throws SAXException {
if (line[0] == -1) {
if (localname.equals(elementLocalName) && match.equals(attr.getValue(elementAttributeName))) { // NOI18N
line[0] = locator.getLineNumber() - 1;
}
}
}
}
parser.parse(in, new Handler());
return line[0];
}