Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: Créer un document DOM vide

Java: Créer un document DOM vide
{filelink=8375}

/***** Code de MesExemples.com *******/
/**
 * @(#)CreateDomDocument.java
 *
 *
 * @author 
 *sakoba(java.mesexemples.com) @version 1.00 2013/7/4
 */
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.w3c.dom.Document;
 
public class CreateDomDocument {
  public static Document newEmptyDocument() {
    DocumentBuilderFactory factory = null;
    DocumentBuilder builder = null;
    Document ret;
 
    try {
      factory = DocumentBuilderFactory.newInstance();
      builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    }
 
    ret = builder.newDocument();
 
    return ret;
  }
}

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>