Search code examples
javaspringbean-iospring-io

Using an annotated class in BeanIO instead of an XML for mapping


I'm following this simple tutorial http://beanio.org/
Toward the end says you can use an annotated class instead of an XML file. I did that and in my factory.load() I pass the value with the name of my annotated class. and I get am org.xml.sax.SAXParseException. I believe this is caused because its expecting an XML file.

What method I need to use to pass my annotated class instead of an XML?


Solution

  • In order to use a mapping class instead of an XML you only have to add the following code

    StreamFactory factory = StreamFactory.newInstance();
    StreamBuilder builder = new StreamBuilder("") // Your file
        .format("delimited")
        .parser(new DelimitedParserBuilder(',')) // Sign to  use as a delimiter
        .addRecord(Yourclass.class); // class to be mapped 
    
    factory.define(builder);
    

    This way an XML file is not need it at all.

    Source:
    http://beanio.org/2.1/docs/reference/index.html#BuilderApiAndAnnotations