Java XML: Lire un fichier XML avec InputStream*

Author:

Java XML: Lire un fichier XML avec InputStream*
{filelink=8385}

/***** Code de MesExemples.com *******/
 /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */import java.io.IOException;import java.io.InputStream;import java.io.StringReader;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.stream.StreamSource;import org.w3c.dom.Document;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * Few simple utils to read DOM. This is originally from the Jakarta Commons * Modeler. *  * @author Costin Manolache */public class Utils {  /**   * Read XML as DOM.   */  public static Document readXml(InputStream is) throws SAXException, IOException,      ParserConfigurationException {      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();      dbf.setValidating(false);      dbf.setIgnoringComments(false);      dbf.setIgnoringElementContentWhitespace(true);      dbf.setNamespaceAware(true);      // dbf.setCoalescing(true);      // dbf.setExpandEntityReferences(true);      DocumentBuilder db = null;      db = dbf.newDocumentBuilder();      db.setEntityResolver(new NullResolver());      // db.setErrorHandler( new MyErrorHandler());      return db.parse(is);  }}class NullResolver implements EntityResolver {  public InputSource resolveEntity(String publicId, String systemId) throws SAXException,      IOException {    return new InputSource(new StringReader(""));  }}     

Code testé avec le fichier XML Suivant




	Sakoba
	Adams
	Rappel
	Ne m'oubliez pas ce week-end!

A Voir sur le même Sujet:

  • comment gérer le format date java d'un jtable vers un jtextfield

Leave a Reply

Your email address will not be published. Required fields are marked *

Java XML: Lire un fichier XML avec InputStream*

Author:

Java XML: Lire un fichier XML avec InputStream*
{filelink=8385}

/***** Code de MesExemples.com *******/

/** * Licensed to the Apache Software Foundation (ASF) under one 
* or more contributor license agreements. See the NOTICE file 
* distributed with this work for additional information 
* regarding copyright ownership. The ASF licenses this file 
* to you under the Apache License, Version 2.0 (the 
* "License"); you may not use this file except in compliance 
* with the License. You may obtain a copy of the License at 
* * http://www.apache.org/licenses/LICENSE-2.0 
* * Unless required by applicable law or agreed to in writing, 
* software distributed under the License is distributed on an 
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
* KIND, either express or implied. See the License for the 
* specific language governing permissions and limitations 
* under the License. 
*/

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** * Few simple utils to read DOM. This is originally from the Jakarta Commons 
* Modeler. 
*  * @author Costin Manolache 
*/

public class Utils 
  { 
      /**   * Read XML as DOM.   */  
      public static Document readXml(InputStream is) throws SAXException, IOException,      
      	ParserConfigurationException 
      	  {      
      	  	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();      
      	  	dbf.setValidating(false);      dbf.setIgnoringComments(false);      
      	  	dbf.setIgnoringElementContentWhitespace(true);      
      	  	dbf.setNamespaceAware(true);      // dbf.setCoalescing(true);      
      	  	// dbf.setExpandEntityReferences(true);      
      	  	DocumentBuilder db = null;      
      	  	db = dbf.newDocumentBuilder();      
      	  	db.setEntityResolver(new NullResolver());      // db.setErrorHandler( new MyErrorHandler());      
      	  	return db.parse(is); 
      	   }
      }
      class NullResolver implements EntityResolver {  
      		public InputSource resolveEntity(String publicId, String systemId) throws SAXException,  IOException {    
      		return new InputSource(new StringReader(""));  
      		}
      		} 

Code testé avec le fichier XML Suivant




	Sakoba
	Adams
	Rappel
	Ne m'oubliez pas ce week-end!

A Voir sur le même Sujet:

  • base de donne en xml
  • comment lire fichier yml

Leave a Reply

Your email address will not be published. Required fields are marked *