Pages

Tuesday, February 25, 2014

Load Categories with Java for Oracle iProcurement

The code below can be used to pull in a text file that contains the Category information and then it writes an XML file that Oracle can pull in to load (or delete categories that are not used) categories in Oracle iProcurement.

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Date;
import java.util.Scanner;



import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;

public class XMLW {

    static String filename = "/Users/mclancy/Documents/workspace/text.xml";

    public static void main(String[] args) throws Exception {
        Scanner lsc1 = null;
        Date date = new Date();
        File opf = new File(filename);
        FileWriter fw = new FileWriter(opf);
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = factory.createXMLStreamWriter(fw);
        IndentingXMLStreamWriter sw = new IndentingXMLStreamWriter(writer);
        sw.setIndentStep("    ");
        // Header Section
        sw.writeStartDocument("1.0");

        sw.writeStartElement("CATALOG");
        sw.writeAttribute("xml:lang", "EN-US");
        // ADMIN Section
        sw.writeStartElement("ADMIN");
        sw.writeStartElement("NAME");
        sw.writeCharacters("Outbev Cat import");
        sw.writeEndElement();

        sw.writeStartElement("INFORMATION");
        sw.writeStartElement("DATE");
        sw.writeCharacters(date.toString());
        sw.writeEndElement();
        sw.writeStartElement("SOURCE");
        sw.writeCharacters("ACME");
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeEndElement();
        sw.writeStartElement("SCHEMA");

        // File input data
        File rec = new File("/Users/mclancy/Documents/workspace/rec.txt");
        FileReader fr = new FileReader(rec);
        Scanner fsc1 = new Scanner(fr);

        while (fsc1.hasNext()) {
            String line = fsc1.next();
            lsc1 = new Scanner(line);
            lsc1.useDelimiter(",");
            while (lsc1.hasNext()) {
                // System.out.println(lsc1.next());

                // Catalog data

                sw.writeStartElement("CATEGORY");
                sw.writeAttribute("ACTION", "SYNC");
                sw.writeStartElement("KEY");
                sw.writeCharacters(lsc1.next());
                sw.writeEndElement();
                sw.writeStartElement("NAME");
                sw.writeCharacters(lsc1.next());
                sw.writeEndElement();
                sw.writeStartElement("TYPE");
                sw.writeCharacters("GENUS");
                sw.writeEndElement();
                sw.writeEndElement();
            }
        }
        sw.writeEndElement();
        sw.writeEndDocument();
        sw.flush();
        sw.close();

        fsc1.close();
        lsc1.close();

        System.out.println("Done");
        System.out.println(date);
    }
}