Altova XMLSpy 2025 Enterprise Edition

Code Generator

Home Prev Top Next

XMLSpy includes a built-in code generator which can generate Java, C++ or C# class files from XML schemas. The generated code consists of strongly-typed schema wrapper libraries that enable you to create software applications that process XML data. The schema wrapper libraries enable you to work with XML data via programs, using types generated from the schema. You would typically use code generator as follows. First, model your XML Schema in XMLSpy's graphical schema editor (Schema View). Then generate code in your preferred code language (Java, C++ or C#). If you change the schema's content  model, re-run the code generator.

 

The generated code supports the following operations:

 

Read XML files into a Document Object Model (DOM) in-memory representation

Write XML files from a DOM representation back to a system file

Convert strings to XML DOM trees and vice versa.

 

The table below summarizes support information.

 

Target Language

C++

C#

Java

Development environments

Microsoft Visual Studio 2013, 2015, 2017, 2019, 2022

Microsoft Visual Studio 2013, 2015, 2017, 2019, 2022

 

Target frameworks:

 

.NET Framework

.NET Core 3.1

.NET 5.0

.NET 6.0

.NET 8.0

Java SE JDK 8, 11, 17, 21 (including OpenJDK)

Eclipse 4.4 or later

Apache Ant (build.xml file)

XML DOM implementations

MSXML 6.0

Apache Xerces 3

System.Xml

JAXP

 

Language-specific information

Language-specific information is provided in the subsections below.

 

C++

You can configure whether the C++ generated output should use MSXML 6.0 or Apache Xerces 3. XMLSpy generates complete project (.vcproj) and solution (.sln) files for all supported versions of Visual Studio (see table above). The generated code optionally supports MFC.

 

Note the following prerequisites:

 

To compile the generated C++ code, Windows SDK must be installed on your computer.

To use Xerces 3 for C++, you will need to install and build it using the instructions on the Apache Xerces page. Make sure to add the XERCES3 environment variable that points to the directory where Xerces is installed (e.g., C:\xerces-c-3.2.2). Also, the PATH environment variable must include the path where the Xerces binaries are (e.g., %XERCES3%\bin).

When you build C++ code for Visual Studio and use a Xerces library precompiled for Visual C++, you will need to change the compiler setting in all the projects of the solution. Follow the steps below:

 

a)Select all projects in the Solution Explorer.

b)Click Properties in the Project menu.

c)Click Configuration Properties | C/C++ | Language.

d)In the list of configurations, select All Configurations.

e)Change Treat wchar_t as Built-in Type to No (/Zc:wchar_t-).

 

C#

The generated C# code can be used from any .NET capable programming language, such as VB.NET, Managed C++, or J#. Project files can be generated for all supported versions of Visual Studio (see table above).

 

Java

The generated Java output is written against the Java API for XML Processing (JAXP) and includes an Ant build file and project files for supported versions of Java and Eclipse (see table above).

 

Resolving "Out of memory" exceptions during Java compilation

Complex schemas can produce a large amount of code, which might cause a java.lang.OutofMemory exception during compilation using Ant. To rectify this:

 

Add the environment variable ANT_OPTS, which sets specific Ant options such as the memory to be allocated to the compiler, and set its value as shown below.

cg_ANT_OPTS

To make sure that the compiler and the generated code run in the same process as Ant, change the fork attribute, in build.xml, to false.

 

You may need to customize the values depending on the amount of memory in your machine and the size of the project you are working with. For more details, see your Java VM documentation.

 

When running the ant jar command, you may get an error message similar to "[...] archive contains more than 65535 entities". To prevent this, it is recommended that you use Ant 1.9 or later, and, in the build.xml file, add zip64mode="as-needed" to the <jar> element.

 

 

Generated output

The designated destination folder for the generated code includes all the libraries and files required to manipulate XML files programmatically, namely:

 

Standard Altova libraries

Schema wrapper libraries

An empty test application with sample source code. The test application skeleton is a compilable application that calls an empty Example() method. You can add your test code into this method for easy and quick testing of your new generated library.

 

Code generator templates

The generated code is built via a template that is written in a template language called SPL (Spy Programming Language). You can customize the template used for code-generation. For example, you can use SPL to map XML Schema's built-in data types to the primitive datatypes of a particular programming language and to build your own templates to automate the generation of virtually any other format, for example, EJB's, WSDL files, SQL scripts, ASP and WML code.

 

Examples

For examples illustrating code generation capabilities, see Example: Book Library and Example: Purchase Order.

 

© 2018-2024 Altova GmbH