XML: Exemple d’utilisation de ‘XMLStreamWriter’ pour créer/écrire un document XML

Author:


{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);

    writer.writeStartDocument("1.0");

    writer.writeStartElement("Titre");

    writer.writeStartElement("Police");
    writer.writeAttribute("id", "1");

    writer.writeStartElement("Nom");
    writer.writeCharacters("Helvetica");
    writer.writeEndElement();

    writer.writeStartElement("Taille");
    writer.writeCharacters("33");
    writer.writeEndElement();

    writer.writeEndElement();
    writer.writeEndDocument();

    writer.flush();
    writer.close();
  }
}

Leave a Reply

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