本文整理汇总了Java中org.xml.sax.XMLReader类的典型用法代码示例。如果您正苦于以下问题:Java XMLReader类的具体用法?Java XMLReader怎么用?Java XMLReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLReader类属于org.xml.sax包,在下文中一共展示了XMLReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testcase08
import org.xml.sax.XMLReader; //导入依赖的package包/类
/**
* Test newTransformerHandler with a Template Handler.
*
* @throws Exception If any errors occur.
*/
public void testcase08() throws Exception {
String outputFile = USER_DIR + "saxtf008.out";
String goldFile = GOLDEN_DIR + "saxtf008GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
TransformerHandler tfhandler
= saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例2: getSAXParser
import org.xml.sax.XMLReader; //导入依赖的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: processSheet
import org.xml.sax.XMLReader; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private <T> List<T> processSheet(StylesTable styles, ReadOnlySharedStringsTable readOnlySharedStringsTable,
Class<T> type, InputStream sheetInputStream) {
DataFormatter formatter = new DataFormatter();
InputSource sheetSource = new InputSource(sheetInputStream);
try {
XMLReader sheetParser = SAXHelper.newXMLReader();
PoijiHandler poijiHandler = new PoijiHandler(type, options);
ContentHandler contentHandler =
new XSSFSheetXMLHandler(styles, null, readOnlySharedStringsTable, poijiHandler, formatter, false);
sheetParser.setContentHandler(contentHandler);
sheetParser.parse(sheetSource);
return poijiHandler.getDataset();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}
示例4: read
import org.xml.sax.XMLReader; //导入依赖的package包/类
public static RssFeed read(InputStream stream) throws SAXException, IOException {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
RssHandler handler = new RssHandler();
InputSource input = new InputSource(stream);
reader.setContentHandler(handler);
reader.parse(input);
return handler.getResult();
} catch (ParserConfigurationException e) {
throw new SAXException();
}
}
示例5: load
import org.xml.sax.XMLReader; //导入依赖的package包/类
public static SyntaxScheme load(Font baseFont, InputStream in)
throws IOException {
SyntaxSchemeLoader parser = null;
try {
XMLReader reader = XMLReaderFactory.createXMLReader();
parser = new SyntaxSchemeLoader(baseFont);
parser.baseFont = baseFont;
reader.setContentHandler(parser);
InputSource is = new InputSource(in);
is.setEncoding("UTF-8");
reader.parse(is);
} catch (SAXException se) {
throw new IOException(se.toString());
}
return parser.scheme;
}
示例6: getFromFile
import org.xml.sax.XMLReader; //导入依赖的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;
}
}
示例7: getXMLReader
import org.xml.sax.XMLReader; //导入依赖的package包/类
private XMLReader getXMLReader() throws Exception {
SAXParser parser = spf.newSAXParser();
parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
parser.setProperty(JAXP_SCHEMA_SOURCE, "catalog.xsd");
XMLReader catparser = parser.getXMLReader();
catparser.setErrorHandler(new CatalogErrorHandler());
return catparser;
}
示例8: xsltprocess
import org.xml.sax.XMLReader; //导入依赖的package包/类
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
// 1. Instantiate a TransformerFactory.
SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
InputStream is = getClass().getResourceAsStream("xmg2pol.xsl");
Transformer transformer = tFactory.newTransformer (new StreamSource(is));
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
// 3. Use the Transformer to transform an XML Source and send the
// output to a Result object.
try {
String input = args[0];
String output= args[1];
SAXSource saxs = new SAXSource(new InputSource(input));
XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
saxReader.setEntityResolver(new MyEntityResolver());
saxs.setXMLReader(saxReader);
transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8")));
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: testcase12
import org.xml.sax.XMLReader; //导入依赖的package包/类
/**
* Unit test for contentHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase12() throws Exception {
String outputFile = USER_DIR + "saxtf012.out";
String goldFile = GOLDEN_DIR + "saxtf012GF.out";
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
InputSource is = new InputSource(new FileInputStream(XSLT_FILE));
SAXSource saxSource = new SAXSource();
saxSource.setInputSource(is);
SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
XMLFilter filter = saxTFactory.newXMLFilter(saxSource);
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(XML_FILE));
assertTrue(compareWithGold(goldFile, outputFile));
}
示例10: processAll
import org.xml.sax.XMLReader; //导入依赖的package包/类
private void processAll(OPCPackage pkg)
throws IOException, OpenXML4JException, InvalidFormatException, SAXException {
XSSFReader xssfReader = new XSSFReader(pkg);
mStylesTable = xssfReader.getStylesTable();
SharedStringsTable sst = xssfReader.getSharedStringsTable();
XMLReader parser = this.fetchSheetParser(sst);
Iterator<InputStream> sheets = xssfReader.getSheetsData();
while (sheets.hasNext()) {
mCurrentRowIndex = 0;
mSheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
pkg.close();
}
示例11: getItems
import org.xml.sax.XMLReader; //导入依赖的package包/类
private Map<String,Map<String,List<Map<String,String>>>> getItems () {
if (items == null)
try {
XMLReader reader = XMLUtil.createXMLReader ();
Handler handler = new Handler ();
reader.setEntityResolver (handler);
reader.setContentHandler (handler);
ClassLoader loader = (ClassLoader) Lookup.getDefault ().
lookup (ClassLoader.class);
InputStream is = loader.getResourceAsStream (resourceName);
try {
reader.parse (new InputSource (is));
} finally {
is.close ();
}
items = handler.result;
} catch (Exception ex) {
ErrorManager.getDefault ().notify (ex);
items = Collections.<String,Map<String,List<Map<String,String>>>> emptyMap ();
}
return items;
}
示例12: parse
import org.xml.sax.XMLReader; //导入依赖的package包/类
@Override
public GraphDocument parse() throws IOException {
if (monitor != null) {
monitor.setState("Starting parsing");
}
try {
XMLReader reader = createReader();
reader.setContentHandler(new XMLParser(xmlDocument, monitor));
reader.parse(new InputSource(Channels.newInputStream(channel)));
} catch (SAXException ex) {
if (!(ex instanceof SAXParseException) || !"XML document structures must start and end within the same entity.".equals(ex.getMessage())) {
throw new IOException(ex);
}
}
if (monitor != null) {
monitor.setState("Finished parsing");
}
return graphDocument;
}
示例13: testWithTrueFalse
import org.xml.sax.XMLReader; //导入依赖的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));
}
示例14: testSAX
import org.xml.sax.XMLReader; //导入依赖的package包/类
@Test(dataProvider = "xml-data")
public void testSAX(String xml, int chunkSize, int numOfChunks, boolean withinLimit) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader reader = spf.newSAXParser().getXMLReader();
MyHandler handler = new MyHandler(chunkSize);
reader.setContentHandler(handler);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
if (chunkSize > 0) {
reader.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
reader.parse(new InputSource(new StringReader(xml)));
System.out.println("CData num of chunks:" + handler.getNumOfCDataChunks());
System.out.println("CData size within limit:" + handler.chunkSizeWithinLimit());
Assert.assertEquals(handler.getNumOfCDataChunks(), numOfChunks);
Assert.assertEquals(handler.chunkSizeWithinLimit(), withinLimit);
}
示例15: testcase01
import org.xml.sax.XMLReader; //导入依赖的package包/类
/**
* Content event handler visit all nodes to print to output file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase01() throws Exception {
String outputFile = USER_DIR + "Content.out";
String goldFile = GOLDEN_DIR + "ContentGF.out";
String xmlFile = XML_DIR + "namespace1.xml";
try(FileInputStream instream = new FileInputStream(xmlFile);
MyContentHandler cHandler = new MyContentHandler(outputFile)) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setContentHandler(cHandler);
xmlReader.parse(new InputSource(instream));
}
assertTrue(compareWithGold(goldFile, outputFile));
}