Java Tutorials // Example – Sdmx Structure Parser


The following demo will process an SDMX structure file, resolving cross referenced structures if required.

The StructureParser can read SDMX-ML or SDMX-EDI files and create the domain objects used by SdmxSource, SdmxBeans. The StructureParser can also output SDMX-ML or SDMX-EDI files from the SdmxBeans. The StructureParser can read and write any version of SDMX, and can therefore be used to transform between SDMX versions. The StructureParser can also consume any type of document containing structures, this includes structures contained in a RegistryInterface document, or structures contained in QueryResponse documents.

The Structure parser will validate the SDMX file against the relevant SDMX Schema, it will then build the SdmxBeans. Each bean will be further validated against rules that are not defined in the SDMX Schema. Additionally the StructureParser can be used to retrieve any additional structures referenced from the Structures built, this includes cross references and external references.

It is important to note that once the SdmxBeans are built, they can not be modified, so systems can be sure of validity.


Demo Project

The following link will download a demo project from which the following example is taken.
DEMO STRUCTURE PARSER PROJECT

Adding Structure Parser capabilities to your project

The StructureParser Interface lives in the SdmxApi project. There is only one implementation of this interface, and is provided in the SdmxStructureParser project. The following dependencies are used in the demo project.

  <!-- WEAVING -->
  <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.11</version>
  </dependency>
   	
  <!-- REQUIRED FOR APPLICATION CONTEXT -->
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
  </dependency>

  <!-- STRUCTURE PARSER -->
  <dependency>
    <groupId>org.sdmxsource</groupId>
    <artifactId>SdmxStructureParser</artifactId>
    <version>1.5.6.6</version>
  </dependency>

The SdmxStructureParserdependency has dependencies on the SdmxApi (amongst other) projects, and all dependencies will be automatically download by the IDE and included on the build path of the project.

Sample 1 – Parse an SDMX-ML file into SdmxBeans

The following sample code will process the SDMX file at a web location to return the SdmxBeans.


//1. Create a Readable Location
String url = "http://www.sdmxsource.org/samples/structure/abc.xml";
ReadableDataLocation structureLocation = new ReadableDataLocation(url);

//2. Parse Structures
StructureWorkspace workspace = structureParsingManager.parseStructures(structureLocation);

//3. Get the SdmxBeans from the workspace
SdmxBeans beans = workspace.getStructureBeans(false);

Sample 2 – Parse an SDMX-EDI file into SdmxBeans

This sample is the same as the first, although this time the file is in SDMX-EDI (also known as GESMES/TS) format.

String url = "http://www.sdmxsource.org/samples/structure/abc.edi";
ReadableDataLocation structureLocation = new ReadableDataLocation(url);
StructureWorkspace workspace = structureParsingManager.parseStructures(structureLocation);
SdmxBeans beans = workspace.getStructureBeans(false);

Sample 3 – Resolve References

This sample parses the SDMX file, as in sample 1, additionally it will resolve any external references and resolve any structures that are cross referenced by the structure in the input file.

String url = "http://www.sdmxsource.org/demofiles/ecb_exr_sg_full.xml";
ReadableDataLocation structureLocation = new ReadableDataLocation(url);
ResolutionSettings resolutionSettings = new ResolutionSettings(RESOLVE_EXTERNAL_SETTING.RESOLVE_SUBSTITUTE,
RESOLVE_CROSS_REFERENCES.RESOLVE);
StructureWorkspace workspace = structureParsingManager.parseStructures(structureLocation, resolutionSettings, null);