Write file XML – Event Iterator API


Bài trước chúng ta biết cách viết read file XML như thế nào, thì bài này hướng dẫn chúng ta cách tạo ra file xml.

đây là kết quả khi chúng ta thực hiện ok

d1

Chúng ta viết class WriteXmlDocument như sau , ở đây mình thao tác tạo 2 item của products.


package thaihoanghai;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class WriteXmlDocument {
 private String configFile;

public void setFile(String configFile) {
 this.configFile = configFile;
 }

public void saveConfig() throws Exception {
 // Create a XMLOutputFactory
 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
 // Create XMLEventWriter
 XMLEventWriter eventWriter = outputFactory
 .createXMLEventWriter(new FileOutputStream(configFile));
 // Create a EventFactory
 XMLEventFactory eventFactory = XMLEventFactory.newInstance();
 XMLEvent end = eventFactory.createDTD("\n");
 XMLEvent tab = eventFactory.createDTD("\t");
 // Create and write Start Tag
 StartDocument startDocument = eventFactory.createStartDocument();
 eventWriter.add(startDocument);

// Create config open tag
 eventWriter.add(end);
 StartElement configStartElement = eventFactory.createStartElement("","", "products");
 eventWriter.add(configStartElement);
 eventWriter.add(end);

// Create Item open tag
 eventWriter.add(tab);
 StartElement ItemStartElement = eventFactory.createStartElement("","", "item");
 eventWriter.add(ItemStartElement);
 eventWriter.add(end);

// Write the different nodes

createNodeChild(eventWriter, "id", "SP001");
 createNodeChild(eventWriter, "name", "Iphone 5");
 createNodeChild(eventWriter, "price", "$5000");
 createNodeChild(eventWriter, "unit", "1");

eventWriter.add(tab);
 eventWriter.add(eventFactory.createEndElement("", "", "item"));
 eventWriter.add(end);

//===========================================================
 // Create Item open tag
 eventWriter.add(tab);
 StartElement ItemStartElement1 = eventFactory.createStartElement("","", "item");
 eventWriter.add(ItemStartElement1);
 eventWriter.add(end);

// Write the different nodes

createNodeChild(eventWriter, "id", "SP002");
 createNodeChild(eventWriter, "name", "Iphone 4S");
 createNodeChild(eventWriter, "price", "$4000");
 createNodeChild(eventWriter, "unit", "1");

eventWriter.add(tab);
 eventWriter.add(eventFactory.createEndElement("", "", "item"));
 eventWriter.add(end);
 //==========================================================
 eventWriter.add(eventFactory.createEndElement("", "", "products"));
 eventWriter.add(end);

 

eventWriter.add(eventFactory.createEndDocument());
 eventWriter.close();
 }

private void createNodeChild(XMLEventWriter eventWriter, String name,
 String value) throws XMLStreamException {

XMLEventFactory eventFactory = XMLEventFactory.newInstance();
 XMLEvent end = eventFactory.createDTD("\n");
 XMLEvent tab = eventFactory.createDTD("\t\t");
 // Create Start node
 StartElement sElement = eventFactory.createStartElement("", "", name);
 eventWriter.add(tab);
 eventWriter.add(sElement);
 // Create Content
 Characters characters = eventFactory.createCharacters(value);
 eventWriter.add(characters);
 // Create End node
 EndElement eElement = eventFactory.createEndElement("", "", name);
 eventWriter.add(eElement);
 eventWriter.add(end);

}

}

Xong class Test chương trình :


public static void main(String[] args) {
 WriteXmlDocument configFile = new WriteXmlDocument();
 configFile.setFile("config2.xml");
 try {
 configFile.saveConfig();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

Hy vọng giúp ích cho các bạn

Nguồn tham khảo : http://www.vogella.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: