• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

validate schema in DOM

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I want to validate xsd file which is associated with my xml file using DOM, any sample or examples which explains the functionality of DOM validating xml Schema, the input is an xml file where DOM parses it and automatically validates the associated xsd file.
could be appreciated,
[email protected]
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try Xerces DOM parser for validation, have a look here http://xml.apache.org/xerces2-j/faq-pcfp.html
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
balaji,
can u give few examples for xml schema validation using Xerces. and how to setup/install the xerces package after downloading.
Thanks
Kiran
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kiran, have you seen this tutorial?
 
srinivasa varadhan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi lasse,
thanks for ur help.but i have seen the link.it is not what i require is an api in java to traverse a schema and validate it to the corresponding xml file.i tried many DOMParsers , like xerces Domparser.i want a parser which validates schema against an xml file. i also tried castor. but its too advanced. if i have a tutorial on castor to validate a schema against an xml file it would be helpful
Thanks
Kiranmanohar
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not reading you. Xerces should be able to validate an XML document against a schema document reference by the XML document or any external schema document. Maybe you post some pseudo-code to explain what you want to do?
By the way, I don't think Castor XML can help you validate your XML any better -- it's not a parser but a data binding framework.
[ October 11, 2003: Message edited by: Lasse Koskela ]
 
kiran manohar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi lasse,
thanks for the clarification. in that case , i will go by xerces DOMParser itself. i m giving u the code and what i need to do. hope u can give me some idea on it.the xml and xml schema files content will change.In the xml file there is an order like client-->order--->orderitem.now i want to track the sequence in which the schema file is written and in that order input the data in to my java object.
The XML Code
============
<client xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="A.xsd"><!-- First Level-->
<clientRecord>
<name>KiranManohar</name>
<id>000001</id>
<order><!-- Second Level-->
<orderRecord>
<orderId>123456</orderId>
<title>10 PCs</title>
<shippingDate>2002-03-09</shippingDate>
<orderDate>2002-05-02</orderDate>
<orderItem><!-- Third Level -->
<orderItemRecord>
<ItemId>100001</ItemId>
<ProductName>Dell Latitude Laptop</ProductName>
<ProductId>A00000</ProductId>
<Quantity>4</Quantity>
<UnitPrice>100.50</UnitPrice>
<TotalPrice>402.00</TotalPrice>
</orderItemRecord>
</orderItem>
</orderRecord>
</order>
</clientRecord>
</client>
The Schema File
===============
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="client" type="Client"/>
<xs:complexType name="Client">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="clientRecord"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ClientRecord">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="id" type="xs:string"/>
<xs:element ref="order"/>
</xs:sequence>
</xs:complexType>
<xs:element name="clientRecord" type="ClientRecord"/>
<xs:complexType name="Order">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="orderRecord"/>
</xs:sequence>
</xs:complexType>
<xs:element name="order" type="Order"/>
<xs:complexType name="OrderRecord"/>
<xs:element name="orderRecord">
<xs:complexType>
<xs:complexContent>
<xs:extension base="OrderRecord">
<xs:sequence>
<xs:element name="orderId" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="shippingDate" type="xs ate"/>
<xs:element name="orderDate" type="xs ate"/>
<xs:element ref="orderItem"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="OrderItemRecord">
<xs:sequence>
<xs:element name="ItemId" type="xs:string"/>
<xs:element name="ProductName" type="xs:string"/>
<xs:element name="ProductId" type="xs:string"/>
<xs:element name="Quantity" type="xs:integer" default="1"/>
<xs:element name="UnitPrice" type="xs ecimal"/>
<xs:element name="TotalPrice" type="xs ecimal"/>
</xs:sequence></xs:complexType>
<xs:complexType name="OrderItem">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="orderItemRecord"/>
</xs:sequence>
</xs:complexType>
<xs:element name="orderItem" type="OrderItem"/>
<xs:element name="orderItemRecord" type="OrderItemRecord"/>
</xs:schema>
Any help is appreciated.
Regards
Kiranmanohar
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kiran manohar:
In the xml file there is an order like client-->order--->orderitem.now i want to track the sequence in which the schema file is written and in that order input the data in to my java object.


Nope, still not reading. Or at least I think I'm not. Are you trying to say that you want to parse the schema document first, and then parse the XML document so that the results of parsing the schema affect which information you pull out from the XML document?
Again, if you could post a piece of pseudo-code (programming language, not xml). Something like
PS. You can use the [CODE] UBB tags to pretty-print your code snippets.
 
