1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| package com.datalion.exogema.application;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class Document
{
private String ENCODING="utf-8";
private String VERSION="1.0";
private FileOutputStream output; // <-- le flux de sortie
private XMLStreamWriter writer; // <-- l'écrivain
private String NS="http://datalion.eu"; // <-- le namespace
public Document(String URL)
{
System.out.println("document creation");
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
try
{
this.output = new FileOutputStream(URL);
this.writer = outputFactory.createXMLStreamWriter(output);
this.writer.writeStartDocument(this.ENCODING, this.VERSION);
this.writer.writeStartDocument();
this.writer.setPrefix("tns",this.NS);
this.writer.setDefaultNamespace(this.NS);
this.writer.writeStartElement(this.NS,"enquiry");
this.writer.writeNamespace("dtl",this.NS);
}
catch(XMLStreamException e1)
{
e1.printStackTrace();
}
catch(IOException e2)
{
e2.printStackTrace();
}
}
public void add(Question q)
{
try
{
this.writer.writeStartElement(this.NS,"question");
this.writer.writeAttribute("group", q.getGroup());
this.writer.writeAttribute("id",String.valueOf(q.getIdentifiant()));
this.writer.writeStartElement(this.NS,"title");
this.writer.writeCharacters(q.getTitle());
this.writer.writeEndElement();
this.writer.writeStartElement(this.NS,"choice");
this.writer.writeCharacters(q.getChoiceType());
for(int i=0;i<q.getPossibilities().size();i++)
{
this.writer.writeStartElement(this.NS,"possibility");
this.writer.writeCharacters((q.getPossibilities().get(i).toString()));
this.writer.writeEndElement();
}
this.writer.flush();
}
catch(XMLStreamException e)
{
e.printStackTrace();
}
}
/** Méthode permettant de déplacer une question */
public void modify(int id)
{
}
/** Méthode permettant de détruire une question */
public void remove(int id)
{
}
public void close()
{
try
{
this.writer.writeEndElement();
this.writer.writeEndDocument();
this.writer.flush();
this.writer.close();
}
catch (XMLStreamException e)
{
e.printStackTrace();
}
}
} |
Partager