csvan
csvan

Reputation: 9454

Is there a standard XML schema for Java objects/classes?

Is there an available, standard XML schema which describes the general structure and content of Java Objects? I have searched for this to no avail.

I am relatively new to both XML in general, and various Java based XML technologies (primarily JaxB) in particular, so forgive me if I have overlooked something.

Upvotes: 1

Views: 257

Answers (4)

Stephen C
Stephen C

Reputation: 718758

The simple answer is that there is no such standard schema.

When you serialize using something like JAXB, you effectively have a different XML schema for each class / network of classes that you bind.


A couple more observations:

  • It would be possible to write an XML schema that is capable of representing any kind of Java object. You would need an element for "object" which had an attribute called class name, child elements called "field" and so on. But the resulting XML representations would take significantly more space, and would be difficult to read and to use.

  • In JAXB and the like, there (clearly) is a systematic relationship between the network of classes and the XML structure, but it is not expressible as an XML schema. Rather, it is a mapping that "generates" a schema from the classes, in a conceptual sense. (In fact, it is analogous to what is happening in XMI, where a meta-model is mapped to an XML schema.)

  • Referential integrity is beyond what is expressible in a XML schemas, but that is not the reason why there is no common schema. Even if you ignore referential integrity and reduce the problem to "tree shaped", you still cannot generate a useful common schema for the kind of XML that JAXB, XMI and similar mappings produce.

Upvotes: 3

13ren
13ren

Reputation: 12187

It's not an official Oracle/Sun standard, but a third-party tool, JSX, serializes java objects to xml, and has an XML Schema specification of its format.

The example XML shows how it records the internal details of objects, including field types and inherited classes.

Even if you don't use this schema yourself, it would give you a starting point for the issues to be addressed by a format in order to fully represent object state.

Upvotes: 1

bmargulies
bmargulies

Reputation: 100013

It is hypothetically possible to construct a sort of meta-schema that would allow all the possible XML files generated by JAX-B or some other serialization library. However, most people would not find such a schema useful, because it would permit such a broad range of XML documents as to be essentially unhelpful. Instead, people either start from a schema and generate Java classes, or start from a particular graph of java classes and generate a schema that reflects (haha) the particular data in it.

Upvotes: 0

Jérôme Radix
Jérôme Radix

Reputation: 10533

An XMLSchema file describes what is possible and not possible in an XML document conforming to definitions of elements stored inside this XMLSchema file.

XMLSchema, in itself, is not related to Java : an XMLSchema file is here solely to check validity of XML files conforming to the XMLSchema file.

The Java people use XMLSchema file to generate Java source code that is able to receive data from XML file conforming the XMLSchema (the xjc tool).

You could also generate XMLSchema file from a set of Java source files (use schemagen tool for that).

When you have the following elements, you are able to use JAXB to marshall/unmarshall Java objects to/from XML files :

  • XMLSchema files
  • Java source files
  • XML files

There are other ways to marshall/unmarshall xml data from/to Java.

Upvotes: 0

Related Questions