It's not a secret anymore!*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 916

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: 39529
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

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: 39529
    
  27
That's hard to say without seeing the XML and the Schema.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

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: 916

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
 
Similar Threads
XML validation question
from jaxp program how to get schema/dtd name present in xml
Validate xml against schema
multiple validation errors in xml for given xsd with java
SAX XSD Validation. Error callback is called twice.