当前位置: 首页>>代码示例>>Java>>正文


Java SAXParser.parse方法代码示例

本文整理汇总了Java中javax.xml.parsers.SAXParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java SAXParser.parse方法的具体用法?Java SAXParser.parse怎么用?Java SAXParser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.parsers.SAXParser的用法示例。


在下文中一共展示了SAXParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import javax.xml.parsers.SAXParser; //导入方法依赖的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());
    }
}
 
开发者ID:Bibliome,项目名称:bibliome-java-utils,代码行数:19,代码来源:Test.java

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

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:DefaultHandlerTest.java

示例3: parseRules

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
public List<XmlRule> parseRules(final InputStream input) throws IOException {
    final SAXParser parser;
    try {
        parser = SAXParserFactory.newInstance().newSAXParser();
        final RulesHandler handler = new RulesHandler();
        parser.parse(input, handler);

        final List<XmlRuleImpl> rules = handler.getRules();
        final List<XmlRule> result = new ArrayList<>(rules.size());
        result.addAll(rules);

        return result;
    } catch (ParserConfigurationException | SAXException e) {
        throw new IOException("Can't create SAX parser", e);
    }
}
 
开发者ID:AlexanderBartash,项目名称:hybris-integration-intellij-idea-plugin,代码行数:17,代码来源:XmlRuleParser.java

示例4: readTestConfigFile

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
/**
 * Read the test config file - testConfig.xml
 */
protected void readTestConfigFile() {
  String testConfigFile = getTestFile();
  if (testsFromConfigFile == null) {
    boolean success = false;
    testConfigFile = TEST_CACHE_DATA_DIR + File.separator + testConfigFile;
    try {
      SAXParser p = (SAXParserFactory.newInstance()).newSAXParser();
      p.parse(testConfigFile, getConfigParser());
      success = true;
    } catch (Exception e) {
      LOG.info("File: " + testConfigFile + " not found");
      success = false;
    }
    assertTrue("Error reading test config file", success);
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:20,代码来源:CLITestHelper.java

示例5: 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; 
	}
  }
 
开发者ID:capitalone,项目名称:Hydrograph,代码行数:21,代码来源:XMLParser.java

示例6: 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

示例7: main

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        saxParserFactory.setNamespaceAware(true);

        SAXParser saxParser = saxParserFactory.newSAXParser();

        ParserVocabulary referencedVocabulary = new ParserVocabulary();

        VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(referencedVocabulary);
        File f = new File(args[0]);
        saxParser.parse(f, vocabularyGenerator);

        printVocabulary(referencedVocabulary);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:22,代码来源:PrintTable.java

示例8: XmlInputArchive

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
/** Creates a new instance of BinaryInputArchive */
public XmlInputArchive(InputStream in)
throws ParserConfigurationException, SAXException, IOException {
    valList = new ArrayList<Value>();
    DefaultHandler handler = new XMLParser(valList);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    parser.parse(in, handler);
    vLen = valList.size();
    vIdx = 0;
}
 
开发者ID:l294265421,项目名称:ZooKeeper,代码行数:12,代码来源:XmlInputArchive.java

示例9: sendToParser

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
private static void sendToParser(String b) throws Throwable {
    byte[] input = b.getBytes("UTF-8");
    ByteArrayInputStream in = new ByteArrayInputStream(input);

    SAXParserFactory  spf = SAXParserFactory.newInstance();
    SAXParser p = spf.newSAXParser();
    p.parse(new InputSource(in), new DefaultHandler());
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:9,代码来源:UTF8ReaderBug.java

示例10: testValidMaxOccurLimitWithOutSecureProcessing

import javax.xml.parsers.SAXParser; //导入方法依赖的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());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:Bug6309988.java

示例11: runTest

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
void runTest(String xmlString) {
    Bug6573786ErrorHandler handler = new Bug6573786ErrorHandler();
    try {
        InputStream is = new StringBufferInputStream(xmlString);
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(is, handler);
    } catch (Exception e) {
        if (handler.fail) {
            Assert.fail("The value of standalone attribute should be merged into the error message.");
        }
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:Bug6573786.java

示例12: parse

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
private static void parse(Bundle bundle, InputStream is, Collection<PersistenceUnit> punits) {
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = parserFactory.newSAXParser();
        JPAHandler handler = new JPAHandler(bundle);
        parser.parse(is, handler);
        punits.addAll(handler.getPersistenceUnits());
    } catch (Exception e) {
        throw new RuntimeException("Error parsing persistence unit in bundle " + bundle.getSymbolicName(), e); // NOSONAR
    } finally {
        safeClose(is);
    }
}
 
开发者ID:apache,项目名称:aries-jpa,代码行数:14,代码来源:PersistenceUnitParser.java

示例13: test

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
    String invalidXml = "<a>";
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
    SAXParser parser = saxParserFactory.newSAXParser();
    parser.parse(new InputSource(new StringReader(invalidXml)), new DefaultHandler() {
        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            System.err.printf("%s%n", e.getMessage());
        }
    });
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:HandleError.java

示例14: test

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
@Test
public void test() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new FileReader(getClass().getResource("Bug6518733.xml").getFile())), new Handler());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:Bug6518733.java

示例15: testEmptyStream

import javax.xml.parsers.SAXParser; //导入方法依赖的package包/类
@Test
public void testEmptyStream() {
    try {
        SAXParser parser = factory.newSAXParser();
        InputSource source = new InputSource(new StringReader(""));
        parser.parse(source, new MyHandler());
        Assert.fail("Inputstream without document element accepted");
    } catch (Exception ex) {
        System.out.println("Exception thrown: " + ex.getMessage());
        // Premature end of file exception expected
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:ParseEmptyStream.java


注:本文中的javax.xml.parsers.SAXParser.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。