本文整理汇总了Java中org.xml.sax.XMLReader.parse方法的典型用法代码示例。如果您正苦于以下问题:Java XMLReader.parse方法的具体用法?Java XMLReader.parse怎么用?Java XMLReader.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.xml.sax.XMLReader
的用法示例。
在下文中一共展示了XMLReader.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: parse
import org.xml.sax.XMLReader; //导入方法依赖的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();
}
示例3: verifyXML
import org.xml.sax.XMLReader; //导入方法依赖的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: process
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* 遍历工作簿中所有的电子表格
*
* @param filename
* @throws Exception
*/
public void process(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
Iterator<InputStream> sheets = r.getSheetsData();
while (sheets.hasNext()) {
curRow = 0;
sheetIndex++;
InputStream sheet = sheets.next();
InputSource sheetSource = new InputSource(sheet);
parser.parse(sheetSource);
sheet.close();
}
}
示例5: create
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
if (_buffer == null) {
createBuffer();
}
_buffer.setSystemId(systemId);
reader.setContentHandler(this);
reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);
try {
setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
} catch (SAXException e) {
}
if (systemId != null) {
InputSource s = new InputSource(systemId);
s.setByteStream(in);
reader.parse(s);
} else {
reader.parse(new InputSource(in));
}
return getXMLStreamBuffer();
}
示例6: modifyXml
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
@Override
public String modifyXml(Reader reader, HtmlContentHandler writer)
{
InputSource s = new InputSource();
s.setEncoding(Constants.UTF8);
s.setCharacterStream(reader);
try
{
XMLReader r = new Parser();
r.setContentHandler(writer);
r.parse(s);
return writer.getOutput();
}
catch( Exception e )
{
throw new RuntimeException(e);
}
}
示例7: testcase13
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Unit test for TemplatesHandler setter/getter.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase13() throws Exception {
String outputFile = USER_DIR + "saxtf013.out";
String goldFile = GOLDEN_DIR + "saxtf013GF.out";
try(FileInputStream fis = new FileInputStream(XML_FILE)) {
// The transformer will use a SAX parser as it's reader.
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
// I have put this as it was complaining about systemid
thandler.setSystemId("file:///" + USER_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_FILE);
XMLFilter filter
= saxTFactory.newXMLFilter(thandler.getTemplates());
filter.setParent(reader);
filter.setContentHandler(new MyContentHandler(outputFile));
filter.parse(new InputSource(fis));
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例8: parse
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* The recognizer entry method taking an InputSource.
* @param input InputSource to be parsed.
* @throws java.io.IOException on I/O error.
* @throws SAXException propagated exception thrown by a DocumentHandler.
* @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
* @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
*
*/
public void parse(final InputSource input) throws SAXException, ParserConfigurationException, IOException {
if (used.getAndSet(true)) {
throw new IllegalStateException("The LibraryDeclarationParser was already used, create a new instance"); //NOI18N
}
try {
final XMLReader parser = XMLUtil.createXMLReader(false, true);
parser.setContentHandler(this);
parser.setErrorHandler(getDefaultErrorHandler());
parser.setEntityResolver(this);
parser.parse(input);
} finally {
//Recover recognizer internal state from exceptions to be reusable
if (!context.empty()) {
context.clear();
}
if (buffer.length() > 0) {
buffer.delete(0, buffer.length());
}
expectedNS = null;
}
}
示例9: 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;
}
示例10: parse
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Parse the content given {@link org.xml.sax.InputSource}
* as XML using the specified
* {@link org.xml.sax.helpers.DefaultHandler}.
*
* @param is The InputSource containing the content to be parsed.
* @param dh The SAX DefaultHandler to use.
*
* @throws IllegalArgumentException If the <code>InputSource</code> object
* is <code>null</code>.
* @throws IOException If any IO errors occur.
* @throws SAXException If any SAX errors occur during processing.
*
* @see org.xml.sax.DocumentHandler
*/
public void parse(InputSource is, DefaultHandler dh)
throws SAXException, IOException {
if (is == null) {
throw new IllegalArgumentException("InputSource cannot be null");
}
XMLReader reader = this.getXMLReader();
if (dh != null) {
reader.setContentHandler(dh);
reader.setEntityResolver(dh);
reader.setErrorHandler(dh);
reader.setDTDHandler(dh);
}
reader.parse(is);
}
示例11: load
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* Load and read the manifest.xml for a Locale.
* @param locale_manifest_in
* @return
*/
public Locale load(InputStream locale_manifest_in) {
LocaleParser localeParser = new LocaleParser();
try {
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader xr = sp.getXMLReader();
xr.setContentHandler(localeParser);
xr.parse(new InputSource(new BufferedInputStream(locale_manifest_in)));
} catch (Exception e) {
Debug.e(e);
}
return localeParser.getLocale();
}
示例12: 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);
}
示例13: read
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
protected void read(final ProtocolFactory protocols, final Local child) throws AccessDeniedException {
try {
final BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream(),
Charset.forName("UTF-8")));
AbstractHandler handler = this.getHandler(protocols);
final XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
xr.parse(new InputSource(in));
}
catch(SAXException | IOException e) {
log.error(String.format("Error reading %s:%s", this.getFile(), e.getMessage()));
}
}
示例14: processOneSheet
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
/**
* 只遍历一个电子表格,其中sheetId为要遍历的sheet索引,从1开始,1-3
*
* @param filename
* @param sheetId
* @throws Exception
*/
public void processOneSheet(String filename, int sheetId) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
// 根据 rId# 或 rSheet# 查找sheet
InputStream sheet2 = r.getSheet("rId" + sheetId);
sheetIndex++;
InputSource sheetSource = new InputSource(sheet2);
parser.parse(sheetSource);
sheet2.close();
}
示例15: testXMLReader
import org.xml.sax.XMLReader; //导入方法依赖的package包/类
public void testXMLReader(boolean setUseCatalog, boolean useCatalog, String catalog,
String xml, MyHandler handler, String expected) throws Exception {
XMLReader reader = getXMLReader(setUseCatalog, useCatalog, catalog);
reader.setContentHandler(handler);
reader.setEntityResolver(handler);
reader.parse(xml);
Assert.assertEquals(handler.getResult().trim(), expected);
}