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 92 93 94 95 96
| package sax.converter;
import org.xml.sax.*;
import org.xml.sax.helpers.LocatorImpl;
import java.io.*;
public class SAXContentHandlerMMToFM implements ContentHandler {
DataOutputStream fs;
public SAXContentHandlerMMToFM() {
super();
// On definit le locator par defaut.
locator = new LocatorImpl();
try
{
fs = new DataOutputStream ( new FileOutputStream("c:/Formation/xmlSaxOut.xml") );
}
catch (IOException e) { System.out.println(e); }
}
public void setDocumentLocator(Locator value) {
locator = value;
}
public void startDocument() throws SAXException {
try
{
fs.writeChars("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fs.writeChars("<map version=\"0.8.0\">\n");
}
catch (IOException e) { System.out.println(e); }
}
public void endDocument() throws SAXException {
try
{
fs.writeChars("</map>");
fs.close();
}
catch (IOException e) { System.out.println(e); }
}
public void startPrefixMapping(String prefix, String URI) throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {}
public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributs) throws SAXException {
try
{
if (localName == "Topic")
{
fs.writeChars("<node POSITION=\"right\" ");
}
else if (localName == "Text")
{
fs.writeChars("TEXT=\"");
for (int index = 0; index < attributs.getLength(); index++) { // on parcourt la liste des attributs
if (attributs.getLocalName(index) == "PlainText")
{
fs.writeChars(attributs.getValue(index) + "\">");
}
}
}
}
catch (IOException e) { System.out.println(e); }
}
public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException {
try
{
if (localName == "Topic")
{
fs.writeChars("</node>\n");
}
else if (localName == "Text")
{}
}
catch (IOException e) { System.out.println(e); }
}
public void characters(char[] ch, int start, int end) throws SAXException {}
public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException {}
public void processingInstruction(String target, String data) throws SAXException {}
public void skippedEntity(String arg0) throws SAXException {
// Je ne fais rien, ce qui se passe n'est pas franchement normal.
// Pour eviter cet evenement, le mieux est quand meme de specifier une dtd pour vos
// documents xml et de les faire valider par votre parser.
}
private Locator locator;
} |
Partager