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


Java SAXParserFactory.newInstance方法代码示例

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


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

示例1: testDefaultHandler

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的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

示例2: main

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的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

示例3: testCheckSchemaSupport3

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
 * Test the default functionality of schema support method. In
 * this case the schema source property is set.
 * @throws Exception If any errors occur.
 */
@Test(dataProvider = "schema-source")
public void testCheckSchemaSupport3(Object schemaSource) throws Exception {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
        spf.setNamespaceAware(true);
        SAXParser sp = spf.newSAXParser();
        sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                W3C_XML_SCHEMA_NS_URI);
        sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource);
        DefaultHandler dh = new DefaultHandler();
        // Not expect any unrecoverable error here.
        sp.parse(new File(XML_DIR, "test1.xml"), dh);
    } finally {
        if (schemaSource instanceof Closeable) {
            ((Closeable) schemaSource).close();
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:DocumentBuilderFactoryTest.java

示例4: parse

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@NonNull
private Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom)
        throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(NAMESPACE_FEATURE, true);
        factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            xml = xml.replaceFirst("^([\\W]+)<","<");  //$NON-NLS-1$ //$NON-NLS-2$
            return parse(xml, new InputSource(new StringReader(xml)), false);
        }
        throw e;
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:23,代码来源:PositionXmlParser.java

示例5: DefenseInboundAdapter

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public DefenseInboundAdapter(AdapterDefinition definition) throws ComponentException, ParserConfigurationException, SAXException, IOException
{
	super(definition);
	messageParser = new MessageParser(this);
	saxFactory = SAXParserFactory.newInstance();
	saxParser = saxFactory.newSAXParser();
}
 
开发者ID:Esri,项目名称:defense-solutions-proofs-of-concept,代码行数:8,代码来源:DefenseInboundAdapter.java

示例6: testSaxParser

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
 * <p><b>Description:</b> Verify the attribute collector over DITA map.
 * Collect referred files non-recursion.</p>
 * <p><b>Bug ID:</b> #9</p>
 *
 * @author adrian_sorop
 *
 * @throws Exception
 */
public void testSaxParser() throws Exception {

  File ditaFile = new File(rootDir,"rootMap.ditamap");
  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("Referred topic in dita maps should be topic2.dita", 
      referredFiles.toString().contains("issue-9/topics/topic2.dita"));
  assertTrue("Referred topic in dita maps should be topic1.dita", 
      referredFiles.toString().contains("issue-9/topics/topic1.dita"));
}
 
开发者ID:oxygenxml,项目名称:oxygen-dita-translation-package-builder,代码行数:34,代码来源:AttributesCollectorUsingSaxTest.java

示例7: testXMLNoNsAwareStreamResult2

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
 * Test an identity transform of an XML document with NS decls using a
 * non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to
 * FALSE and prefixes to TRUE.
 */
