aspose file tools*
The moose likes XML and Related Technologies and the fly likes validate schema in DOM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "validate schema in DOM" Watch "validate schema in DOM" New topic
Author

validate schema in DOM

srinivasa varadhan
Greenhorn

Joined: Oct 03, 2003
Posts: 23
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,
srini579_key@hotmail.com


Srini...
Torch your Thoughts....
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
You can try Xerces DOM parser for validation, have a look here http://xml.apache.org/xerces2-j/faq-pcfp.html


Spritle Software Blogs
kiran manohar
Ranch Hand

Joined: Sep 28, 2003
Posts: 49
balaji,
can u give few examples for xml schema validation using Xerces. and how to setup/install the xerces package after downloading.
Thanks
Kiran
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Kiran, have you seen this tutorial?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
srinivasa varadhan
Greenhorn

Joined: Oct 03, 2003
Posts: 23
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Sep 28, 2003
Posts: 49
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Sep 28, 2003
Posts: 49
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

    Joined: Sep 28, 2003
    Posts: 49
    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

    Joined: Oct 03, 2003
    Posts: 23
    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
    srini579_key@hotmail.com
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    Unrecognized error (SAXNotRecognizedException):
    "http://apache.org/xml/features/validation/schema"

    Are you using a version of Xerces that supports schema validation?
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    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

    Joined: Oct 03, 2003
    Posts: 23
    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

    Joined: Sep 28, 2003
    Posts: 49
    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
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    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
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: validate schema in DOM
     
    Similar Threads
    XML/ DOM validation against Xml Schema
    Validate XML against XSD
    Validating input data passed to web service
    Marshalling using JAXB API
    XML/XSD to Database using Java