/***** Code de MesExemples.com *******/ /** * @(#)XMLReadDOM.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/5 */ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; public class XMLReadDOM { public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); // Lire la structure du fichier xml "test.xml" Document document = loader.parse("test.xml"); Element tree = document.getDocumentElement(); } } |
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> |
A Voir sur le même Sujet
-
Javascript: Les différentes manières de créer une date
{filelink=49} // Créer une date sans paramètre var dt1 = new Date( ); document.writeln(“”+dt1); // Créer une date à partir d’un String var dt2 = …
-
Attributs des fichiers: Exemple de Fichier en mode lecture seule.
{filelink=866} import java.io.File; import java.io.IOException; public class FichierModeEcriture { public static void main (String[] args) throws IOException { // Créer un fichier en mode ecriture …
-
Java: Comment ignorer les commentaires pendant la lecture d’un fichier XML
{filelink=8343} /***** Code de MesExemples.com *******/ /** * @(#)IgnorerCommentaire.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import java.io.File; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public …
-
Java XML: Construire un arbre à partir des événements SAX
{filelink=8732} /***** Code de MesExemples.com *******/ /*Java Swing, 2nd EditionBy Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian ColeISBN: 0-596-00408-7Publisher: O’Reilly */// VSX.java//A simple XML parser that builds a tree from SAX events. //import java.io.File;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreeModel;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class VSX { public TreeModel parse(String filename) { SAXParserFactory factory = SAXParserFactory.newInstance(); XMLTreeHandler handler = new XMLTreeHandler(); try { // Parse the input. SAXParser saxParser = factory.newSAXParser(); saxParser.parse(new File(filename), handler); } catch (Exception e) { System.err.println(“File Read Error: ” + e); e.printStackTrace(); return new DefaultTreeModel(new DefaultMutableTreeNode(“error”)); } return new DefaultTreeModel(handler.getRoot()); } public static class XMLTreeHandler extends DefaultHandler { private DefaultMutableTreeNode root, currentNode; public DefaultMutableTreeNode getRoot() { return root; } // SAX Parser Handler methods… public void startElement(String namespaceURI, String lName, String qName, Attributes attrs) throws SAXException { String eName = lName; // Element name if (“”.equals(eName)) eName = qName; Tag t = new Tag(eName, attrs); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(t); if (currentNode == null) { root = newNode; } else { // Must not be the root node… currentNode.add(newNode); } currentNode = newNode; } public void endElement(String namespaceURI, String sName, String qName) throws SAXException { currentNode = (DefaultMutableTreeNode) currentNode.getParent(); } public void characters(char buf[], int offset, int len) throws SAXException { String s = new String(buf, offset, len).trim(); ((Tag) currentNode.getUserObject()).addData(s); } } public static class Tag { private String name; private String data; private Attributes attr; public Tag(String n, Attributes a) { name = n; attr = a; } public String getName() { return name; } public Attributes getAttributes() { return attr; } public void setData(String d) { data = d; } public String getData() { return data; } public void addData(String d) { if (data == null) { setData(d); } else { data += d; } } public String getAttributesAsString() { StringBuffer buf = new StringBuffer(256); for (int i = 0; i
-
Java-source: Exemple d’utilisation de PushbackInputStream
{filelink=351} /***** Code de MesExemples.com *******/ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PushbackInputStream; class ExemplePushbackInputStreamEnJava { public static void main(String[] args) { String strExpression = “a …
-
Java: Créer un nouveau document à partir d’un objet InputStream*
{filelink=8377} /***** Code de MesExemples.com *******/ /** * @(#)DomDocInputStream.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import …
-
Java: Créer un nouveau document à partir d’un objet InputStream*
{filelink=8377} /***** Code de MesExemples.com *******/ /** * @(#)DomDocInputStream.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/7/4 */ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import …
-
Lecture clavier: Utiliser la classe ‘Scanner’ pour lire les entrées standards(Clavier)
{filelink=5703} import java.util.Scanner; /* À partir de JDK 1.5 Lire les caractères tapés sur le clavier */ public class ClavierAvecScanner { public static void main(String[] …
-
JSP: Configuration et lecture des cookies
Réglage et lecture des Cookies
-
Java PDF: Exemple d’utilisation des polices dans le document PDF
{filelink=10409} /***** Code de MesExemples.com *******/ /** * @(#)PDFExemple.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/4/29 */ import java.io.FileOutputStream; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import …
-
Java: Obtenir les éléments d’un document à partir d’un fichier*
{filelink=8381} /***** Code de MesExemples.com *******/ /* * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * “This product includes software developed by the * Apache Software Foundation (http://www.apache.org/).” * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names “The Jakarta Project”, “Ant”, and “Apache Software * Foundation” must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called “Apache” * nor may “Apache” appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED “AS IS” AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;/** * * @author Abey Mullassery * */public class Main { public static Element getDocumentElement(File fileName) throws Exception { try { Document doc = getDocumentBuilder().parse(fileName); return doc.getDocumentElement(); } catch (Exception se) { return null; } } /** * Returns a default DocumentBuilder instance or throws an * ExceptionInInitializerError if it can’t be created. * * @return a default DocumentBuilder instance. */ public static DocumentBuilder getDocumentBuilder() throws Exception { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringComments(true); dbf.setCoalescing(true); dbf.setIgnoringElementContentWhitespace(true); dbf.setValidating(false); return dbf.newDocumentBuilder(); } catch (Exception exc) { throw new Exception(exc.getMessage()); } }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Java PDF: Exemple de rotation des images
{filelink=10460} /***** Code de MesExemples.com *******/ /** * @(#)ExempleRotationImagePDF.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/4/30 */ import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import …
-
Java PDF: Exemple de rotation des image
{filelink=10460} /***** Code de MesExemples.com *******/ /** * @(#)ExempleRotationImagePDF.java * * * @author *sakoba(java.mesexemples.com) @version 1.00 2013/4/30 */ import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import …
-
Java: Exemple d’utilisation des code-barres avec PDF
{filelink=10332} /***** Code de MesExemples.com *******/ import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.BarcodePDF417; import com.itextpdf.text.pdf.PdfWriter; public class CodeBarrePDF { public static void …
-
Créer un fichier en mode lecture et écriture
{filelink=7621} import java.io.File; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Fichiers_Permission { public static void main(String[] argv) throws Exception { File fichier = new …
Cet article Java XML: Exemple de lecture d’un arbre DOM à partir d’un document XML est apparu en premier sur .