@Test
public void testXMLNoNsAwareStreamResult2() {
    try {
        // Create SAX parser *without* enabling ns
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(false); // Same as default
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        SAXParser sp = spf.newSAXParser();

        // Make sure that the output is well formed
        String xml = runTransform(sp);
        checkWellFormedness(xml);
    } catch (Throwable ex) {
        Assert.fail(ex.toString());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:Bug6594813.java

示例8: main

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
    System.setSecurityManager(new SecurityManager());
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        SAXParser sp = spf.newSAXParser();
        // The following line shouldn't throw a
        // java.security.AccessControlException.
        sp.setProperty("foo", "bar");
    } catch (SAXNotRecognizedException e) {
        // Ignore this expected exception.
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:15,代码来源:Bug6359330.java

示例9: testWithTrueFalse

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的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 SAXParser with these conditions.
 *
 * @throws Exception If any errors occur.
 */
public void testWithTrueFalse() throws Exception {
    String outputFile = USER_DIR + "SPNSTableTF.out";
    String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
    String xmlFile = XML_DIR + "namespace1.xml";
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
        spf.newSAXParser().parse(new File(xmlFile), handler);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:SAXParserNSTableTest.java

示例10: buildMappedDocument

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private static Document buildMappedDocument(final InputSource source) throws SAXException, IOException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);

    final Document result;
    try {
        final SAXParser sp = factory.newSAXParser();
        result = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        sp.parse(source, new MappedDocumentBuilder(result));
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("Invalid Parser Configuration", e);
    }
    return result;
}
 
开发者ID:AlexanderBartash,项目名称:hybris-integration-intellij-idea-plugin,代码行数:15,代码来源:ValidateContextImpl.java

示例11: getXMLReader

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
/**
 * Returns an instance of XMLReader with a catalog if one is provided.
 *
 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
 * through the factory
 * @param useCatalog the value of USE_CATALOG
 * @param catalog a catalog
 * @return an instance of XMLReader
 * @throws ParserConfigurationException
 * @throws SAXException
 */
XMLReader getXMLReader(boolean setUseCatalog, boolean useCatalog, String catalog)
        throws ParserConfigurationException, SAXException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    XMLReader reader = spf.newSAXParser().getXMLReader();
    if (setUseCatalog) {
        reader.setFeature(XMLConstants.USE_CATALOG, useCatalog);
    }
    reader.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
    return reader;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:CatalogSupportBase.java

示例12: parse

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
@NonNull
private Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom)
        throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(NAMESPACE_FEATURE, true);
        factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
        factory.setFeature(PROVIDE_XMLNS_URIS, true);
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setProperty(
                "http://xml.org/sax/properties/lexical-handler",
                handler
        );
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            xml = xml.replaceFirst("^([\\W]+)<","<");  //$NON-NLS-1$ //$NON-NLS-2$
            return parse(xml, new InputSource(new StringReader(xml)), false);
        }
        throw e;
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:29,代码来源:PositionXmlParser.java

示例13: parse

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
private void parse(URL file, TrackingHandler handler) {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        URLConnection connection = file.openConnection();
        connection.connect();
        this.lastModified = Math.max(this.lastModified, connection.getLastModified());
        saxParser.parse(connection.getInputStream(), handler);

    }
    catch (Exception ex) {
        throw new RuntimeException("Error parsing XML file " + handler.getLineNumberString(), ex);
    }
}
 
开发者ID:salesforce,项目名称:grammaticus,代码行数:16,代码来源:GrammaticalLabelFileParser.java

示例14: parseXMLFile

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public void parseXMLFile(String url) {
	SAXParserFactory spf = SAXParserFactory.newInstance();
	try {
		Scanner sc = new Scanner(new File(url), "UTF-8");
		
		while(sc.hasNextLine())
		{
			sc.nextLine().trim().replaceFirst("^([\\W]+)<","<");
			break;
		}

	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	try {
		//get a new instance of parser
		SAXParser sp = spf.newSAXParser();

		//parse the file and also register this class for call backs
		sp.parse(url, this);


	}catch(SAXException se) {
		se.printStackTrace();
	}catch(ParserConfigurationException pce) {
		pce.printStackTrace();
	}catch (IOException ie) {
		ie.printStackTrace();
	}
}
 
开发者ID:iedadata,项目名称:geomapapp,代码行数:32,代码来源:ShipControl.java

示例15: parse

import javax.xml.parsers.SAXParserFactory; //导入方法依赖的package包/类
public static ArrayList<LogEvent> parse(Reader reader, boolean cleanup) throws Exception {
    // Create the XML input factory
    SAXParserFactory factory = SAXParserFactory.newInstance();

    // Create the XML LogEvent reader
    SAXParser p = factory.newSAXParser();

    if (cleanup) {
        // some versions of the log have slightly malformed XML, so clean it
        // up before passing it to SAX
        reader = new LogCleanupReader(reader);
    }

    LogParser log = new LogParser();
    p.parse(new InputSource(reader), log);

    // Associate compilations with their NMethods
    for (NMethod nm : log.nmethods.values()) {
        Compilation c = log.compiles.get(nm.getId());
        nm.setCompilation(c);
        // Native wrappers for methods don't have a compilation
        if (c != null) {
            c.setNMethod(nm);
        }
    }

    // Initially we want the LogEvent log sorted by timestamp
    Collections.sort(log.events, sortByStart);

    return log.events;
}
 
开发者ID:arodchen,项目名称:MaxSim,代码行数:32,代码来源:LogParser.java


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