javax.xml.parsers.SAXParserFactory#newInstance ( )源码实例Demo

下面列出了javax.xml.parsers.SAXParserFactory#newInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: Parser.java
private XMLReader createReader() throws SAXException {
    try {
        SAXParserFactory pfactory = SAXParserFactory.newInstance();
        pfactory.setValidating(true);
        pfactory.setNamespaceAware(true);

        // Enable schema validation
        SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
        pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));

        return pfactory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException ex) {
        throw new SAXException(ex);
    }
}
 
源代码2 项目: freeacs   文件: UnittypeXML.java
@SuppressWarnings("rawtypes")
public void load(String file_input) throws Exception {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setXIncludeAware(true);
  factory.setNamespaceAware(false);
  factory.setValidating(false);

  SAXParser parser = factory.newSAXParser();

  SAXReader reader = new SAXReader(parser.getXMLReader());
  Document document = reader.read(file_input);

  info = new Info();
  info.load(document.selectSingleNode("//unittype/info"));

  Enums enums = new Enums();
  enums.load(document.selectSingleNode("//unittype/parameters/enums"));

  parameters = new Parameters();
  List parameters_nodes = document.selectNodes("//unittype/parameters");
  for (Object parameters_node : parameters_nodes) {
    Node parameter_node = (Node) parameters_node;
    parameters.load(parameter_node, enums);
  }
}
 
源代码3 项目: phrasal   文件: TMXExtractor.java
static public void main(String[] args) throws Exception {
  if (args.length < 3) {
    usage();
    exit(-1);
  }
  
  String tmxFn = args[0];
  String outputPrefixFn = args[1];
  Set<String> langs = new HashSet<String>();
  for (int i = 2; i < args.length; i++) {
    langs.add(args[i]);
  }
  SAXParserFactory saxpf = SAXParserFactory.newInstance();
  SAXParser saxParser = saxpf.newSAXParser();
  saxParser.parse(new File(tmxFn), new TMXExtractor(outputPrefixFn, langs));        
}
 
源代码4 项目: AndrOBD   文件: PvXMLHandler.java
/**
 * main routine for testing class implementation
 *
 * @param argv command line arguments
 */
public static void main(String[] argv)
{
	try
	{
		// Create a new Parser
		PvXMLHandler handler = new PvXMLHandler();
		// Use the default (non-validating) parser
		SAXParserFactory factory = SAXParserFactory.newInstance();
		// Parse the input
		SAXParser saxParser = factory.newSAXParser();
		saxParser.parse(new FileInputStream(argv[0]), handler);
	} catch (Exception e)
	{
		e.printStackTrace();
	}
}
 
源代码5 项目: jdk1.8-source-analysis   文件: Catalog.java
/**
 * Setup readers.
 */
public void setupReaders() {
  SAXParserFactory spf = catalogManager.useServicesMechanism() ?
                  SAXParserFactory.newInstance() : new SAXParserFactoryImpl();
  spf.setNamespaceAware(true);
  spf.setValidating(false);

  SAXCatalogReader saxReader = new SAXCatalogReader(spf);

  saxReader.setCatalogParser(null, "XMLCatalog",
                             "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");

  saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,
                             "catalog",
                             "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");

  addReader("application/xml", saxReader);

  TR9401CatalogReader textReader = new TR9401CatalogReader();
  addReader("text/plain", textReader);
}
 
源代码6 项目: stendhal   文件: GroupsXMLLoader.java
/**
 * Load and returns the list of files.
 *
 * @param in
 *            The config file stream.
 * @return list of group entries
 *
 * @throws SAXException
 *             If a SAX error occurred.
 * @throws IOException
 *             If an I/O error occurred.
 */
protected List<URI> load(final InputStream in) throws SAXException, IOException {
	SAXParser saxParser;

	// Use the default (non-validating) parser
	final SAXParserFactory factory = SAXParserFactory.newInstance();
	try {
		saxParser = factory.newSAXParser();
	} catch (final ParserConfigurationException ex) {
		throw new SAXException(ex);
	}

	// Parse the XML
	groups = new LinkedList<URI>();
	saxParser.parse(in, this);
	return groups;
}
 
源代码7 项目: java   文件: SAXParserDemo2.java
public static void main(String[] args) throws Exception {
  SAXParserFactory parserFactor = SAXParserFactory.newInstance();
  SAXParser parser = parserFactor.newSAXParser();
  SAXHandler handler = new SAXHandler();
  parser.parse(ClassLoader.getSystemResourceAsStream("Student.xml"), 
               handler);
}
 
源代码8 项目: mzmine3   文件: UserParameterOpenHandler_2_3.java
/**
 * Load the user parameters
 */
@Override
public void readUserParameters(InputStream inputStream)
    throws IOException, ParserConfigurationException, SAXException {

  logger.info("Loading user parameters");
  charBuffer = new StringBuffer();

  // Parse the XML file
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser saxParser = factory.newSAXParser();
  saxParser.parse(inputStream, this);

}
 
源代码9 项目: openjdk-jdk8u   文件: WhitespacesTest.java
@Test(dataProvider = "TestSchemaFiles")
public void testWhitespacesCollapse(String schemaFile) throws Exception {
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    XsomErrorHandler eh = new XsomErrorHandler();

    parser.setErrorHandler(eh);
    parser.parse(getSchemaFile(schemaFile));

    if (eh.gotError) {
        throw new RuntimeException("XSOM parser encountered error", eh.e);
    }
}
 
