SHARE
TWEET

Untitled

a guest Nov 19th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package eg.edu.alexu.csd.oop.XML;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.util.*;
  6.  
  7. import javax.xml.stream.XMLEventFactory;
  8. import javax.xml.stream.XMLEventWriter;
  9. import javax.xml.stream.XMLOutputFactory;
  10. import javax.xml.stream.XMLStreamException;
  11. import javax.xml.stream.events.Characters;
  12. import javax.xml.stream.events.EndElement;
  13. import javax.xml.stream.events.StartElement;
  14. import javax.xml.stream.events.XMLEvent;
  15.  
  16. public class StaxXMLWriter {
  17.  
  18.     public static void main(String[] args) {
  19.         String fileName = "C:\\Users\\hp\\Desktop\\College\\OOPAssignments\\DBMS\\OOP_TAs-oop-course-projects-b15818ce512d\\XML\\T.xml";
  20.         String rootElement = "Table";
  21.         StaxXMLWriter xmlWriter = new StaxXMLWriter();
  22.         Map<String,String> elementsMap = new LinkedHashMap<String, String>();
  23.         elementsMap.put("ID", "55510");
  24.         elementsMap.put("Status", "RIP");
  25.         elementsMap.put("Year", "750");
  26.         xmlWriter.writeXML(fileName, rootElement, elementsMap);
  27.     }
  28.  
  29.     public void writeXML(String fileName, String rootElement, Map<String, String> elementsMap){
  30.         XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
  31.         try {
  32.             XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(fileName), "UTF-8");
  33.             XMLEventFactory eventFactory = XMLEventFactory.newInstance();
  34.             XMLEvent end = eventFactory.createDTD("\n");
  35.             xmlEventWriter.add(end);
  36.             StartElement configStartElement = eventFactory.createStartElement("", "", rootElement);
  37.             xmlEventWriter.add(configStartElement);
  38.             xmlEventWriter.add(end);
  39.  
  40.             // Add Record
  41.             newRecord(xmlEventWriter,elementsMap);
  42.  
  43.             xmlEventWriter.add(eventFactory.createEndElement("", "", rootElement));
  44.             xmlEventWriter.add(end);
  45.             xmlEventWriter.add(eventFactory.createEndDocument());
  46.             xmlEventWriter.close();
  47.         } catch (FileNotFoundException | XMLStreamException e) {
  48.             e.printStackTrace();
  49.         }
  50.     }
  51.     public void newRecord(XMLEventWriter eventWriter, Map<String, String> elementsMap) throws XMLStreamException {
  52.         XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
  53.         XMLEvent end = xmlEventFactory.createDTD("\n");
  54.         XMLEvent tab = xmlEventFactory.createDTD("\t");
  55.         //Create Start node
  56.         StartElement sElement = xmlEventFactory.createStartElement("", "", "Row");
  57.         eventWriter.add(tab);
  58.         eventWriter.add(sElement);
  59.         eventWriter.add(end);
  60.         //Create Content
  61.         Set<String> elementsNodes = elementsMap.keySet();
  62.         for(String key : elementsNodes)
  63.             createNode(eventWriter, key, elementsMap.get(key));
  64.         // Create End node
  65.         EndElement eElement = xmlEventFactory.createEndElement("", "", "Row");
  66.         eventWriter.add(tab);
  67.         eventWriter.add(eElement);
  68.         eventWriter.add(end);
  69.     }
  70.     private static void createNode(XMLEventWriter eventWriter, String element, String value) throws XMLStreamException {
  71.         XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
  72.         XMLEvent end = xmlEventFactory.createDTD("\n");
  73.         XMLEvent doubleTab = xmlEventFactory.createDTD("\t\t");
  74.         //Create Start node
  75.         StartElement sElement = xmlEventFactory.createStartElement("", "", element);
  76.         eventWriter.add(doubleTab);
  77.         eventWriter.add(sElement);
  78.         //Create Content
  79.         Characters characters = xmlEventFactory.createCharacters(value);
  80.         eventWriter.add(characters);
  81.         // Create End node
  82.         EndElement eElement = xmlEventFactory.createEndElement("", "", element);
  83.         eventWriter.add(eElement);
  84.         eventWriter.add(end);
  85.  
  86.     }
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top