Write xml with Stax, examples for CURSOR and ITERATOR api
If you are familiar with SAX it will be much easier to understand
To choose between them you can visit (bottom of the page)
http://docs.oracle.com/javase/tutorial/jaxp/stax/api.html
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 |
public class JavaStaxWriteEvent { public static void main(String[] args) { try { XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory(); // output stream FileOutputStream output = new FileOutputStream("test.xml"); // Write to XML stream XMLEventWriter eventWriter = xmlOutputFactory.createXMLEventWriter(output); // create XMLEvents XMLEventFactory eventFactory = XMLEventFactory.newFactory(); // write start element eventWriter.add(eventFactory.createStartElement("", "", "product")); // write attribute eventWriter.add(eventFactory.createAttribute("id", "1000")); // write name element eventWriter.add(eventFactory.createStartElement("", "", "name")); // write characters(text) eventWriter.add(eventFactory.createCharacters("XML")); // write end of name element eventWriter.add(eventFactory.createEndElement("", "", "name")); // write end of product element eventWriter.add(eventFactory.createEndElement("", "", "product")); eventWriter.flush(); eventWriter.close(); } catch (XMLStreamException ex) { } catch (FileNotFoundException ex) { } } } |
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 |
public class JavaStaxWrite { public static void main(String[] args) { try { FileOutputStream outputStream = new FileOutputStream("test.xml"); XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream); xmlWriter = new IndentingXMLStreamWriter(xmlWriter); // write the start element product xmlWriter.writeStartElement("product"); // write attribute xmlWriter.writeAttribute("id", "100"); // write name element xmlWriter.writeStartElement("name"); // write characters(text) xmlWriter.writeCharacters("XML"); // write end of name element xmlWriter.writeEndElement(); // write end of product element xmlWriter.writeEndElement(); xmlWriter.flush(); xmlWriter.close(); } catch (FileNotFoundException ex) { } catch (XMLStreamException ex) { } } } |