當前位置: 首頁>>代碼示例>>Java>>正文


Java XMLReader.parse方法代碼示例

本文整理匯總了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;
    }
}
 
開發者ID:uhuru-mobile,項目名稱:mobile-store,代碼行數:24,代碼來源:RepoDetails.java

示例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();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:23,代碼來源:XML_SAX_StAX_FI.java

示例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)));
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:28,代碼來源:ServiceProvisioningServiceBeanImportExportSchemaIT.java

示例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();
	}
}
 
開發者ID:tb544731152,項目名稱:iBase4J,代碼行數:22,代碼來源:Excel2007Reader.java

示例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();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:25,代碼來源:SAXBufferCreator.java

示例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);
	}
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:19,代碼來源:ConvertHtmlServiceImpl.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:SAXTFactoryTest.java

示例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;
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:31,代碼來源:LibraryDeclarationParser.java

示例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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:23,代碼來源:LibrarySupport.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:SAXParser.java

示例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();
}
 
開發者ID:Linguaculturalists,項目名稱:Phoenicia,代碼行數:19,代碼來源:LocaleLoader.java

示例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);

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:CDataChunkSizeTest.java

示例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()));
    }
}
 
開發者ID:iterate-ch,項目名稱:cyberduck,代碼行數:15,代碼來源:XmlBookmarkCollection.java

示例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();
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:21,代碼來源:Excel2007Reader.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:CatalogSupportBase.java


注:本文中的org.xml.sax.XMLReader.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。