472,353 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 2813
>>>>> "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>...
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...
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...
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"...
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">...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.