Table Of Contents

Previous topic


Next topic


This Page


A Java binding for the cansas1d:1.1 standard has been auto-created using the JAXB tools from Oracle (formerly Sun, see below for more on JAXB) using the cansas1d.xsd XML Schema. See the Downloading section below.

Using the Java Binding

The basics of the binding are these java statements. First, associate a JAXB context with the canSAS namespace URI

jc = JAXBContext.newInstance("org.cansas.cansas1d");

Next, create an object to read XML data into Java data objects created by JAXB from the canSAS XML Schema.

Unmarshaller unmarshaller = jc.createUnmarshaller();

Next, open an XML file from local storage.

InputStream in = new FileInputStream("a/data/file.xml);

Next, load the XML data into a Java data structure

xmlJavaData = (JAXBElement<SASrootType>) unmarshaller.unmarshal(in);

Next, get the SASroot object

SASrootType sasroot = xmlJavaData.getValue();

With a SASroot object, one can iterate over the SASentry groups and, for instance, print the Title string:

for ( SASentryType entry : sasroot.getSASentry() ) {
        System.out.printf("SASentry Title:  %s\n", entry.getTitle());

Full example

An example that uses the binding is provided in the java support and is available for direct download or may be viewed using a web browser:

Output from is:

class: org.cansas.cansas1d.demo.Reader
SVN ID: $Id: binding-java-jaxb.rst 332 2013-04-12 16:54:45Z prjemian $

File: ./resources/cansas1d/cs_collagen.xml
SASentry elements: 1
Title:  dry chick collagen, d = 673 A, 6531 eV, X6B
#Runs:  1
Run:    Sep 19 1994     01:41:02 am
#SASdata:       1
#points:        125

the end.

File: ./resources/cansas1d/1998spheres.xml
SASentry elements: 2
Title:  255 nm PS spheres
#Runs:  1
Run:    scan2.dat, scan 5
#SASdata:       1
#points:        1824

Title:  460 nm PS spheres
#Runs:  1
Run:    scan1.dat, scan 67
#SASdata:       1
#points:        3689

the end.

File: cannot_find_this.xml
File not found: cannot_find_this.xml

example: how to retrieve \(I(Q)\)

This is a slightly longer example. Look near line 75 for this code:

SASdataType sasdata = sasroot.getSASentry().getSASdata()
// ...
Qsas[i] = sasdata.getIdata().get(i).getQ().getValue();
Isas[i] = sasdata.getIdata().get(i).getI().getValue();

to see the operations that unwind the data into usable double[] vectors. Pretty straightforward although there is lots of interesting, yet unnecessary, diagnostic output. Here is a table that describes the items in the line just shown:

java item description
sasdata SASdataType object
getIdata() amongst the /SASdata/Idata tuples ...
get(i) ... pick the Idata tuple from row i.
getQ() Just the /SASdata/Idata/Q
getValue() and specifically the value, not the unit


Resources (JAR files and documentation) for the Java binding may be found in the canSAS subversion Software repositories (for cansas1d:1.1 standard).

JAR file to add to your CLASSPATH in order to use this binding. Adheres to canSAS 1D standard version #.#.

Use this JAR file if you want to add the source code documentation as tooltips to your editor, such as eclipse. Note that this file is compatible with any ZIP program and can be unzipped to provide a directory with all the documentation as a set of HTML pages. Start with the index.html page. Adheres to canSAS 1D standard version #.#.

JAR file of the source code. Note that this is just the source code tree and not the full project development tree for the Java (JAXB) API. Adheres to canSAS 1D standard version #.#.
source code (for developers)

canSAS Development project subversion repository for the Java binding. Only use this if you want to participate as a code developer of this binding.

JAXB: Questions and Answers


What is JAXB?


Java Architecture for XML Binding (


Wow! Is it available for other languages?


Ask Google. JAXB is for Java. (

For example:

Q:How do I pull out the \(I(Q)\) data?
A:See Java code fragment above
Q:Has JAXB been useful?
A:Very useful. Since an XML Schema was defined, JAXB was very useful to create a Java binding automatically. Then, javadoc was able to auto-generate the basic documentation as HTML and pdfdoclet was able to auto-generate the documentation in a PDF file.

(re)building the JAXB code

The Java bindings can be built from the XML Schema file using JAXB. For more information, refer to the JAXB documentation: [1]



To use xjc, you need a JDK, not just a JVM or JRE.

Starting from the <trunk> directory, create a directory (if necessary) for the JAXB Java source code that will be created from the XML Schema:

mkdir -p java/ant-eclipse/src

Staying in the <trunk> directory, build the JAXB JAXB Java source code:

xjc -d java/ant-eclipse/src cansas1d.xsd -p org.cansas.cansas1d

Next, moving into the Java project directory, rebuild the .jar files:

cd java/ant-eclipse
ant rebuild
/bin/cp dist/*.jar ../