469,342 Members | 6,660 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

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


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.

Jul 20 '05 #1
4 2696
>>>>> "Jari" == Jari Kujansuu <ja***********@nokia.com> writes:

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.
--
Arto V. Viitanen av@cs.uta.fi
University of Tampere, Department of Computer Sciences
Tampere, Finland http://www.cs.uta.fi/~av/
Jul 20 '05 #2
Arto V. Viitanen wrote:
>>"Jari" == Jari Kujansuu <ja***********@nokia.com> writes:
>>
>>


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.

Jul 20 '05 #3
I can successfully parse XML document using SAX or DOM and I can also
validate
XML document against schema.

Problem is after parsing the xml i'm getting all values as the string . now i want to store values to hashtable means it will store all are as strings only.

i want to store values in hashtable in corresponding type that is specified in the xsd . so how to get the type from the xsd and how to compare values to the type when store to the hashtable.

Simple example:

If xsd contains something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:simpleType name="comverseIP"><xsd:restriction base="xsd:string"><xsd:pattern value="(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))"/></xsd:restriction></xsd:simpleType><xsd:simpleType name="comverseEmail"><xsd:restriction base="xsd:string"><xsd:pattern value="[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*@[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*"/></xsd:restriction></xsd:simpleType><xsd:element name="root"><xsd:complexType><xsd:sequence><xsd:se quence maxOccurs="1" minOccurs="1"><xsd:element ref="CDR_DESTINATION"/></xsd:sequence></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="CDR_DESTINATION"><xsd:complexType><xsd:seque nce/><xsd:attribute name="ARCHIVE_PATH" use="required"><xsd:simpleType><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType></xsd:attribute><xsd:attribute name="DESTINATION_PATH" use="required"><xsd:simpleType><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType></xsd:attribute></xsd:complexType></xsd:element></xsd:schema>

And corresponding XML document contains something like this

<?xml version="1.0" encoding="UTF-8"?>
<root>
<CDR_DESTINATION ARCHIVE_PATH="/data/backup/mpmcdr/AllFieldsArchive" DESTINATION_PATH="/data/backup/mpmcdr/AllFields"/>
</root>


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.




suresh
Mar 23 '06 #4
please help me


i'm having xml and corresponding xsd.i've parsed the xml, it successfully parsed and get the elementName,attributeName and attributeValue successfully in strings.

whenever create the Hashtable i want to store attributeValue in appripiate type specified in the xsd file. that's why i want to parse the xsd and get the attributeName and appropriate type.

is it possible to parse the xsd using DOM parser in java.

sample xsd file


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:simpleType name="comverseIP"><xsd:restriction base="xsd:string"><xsd:pattern value="(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))\.(([0-9])|([0-9][0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))"/></xsd:restriction></xsd:simpleType><xsd:simpleType name="comverseEmail"><xsd:restriction base="xsd:string"><xsd:pattern value="[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*@[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-'\*\+\-/=\?\^_`\{-~]+)*"/></xsd:restriction></xsd:simpleType><xsd:element name="root"><xsd:complexType><xsd:sequence><xsd:se quence maxOccurs="1" minOccurs="1"><xsd:element ref="CDR_DESTINATION"/></xsd:sequence></xsd:sequence></xsd:complexType></xsd:element><xsd:element name="CDR_DESTINATION"><xsd:complexType><xsd:seque nce/><xsd:attribute name="ARCHIVE_PATH" use="required"><xsd:simpleType><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType></xsd:attribute><xsd:attribute name="DESTINATION_PATH" use="required"><xsd:simpleType><xsd:restriction base="xsd:string"></xsd:restriction></xsd:simpleType></xsd:attribute></xsd:complexType></xsd:element></xsd:schema>


and sample xml file


<?xml version="1.0" encoding="UTF-8"?>
<root>
<CDR_DESTINATION ARCHIVE_PATH="/data/backup/mpmcdr/AllFieldsArchive" DESTINATION_PATH="/data/backup/mpmcdr/AllFields"/>
</root>

plz give me some possible suggestion

regards,
suresh
Mar 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gordon Dickens | last post: by
4 posts views Thread by jlea | last post: by
2 posts views Thread by gisleyt | last post: by
28 posts views Thread by Andy Dingley | last post: by
reply views Thread by Dave Hill | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.