File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes xml validation with the java 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml validation with the java 5" Watch "xml validation with the java 5" New topic
Author

xml validation with the java 5

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi

I try to validate a a XML file using the JAXP from java 5. My validation look like :


but i run on a failure, on the root element.

I do something wrong ?

Regards,
M


SCJP, SCJD, SCWCD, OCPJBCD
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
What kind of failure? Is the document you're trying to validate actually valid according to the schema?

Sample code for Schema validation can also be found here.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi Ulf

Thanks for your answer. My problem is quite strange, I have an extern validation scheme - an xsd file and I try to validate with this scheme a Document. The Document is created at run time (it is not loaded from a file).
The next code snippet shows how I create the Document and its root node.


My validation does not find the root. I get an exception like :
Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Parameterset'.

where the Parameterset is the name for my root.
Even more strange if I wrote the generated document in to a file and I reload it (with docBuilder.parse(new File(file)) ) it works.

Why I can not validate the run time generated document ?

Regards M
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
That's hard to say without seeing the XML and the Schema.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi

I post you only a simplified version of my scheme

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation>Schema for Project PDC</xsd:documentation>
</xsd:annotation>
<!--____ Parameters _____-->
<xsd:element name="Parameterset">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Parameter" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Regards,
M
[ July 11, 2007: Message edited by: Mihai Radulescu ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Is it picking this XSD for validation?


Rahul Bhattacharjee
LinkedIn - Blog
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
I am not sure this is considerable or not but you have mis spelled your method ( missing 'l' in buildRoot method name)..



Thanks,
Ugender
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi guys,

Rahul, yes I use the XSD file for validation. The file is pretty big that why I post just one snippet. This sniper is enough to generate the SAXException (because it does not find the root node).

Ugender, thanks for tip.

In the mean time I do some research and I find a solution for my problem but I still can not explain it. It is like this : if I create the DOM document at run time and I validate it I get a SAXException but if I serialize the DOM in to a file and I parse this file back in to a DOM document, the validation works with the new parsed DOM. Quite strange.
This lead me to a conclusion - the DOM build at run time is wrong, but I don't see any error.
How about you guys ?

Regards,
M
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xml validation with the java 5