Is it possible to get element type specified by schema while parsingthe xml document using SAX/DOM?

J

Jari Kujansuu

I can successfully parse XML document using SAX or DOM and I can also
validate
XML document against schema.
Problem is that my program should deal with user-defined schemas which means
that when I parse some element from XML document I don't know until
runtime the
type of the element (it depends on the type used in user-defined
schema). XML
parser obviously validates that the value of the element is correct type but
still I receive that data in string format from parser and question is
now how
can I convert it to correct java type representation? What I need is the
type
of the element defined by schema, but can I get that somehow when I am
handling
data received by callback methods of ContentHandler in SAX or can I
found the
type information somewhere from DOM tree?

Simple example:

If schema contains something like this:

<element name="id" type="integer"/>

And corresponding XML document contains something like this

<id>15</id>

Now Parser returns the value of id element as a string representation
"15" and
I would like to find out the type defined by schema in this case
"integer" and
based on that information convert string "15" to integer value 15. How can I
do this? Parser have to get this type information anyway to validate
that data
in XML document is correct but I cannot find any ways to get that
information
out of parser to my own application code.
 
A

Arto V. Viitanen

Jari> I can successfully parse XML document using SAX or DOM and I can also
Jari> validate

Jari> XML document against schema. Problem is that my program should deal
Jari> with user-defined schemas which means that when I parse some element
Jari> from XML document I don't know until runtime the

Jari> type of the element (it depends on the type used in user-defined
Jari> schema). XML

Jari> parser obviously validates that the value of the element is correct
Jari> type but still I receive that data in string format from parser and
Jari> question is now how

Jari> can I convert it to correct java type representation? What I need is
Jari> the type

Jari> of the element defined by schema, but can I get that somehow when I am
Jari> handling

Jari> data received by callback methods of ContentHandler in SAX or can I
Jari> found the

Jari> type information somewhere from DOM tree?

Jari> Simple example:

Jari> If schema contains something like this:

Jari> <element name="id" type="integer"/>

Jari> And corresponding XML document contains something like this

Jari> <id>15</id>

Jari> Now Parser returns the value of id element as a string representation
Jari> "15" and

Jari> I would like to find out the type defined by schema in this case
Jari> "integer" and

Jari> based on that information convert string "15" to integer value 15. How
Jari> can I do this? Parser have to get this type information anyway to
Jari> validate that data

Jari> in XML document is correct but I cannot find any ways to get that
Jari> information

Jari> out of parser to my own application code.


So did you mean that you get first the XML-data and then the schema? Or, you
did not get any schema, but like to add some information to your data?

You could use same as SOAP, namely add namespaces

xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

and use <id xsi:type="xsd:int">15</id>.

Then, you can parse the xsi:type attributes and check the type.
 
J

Jari Kujansuu

Arto said:
Jari> I can successfully parse XML document using SAX or DOM and I can also
Jari> validate

Jari> XML document against schema. Problem is that my program should deal
Jari> with user-defined schemas which means that when I parse some element
Jari> from XML document I don't know until runtime the

Jari> type of the element (it depends on the type used in user-defined
Jari> schema). XML

Jari> parser obviously validates that the value of the element is correct
Jari> type but still I receive that data in string format from parser and
Jari> question is now how

Jari> can I convert it to correct java type representation? What I need is
Jari> the type

Jari> of the element defined by schema, but can I get that somehow when I am
Jari> handling

Jari> data received by callback methods of ContentHandler in SAX or can I
Jari> found the

Jari> type information somewhere from DOM tree?

Jari> Simple example:

Jari> If schema contains something like this:

Jari> <element name="id" type="integer"/>

Jari> And corresponding XML document contains something like this

Jari> <id>15</id>

Jari> Now Parser returns the value of id element as a string representation
Jari> "15" and

Jari> I would like to find out the type defined by schema in this case
Jari> "integer" and

Jari> based on that information convert string "15" to integer value 15. How
Jari> can I do this? Parser have to get this type information anyway to
Jari> validate that data

Jari> in XML document is correct but I cannot find any ways to get that
Jari> information

Jari> out of parser to my own application code.


So did you mean that you get first the XML-data and then the schema? Or, you
did not get any schema, but like to add some information to your data?

You could use same as SOAP, namely add namespaces

xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

and use <id xsi:type="xsd:int">15</id>.

Then, you can parse the xsi:type attributes and check the type.
No I mean that I would like to avoid of putting type information to the
xml document (type information is already in schema). Instead I would
like to get the type information directly from schema. I could of course
parse schema first and then parse the xml document but it makes things
just too complex to implement logic that find out from parsed schema
which type definition corresponds which data value in xml document. The
parser need to read type information from schema anyway when it parses
xml document so that it can validate xml document against schema. I just
would like to know if it is possible to somehow access the type
definition (read from schema) while I am parsing xml document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top