/***** Code de MesExemples.com *******/ import javax.xml.stream.XMLOutputFactory;import javax.xml.stream.XMLStreamWriter;public class ExempleWriteStartDocument { public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out); writer.writeStartDocument("1.0"); writer.writeStartElement("personne"); writer.writeStartElement("nom"); writer.writeStartElement("prenom"); writer.writeCharacters("X5"); writer.writeEndDocument(); writer.flush(); }} |
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
-
JSP: Utilisation des opérateurs logiques
Utilisation de condition Utilisation des opérateurs logique
-
JSP Boucle for: utilisation de deux index
Utilisation des index dans une boucle Exemples des index
-
JSP: Utilisation du bloc try/catch
Exemple d’utilisation try/catch Block Utilisation du bloc try/catch
-
XML: Exemple d’utilisation de ‘XMLStreamWriter’ pour créer/écrire un document XML
{filelink=979} import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class ExempleXMLStreamWriter { public static void main(String[] args) throws Exception { XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = factory.createXMLStreamWriter(System.out); …
-
Java XML: Exemple des flux d’entrée et de Sortie de XML
{filelink=8760} /***** Code de MesExemples.com *******/ /* Copyright 2004 The Apache Software Foundation * * Licensed 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. */// Revised from xmlbeansimport javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLStreamWriter;import javax.xml.stream.events.XMLEvent;public final class XmlReaderToWriter { private XmlReaderToWriter() { } public static void writeAll(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { while (xmlr.hasNext()) { write(xmlr, writer); xmlr.next(); } write(xmlr, writer); // write the last element writer.flush(); } public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { switch (xmlr.getEventType()) { case XMLEvent.START_ELEMENT: final String localName = xmlr.getLocalName(); final String namespaceURI = xmlr.getNamespaceURI(); if (namespaceURI != null && namespaceURI.length() > 0) { final String prefix = xmlr.getPrefix(); if (prefix != null) writer.writeStartElement(prefix, localName, namespaceURI); else writer.writeStartElement(namespaceURI, localName); } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i
-
Java xerces: Utilisation de XMLSerializer pour sérialiser la sortie
{filelink=8704} /***** Code de MesExemples.com *******/ import org.w3c.dom.Document;import org.apache.xerces.dom.DOMImplementation;import org.w3c.dom.Element;import org.apache.xml.serialize.XMLSerializer;import java.io.IOException;public class MainClass { public static void main(String args[]) throws IOException { Document dom = DOMImplementation.createDocument(null, null, null); Element root = dom.createElement(“A”); Element child1 = dom.createElement(“B”); child1.appendChild(dom.createTextNode(“C”)); child1.setAttribute(“A”, “a”); root.appendChild(child1); dom.appendChild(root); XMLSerializer serial = new XMLSerializer(System.out, null); serial.serialize(dom.getDocumentElement()); }} Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Exemple de création et d’utilisation d’une classe anonyme
{filelink=11047} public class Personne { // Classe Anonyme interface Action { void manger(); } public static void main(String[] argv) { // Utilisation de la classe …
-
Java: Utilisation de la classe ‘DocumentFragment’*
{filelink=8406} /***** Code de MesExemples.com *******/ //A Document Containing a Single Person Node/*]> Frank Fangston 555-3247 */ public void addFragment(Document doc) { Element person; Element root = doc.getDocumentElement(); DocumentFragment fragment = doc.createDocumentFragment(); person = makePersonNode(doc,”Fred”,”555-4927″); fragment.appendChild(person); person = makePersonNode(doc,”Sam”,”555-9832″); fragment.appendChild(person); root.appendChild(fragment); } private Element makePersonNode(Document doc,String name,String phone) { Element nameNode = doc.createElement(“name”); Text nametextNode = doc.createTextNode(name); nameNode.appendChild(nametextNode); Element phoneNode = doc.createElement(“phone”); Text phonetextNode = doc.createTextNode(phone); phoneNode.appendChild(phonetextNode); Element personNode = doc.createElement(“person”); personNode.appendChild(nameNode); personNode.appendChild(phoneNode); return(personNode); } Code testé avec le fichier XML Suivant Sakoba Adams Rappel Ne m’oubliez pas ce week-end!
-
Comment utiliser ‘XMLStreamWriter’ pour créer un fichier xml avec les éléments de HTML nameSpace
{filelink=984} import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStreamWriter; import javax.xml.namespace.QName; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class CreerXML { public static void main(String[] args) throws …
-
Utilisation de la méthode ‘isLeapYear’ de la classe ‘GregorianCalendar’
{filelink=5784} Pour verifier si une année est Bissextile on utilise la méthode ‘isLeapYear’ import java.util.Calendar; import java.util.GregorianCalendar; class AnneeBissextile { public static void main(String args[]) …
-
Javascript: Utilisation d’un champs d’aide caché
{filelink=860} .help { position: absolute; left: 300px; top: 20px; visibility: hidden; width: 100px; padding: 10px; border: 1px solid #f00; } form { margin: 20px; background-color: …
-
JSP: Utilisation de transaction dans la requête de mise à jour de la base de données
//web.xml jdbc/address javax.sql.DataSource Container Utilisation de transaction Modifier Un client INSERT INTO Clients (nom, prenom, adresse, pays, email) VALUES (?, ?, ?, ?, ?) SELECT * FROM Clients …
-
Utiliser la classe ‘Formatter’ pour écrire dans un fichier
{filelink=5779} import java.io.FileOutputStream; import java.util.Formatter; public class FormatterFichier { public static void main(String[] argv) throws Exception { Formatter fmtCon = new Formatter(System.out); Formatter fmtFile; fmtFile …
-
JSP boucle et: Exemple d’utilisation du mot clé « continue » en jsp
Utilisation des boucle dans une page JSP Utiliser le mot continue = -6; i–) { if (i == 0) continue; out.println(“L’inverse de ” + i + ” est ” + (1 / i) + “.”); } %>
Cet article Java XML: Utilisation du méthode writeStartDocument est apparu en premier sur .