Create a Java starter model from a small XML fixture

Convert fictional XML into Java field declarations, inspect inferred XML values and accessors, and account for parser and model-generation limitations.

By DevSphere Editorial · · 5 min read

Open Data to Class

Prepare a minimal XML document

Data to Class accepts XML as well as JSON and can generate a Java class starter from a small document. Use a fictional shipment fixture with a root element, a text code, a numeric count, and a boolean-like string. Avoid real shipment references or customer records. XML carries values as text, so the component infers booleans and numbers from simple lexical patterns; that inference may not match the data contract. Keep the original XML so you can compare every generated field with its source.

Open Data to Class, switch Input Type to XML, paste the example, enter `ShipmentRecord` in Class Name, and select Java in Target Language. The output appears under Java Output. The Generate Getters/Setters checkbox is enabled by default for Java; leave it on for this walkthrough. Use Load Sample if you need to see the broader example structure, then replace it with a minimal fixture that isolates the fields you want.

Input: <shipment><code>DEMO-17</code><units>4</units><insured>true</insured></shipment>
Expected Java types: code:String, units:int, insured:boolean, with accessors when enabled.

Understand XML to object conversion

The XML parser first creates an object rooted under the document element name. The root element's children become properties; repeated child names are collected into arrays. Attributes are represented with an `@` prefix in the intermediate object. For the sample, `code` remains a string, `units` is inferred as an integer, and `insured` becomes a boolean because its text is exactly `true`. These are simple conversions, not XML Schema-driven typing.

Set Class Name to a meaningful root wrapper name and inspect the Java fields, including any nested classes derived from object keys. When the XML root is represented as an object property, output may include a wrapper model alongside the root element's generated class. Confirm class and field names, since the converter's basic naming helpers are not a full Java naming or XML binding system.

Review Java types and accessors

The Java generator maps inferred strings to `String`, integers to primitive `int`, floating values to `double`, booleans to primitive `boolean`, date-shaped text to `Date`, and arrays to `List` element types. If Generate Getters/Setters remains checked, it emits a getter and setter for each field. Inspect the output for required imports, constructors, nullability, and collection initialization. The generator's output is a starter; it may require imports and project-specific adjustments before compilation.

Primitive Java types cannot represent null, while XML can omit an element or provide empty content. The generated model does not infer optionality from missing tags because a single sample only shows the elements it contains. If the actual XML schema permits absent or nil values, adjust the types and mapping strategy manually. Confirm whether numeric-looking values such as postal codes should remain strings to preserve leading zeroes.

Account for XML edge cases

Repeated siblings with the same tag name are grouped into an array, but mixed content, namespaces, attributes, and nested repeated records may need more careful treatment. Namespace prefixes can become part of tag names, and the simple converter does not use a supplied XSD to infer types or cardinality. A parser error results in invalid input output. Check the source document in a proper XML parser when namespaces, entities, or schema rules matter.

The type inference treats literal `true` and `false` as booleans and numeric-looking text as integer or float. A text identifier such as `00042` may be inferred as a number and lose leading zeros in the generated type expectation. Dates are identified by a limited ISO-like pattern and mapped as Date for Java. Validate these inferences against the source contract before adopting the model.

Troubleshoot and refine the class

If output reports invalid XML, check matching open and close tags, proper attribute quoting, and a single document root. The XML parser runs in the browser and detects parser errors; malformed input cannot generate a class. If the output contains `Object` or an unexpected list type, inspect whether the source value is null, repeated, empty, or represented by a nested element. A representative fixture should include each important structural variation.

Use Copy Output to transfer the starter into your project, then add imports, constructors, annotations, XML binding configuration, or custom conversion logic required by your framework. Compile with the project's Java version and run a serialization/deserialization test against the fixture. The generator does not compile, execute, or bind the generated model to XML automatically.

Validate the model against the actual contract

Compare the class against an authoritative schema or several controlled XML examples. Test missing values, repeated tags, attributes, empty text, and numeric edge cases. Verify that serialization produces the XML structure expected by the consumer and that deserialization preserves values such as IDs with leading zeros. Generated fields alone cannot prove schema fidelity, particularly when one document is only a sample.

Keep the fixture fictional and the output labeled as starter code. Clear the input after the exercise if it contains anything beyond public sample values. Data to Class accelerates the initial translation from a visible XML shape to Java declarations, but project imports, null handling, XML annotations, schema validation, and behavioral tests remain your responsibility. Review generated nested class names for collisions and split declarations into separate source files if that matches your project's conventions.

Browse all tutorials