public DefenseInboundAdapter(AdapterDefinition definition) throws ComponentException, ParserConfigurationException, SAXException, IOException
{
	super(definition);
	messageParser = new MessageParser(this);
	saxFactory = SAXParserFactory.newInstance();
	saxParser = saxFactory.newSAXParser();
}
 
源代码11 项目: jdk8u-jdk   文件: Bug6359330.java
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.
    }
}
 
源代码12 项目: trekarta   文件: XMLReaderAdapter.java
public void parse(DefaultHandler handler, InputStream is) throws ParserConfigurationException, SAXException, IOException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);

    XMLReader xmlReader = factory.newSAXParser().getXMLReader();
    xmlReader.setContentHandler(handler);
    xmlReader.parse(new InputSource(is));
}
 
源代码13 项目: difido-reports   文件: TrxBinder.java
@Override
public void process(File source) throws Exception {
	zeroTime = TIME_MILLIS_FORMAT.parse("00:00:00.000");
	SAXParserFactory factory = SAXParserFactory.newInstance();
	SAXParser saxParser = factory.newSAXParser();
	saxParser.parse(source, this);
}
 
源代码14 项目: stendhal   文件: SpellXMLLoader.java
public Collection<DefaultSpell> load(URI uri) throws SAXException {
	loadedSpells = new LinkedList<DefaultSpell>();
	// Use the default (non-validating) parser
	final SAXParserFactory factory = SAXParserFactory.newInstance();
	try {
		// Parse the input
		final SAXParser saxParser = factory.newSAXParser();

		final InputStream is = SpellXMLLoader.class.getResourceAsStream(uri.getPath());

		if (is == null) {
			throw new FileNotFoundException("cannot find resource '" + uri
					+ "' in classpath");
		}
		try {
			saxParser.parse(is, this);
		} finally {
			is.close();
		}
	} catch (final ParserConfigurationException t) {
		logger.error(t);
	} catch (final IOException e) {
		logger.error(e);
		throw new SAXException(e);
	}
	return loadedSpells;
}
 
源代码15 项目: jdk8u60   文件: CLDRConverter.java
static Map<String, Object> getCLDRBundle(String id) throws Exception {
    Map<String, Object> bundle = cldrBundles.get(id);
    if (bundle != null) {
        return bundle;
    }
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser parser = factory.newSAXParser();
    enableFileAccess(parser);
    LDMLParseHandler handler = new LDMLParseHandler(id);
    File file = new File(SOURCE_FILE_DIR + File.separator + id + ".xml");
    if (!file.exists()) {
        // Skip if the file doesn't exist.
        return Collections.emptyMap();
    }

    info("..... main directory .....");
    info("Reading file " + file);
    parser.parse(file, handler);

    bundle = handler.getData();
    cldrBundles.put(id, bundle);
    String country = getCountryCode(id);
    if (country != null) {
        bundle = handlerSuppl.getData(country);
        if (bundle != null) {
            //merge two maps into one map
            Map<String, Object> temp = cldrBundles.remove(id);
            bundle.putAll(temp);
            cldrBundles.put(id, bundle);
        }
    }
    return bundle;
}
 
源代码16 项目: openjdk-jdk9   文件: SAXParserFactTest.java
/**
 * Test the functionality of setNamespaceAware and
 * isNamespaceAware methods.
 */
@Test
public void testNamespace02() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    assertTrue(spf.isNamespaceAware());
}
 
源代码17 项目: hadoop   文件: XmlRecordInput.java
/** Creates a new instance of XmlRecordInput */
public XmlRecordInput(InputStream in) {
  try{
    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;
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
}
 
源代码18 项目: TencentKona-8   文件: XmlUtil.java
@Override
protected SAXParserFactory initialValue() throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    return factory;
}
 
源代码19 项目: localization_nifi   文件: TestSplitXml.java
@Before
public void setUp() throws Exception {
    factory = SAXParserFactory.newInstance();
    saxParser = factory.newSAXParser();
}
 
源代码20 项目: org.hl7.fhir.core   文件: XmlParser.java
public Element parse(InputStream stream) throws Exception {
	Document doc = null;
	try {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// xxe protection
		factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
		factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
		factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
		factory.setXIncludeAware(false);
		factory.setExpandEntityReferences(false);

		factory.setNamespaceAware(true);
		if (policy == ValidationPolicy.EVERYTHING) {
			// use a slower parser that keeps location data
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer nullTransformer = transformerFactory.newTransformer();
			DocumentBuilder docBuilder = factory.newDocumentBuilder();
			doc = docBuilder.newDocument();
			DOMResult domResult = new DOMResult(doc);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			spf.setNamespaceAware(true);
			spf.setValidating(false);
			SAXParser saxParser = spf.newSAXParser();
			XMLReader xmlReader = saxParser.getXMLReader();
			// xxe protection
			spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
			spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
			xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
			xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

			XmlLocationAnnotator locationAnnotator = new XmlLocationAnnotator(xmlReader, doc);
			InputSource inputSource = new InputSource(stream);
			SAXSource saxSource = new SAXSource(locationAnnotator, inputSource);
			nullTransformer.transform(saxSource, domResult);
		} else {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(stream);
		}
	} catch (Exception e) {
		logError(0, 0, "(syntax)", IssueType.INVALID, e.getMessage(), IssueSeverity.FATAL);
		doc = null;
	}
	if (doc == null)
		return null;
	else
		return parse(doc);
}