I'm looking for a Java sample program for encoding/decoding EXI (Efficient XML Interchange) streams, using either EXIficient or OpenEXI.
Could someone help? I can't seem to find a sample app.
edit: Alternatively, if someone could point me towards documentation that would allow me to use EXIficient or OpenEXI, that would be helpful. I found the javadoc but I have no idea which classes to use.
Or, as @StaxMan points out, is there a particular mention/discussion of the appropriate top-level classes to use with one of the standard XML APIs?
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.
I am not familiar with the two libraries mentioned; but I assume that as any sane XML libraries they implement one of well-known Java XML processing APIs, such as SAX, Stax or DOM (possibly with some extensions). This because EXI is just different encoding of standard XML Infoset; so for application developer things may look exactly like normal XML processing.
So, you may just need to figure out API(s) they implement and find tutorials for using that API?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With