Using Exificient, I've managed to encode & decode a sample XML as EXI using the snippet from their "Help" page and their "demo", I've managed to get the following working using just the Maven dependency
<dependency>
<groupId>com.siemens.ct.exi</groupId>
<artifactId>exificient</artifactId>
<version>0.9.4</version>
</dependency>
with the code
import com.siemens.ct.exi.EXIFactory;
import com.siemens.ct.exi.GrammarFactory;
import com.siemens.ct.exi.api.sax.EXIResult;
import com.siemens.ct.exi.api.sax.EXISource;
import com.siemens.ct.exi.helpers.DefaultEXIFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
// based on: https://sourceforge.net/p/exificient/code/HEAD/tree/tags/exificient-0.9.4/src/sample/java/EXIficientDemo.java?format=raw
public class MinimalistExificientSample {
static final boolean USE_SCHEMA = true;
public static void main(String[] args) throws Exception{
File xmlIn = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom.xml" );
FileInputStream xmlIns = new FileInputStream( xmlIn );
File exi = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom." +(USE_SCHEMA? "schema":"schemaless")+ ".exi" )
FileOutputStream exiOuts = new FileOutputStream( exi );
File xmlOut = new File( "/home/artb/spring-core-4.1.6.RELEASE.pom." +(USE_SCHEMA? "schema":"schemaless")+ ".xml" );
//settings
EXIFactory exiFactory = DefaultEXIFactory.newInstance();
exiFactory.setGrammars( GrammarFactory.newInstance().createGrammars( "/home/artb/maven-4.0.0.xsd" ) );
// encode
InputSource xmlIs = new InputSource( xmlIns );
EXIResult exiResult = USE_SCHEMA ? new EXIResult(exiFactory) : new EXIResult();
exiResult.setOutputStream( exiOuts );
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler( exiResult.getHandler() );
xmlReader.parse( xmlIs );
// decode
FileOutputStream xmlOuts = new FileOutputStream( xmlOut );
FileInputStream exiIns = new FileInputStream( exi );
InputSource exiIs = new InputSource( exiIns );
EXISource exiSource = USE_SCHEMA ? new EXISource(exiFactory) : new EXISource();
exiSource.setInputSource(exiIs);
exiSource.setXMLReader( exiSource.getXMLReader() );
TransformerFactory.newInstance().newTransformer().transform(exiSource, new StreamResult(xmlOuts));
}
}
use the Maven XSD and the Spring 4.1.6 POM from Maven central as sample data.
Nb: that the output XML ought to be equivalent to the input, but not necessarily equal since comments and whitespace are lost and the namespace aliases(? might be using the wrong term here) are arbitrarily assigned.