0

first time using stack overflow, I'm now a student doing a project for analtics purpose, but the company store all the records into XML and I have to convert it and make a program as so to make it automated report send by email.

I'm using java to do XML parser and I'm now trying Apache common digester as other parser needs XSLT to do that, but iIwant a program that doesn't depends on XSLT because the company wants a system and sends a report like every 5 min of summary. So using XSLT may be quite slow as I saw some of the answer here. So may I know how to do that using digester or other methord, try to show some example the codes if possible, to make a conversion.

Here is the sample code I have build under digester:

public void run() throws IOException, SAXException {    
    Digester digester = new Digester();
    // This method pushes this (SampleDigester) class to the Digesters
    // object stack making its methods available to processing rules.
    digester.push(this);
    // This set of rules calls the addDataSource method and passes
    // in five parameters to the method.
    digester.addCallMethod("datasources/datasource", "addDataSource", 5);
    digester.addCallParam("datasources/datasource/name", 0);
    digester.addCallParam("datasources/datasource/driver", 1);
    digester.addCallParam("datasources/datasource/url", 2);
    digester.addCallParam("datasources/datasource/username", 3);
    digester.addCallParam("datasources/datasource/password", 4);


    File file = new File("C:\\Users\\1206432E\\Desktop\\datasource.xml");
    // This method starts the parsing of the document.
    //digester.parse("file:\\Users\\1206432E\\Desktop\\datasource.xml");
    digester.parse(file);


  }

Another one which is build using DOM to convert CSV to XML but still relies one XSLT file:

public static void main(String args[]) throws Exception {
    File stylesheet = new File("C:\\Users\\1206432E\\Desktop\\Style.xsl");
    File xmlSource = new File("C:\\Users\\1206432E\\Desktop\\data.xml");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlSource);

    StreamSource stylesource = new StreamSource(stylesheet);
    Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result outputTarget = new StreamResult(
                    new File("C:\\Users\\1206432E\\Desktop\\temp.csv"));
    transformer.transform(source, outputTarget);
}
Héctor William
  • 756
  • 6
  • 24
meodows92
  • 1
  • 2
  • 4
  • Try OpenCsv - http://opencsv.sourceforge.net/ – Ninad Pingale Jul 24 '14 at 08:44
  • 3
    *"So using XSLT may be quite slow"* - Is that supported by evidence? Have you actually measured? – Tomalak Jul 24 '14 at 09:19
  • haven't yeat but according to some comments in stack overflow, performance will be degrade and the company needs a program to help them send report may be let say 5 min, so the frequency of using XML will increase. If using XSLT, what according will be performance is an issue and may be next time the company may want to add in more variable into it, so it is troublesome for them to keep on editing it, if possible try to automate it. If you will like to see the link is here: http://stackoverflow.com/questions/1086807/how-can-we-convert-xml-file-to-csv?answertab=active#tab-top – meodows92 Jul 24 '14 at 12:03
  • 1
    Possible duplicate of [XML to CSV conversion : Does not understand HTML characters and other special characters](https://stackoverflow.com/questions/19434274/xml-to-csv-conversion-does-not-understand-html-characters-and-other-special-ch) – Paul Sweatte Aug 23 '17 at 16:50

0 Answers0