473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2911
>>>>> "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ranma79 | last post by:
Hi, I was wondering if anyone knows if this is possible. I have the following simple xml document as follows: <test> <field1>Value1</field1> <field2>Value2</field2> </test>
4
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could...
4
by: jlea | last post by:
Is there an option in the schema definition to indicate that when xsd.exe generates classes for the schema that a certain element will not get serialized to and from XML? In other words, I would...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
2
by: gisleyt | last post by:
Is it possibly to specify that I want a either a element or an attribute to occur, but not both at the same time? <xs:complexType name="foo"> <xs:sequence> <xs:element minOccurs="0" ref="bar"/>...
28
by: Andy Dingley | last post by:
What specifies the permitted root element(s) for a document ? HTML, SGML, XHTML or XML ? Valid HTML documents need to have a well-known DTD and a doctypedecl in each document like this:...
13
by: Jiho Han | last post by:
Here's the issue. You have a class, Class Person { public int id; public string firstname; public string lastname; }
0
by: Dave Hill | last post by:
Forgive a newbie question. I'm learning the .NET XML environment. In the walkthrough on using XML designer to create an xsd, there is no discussion of the root element of the target xml document....
0
by: icesign | last post by:
I know that the selector of these elements has a scope relative to the element being declared, but maybe there is a way to get beyond bounds of this scope or maybe just a way to extend base element?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.