kiran manohar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ur absolutely right lasse
i want to read the schema file first then the xml file, so that i want to insert data from the xml file based on the sequence that the schema file conforms.what i mean to say is in the schema file if the order is client then order and then orderitem tags then the xml file should also be in that sequence. then i need to insert the data based on that hierarchy.so i need to find a api which tells me the order in which i need to insert data.
i m giving the code which i used in java below

and AdapterNode is a class which is used to print the child nodes details using toString() method, like the nodes name and values using getNodeName() and getNodeValue().
Hope this code can give an insight into what i m trying to express.
Regards
Kiranmanohar

EDIT: I added the [CODE] tags for you. I hope you don't mind.
[ October 12, 2003: Message edited by: Lasse Koskela ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a suggestion:
  • First, validate the XML document against the schema document. If this validation fails, your XML document is invalid ("the xml file should also be in that sequence", remember?) and you need to take an exception path.
  • Second, parse the XML document with a custom SAX handler. When the handler encounters an element it needs to do something with, just do that something. You already know the element came in in the correct order.


  • How does that sound?
     
    kiran manohar
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi lasse,
    thanks for the suggestions. i get ur point.i will do it like that, but in the xml schema the elements tag type and name attributes values i need to retrieve . u know any api which does this.also first validating the xml schema and then parsing the xml file solves my problem. but i need to store the data found in the xml file in the same sequence.
    Thanks
    Kiranmanohar
     
    srinivasa varadhan
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi lasse,
    I got ur idea, there is an option in xerces parser,i.e

    this setFeature() comes under DOMParser class,but it's throwing an
    error:
    Unrecognized error (SAXNotRecognizedException):
    "http://apache.org/xml/features/validation/schema"
    Can u help me to solve this
    thanks
    Srinivas
    [email protected]
     
    Lasse Koskela
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Unrecognized error (SAXNotRecognizedException):
    "http://apache.org/xml/features/validation/schema"


    Are you using a version of Xerces that supports schema validation?
     
    Lasse Koskela
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    i will do it like that, but in the xml schema the elements tag type and name attributes values i need to retrieve . u know any api which does this.


    No, I don't know such an API, but I'm pretty sure that you'll find a suitable framework if you look into the source code for Xerces (it's open source...).

    also first validating the xml schema and then parsing the xml file solves my problem. but i need to store the data found in the xml file in the same sequence.

    I still insist that if the validation step doesn't complain, the elements in your XML document are in the exact same order as described by your schema document. Thus, you can just parse the XML document and handle elements in whatever order they are.
     
    srinivasa varadhan
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi lasse,
    we have xerces 1.0.4, i think it has validation for schema,
    but the thing is the feature what we setting there in

    it is showing the error was in
    "http://apache.org/xml/features/validation/schema" this parameter
    there is any alternate parameter to set a valid feature..
    and there is one more problem, that is we have to traverse the xsd file
     
    kiran manohar
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi lasse,
    one more thing , u said that parsing the xml schema would mean the xml file is in the same order.but in the xml schema file i will be having a parent child relationship , say a customer record and his contact details.
    so it would be like
    a parent section for customer name and his company details
    and a child section for contact address et al.
    i have to insert the customer name in one java object and the contact address in another java object , so for this i not only need to validate the xsd file with the xml file but also i have to get the sequence of the xml schema file, so that after the parent section is finished , i can proceed to the child section for inserting the java objects.i tried xerces but it looks too advanced.
    Thanks for ur suggestions once again
    Regards
    Kiranmanohar
     
    Lasse Koskela
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    we have xerces 1.0.4, i think it has validation for schema

    According to this page, the support for a feature switching schema validation on/off was only added in version 1.1.2 of Xerces. On the other hand, this would imply that schema validation is on by default so you might want to just catch the exception, comment that your version of Xerces is expected to throw it and that it's ok because ...
     
    Lasse Koskela
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Kiran, for this you really don't need to parse the schema document.
    You have two options:
    1) Parse the XML document into a DOM tree and pull the information you want afterwards in any order you wish
    2) Parse the XML document using a SAX handler and populate your objects as you go
    Here's a nice-looking tutorial for the second option.
     
    What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic