Java XML: Lire un fichier XML avec InputStream*

Author:

Java XML: Lire un fichier XML avec InputStream*
Download

/***** 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

<?xml version="1.0" encoding="windows-1252"?>
<!-- Edited by MesEXemple.com -->
<note>
	<to>Sakoba</to>
	<from>Adams</from>
	<heading>Rappel</heading>
	<body>Ne m'oubliez pas ce week-end!</body>
</note>

Cet article Java XML: Lire un fichier XML avec InputStream* est apparu en premier sur .