Salve gente, ho un problemone con un validatore xml. La cosa strana è che sul pc del mio collega funziona e sul mio ricevo il seguente errore:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'x:books'

Classe java:

package it.grosso.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class Test {

private final static String xml =
"<?xml version=\"1.0\"?>" +
"<x:books xmlns:x=\"urn:books\">" +
" <book id=\"bk001\">" +
" <author>Writer</author>" +
" <title>The First Book</title>" +
" <genre>Fiction</genre>" +
" <price>22.01</price>" +
" <review>An amazing story of nothing.</review>" +
" <pub_date>2000-10-01</pub_date>" +
" </book>" +
" <book id=\"bk002\">" +
" <author>Poet</author>" +
" <title>The Poet's First Poem</title>" +
" <genre>Poem</genre>" +
" <price>24.95</price>" +
" <review>Least poetic poems.</review>" +
" <pub_date>2000-10-02</pub_date>" +
" </book>" +
"</x:books>";

private final static String nomeFile = "book.xsd";

public static void main(String[] args) {

validaXml(xml);

}

/**
*
* @param _xml
*/
private static void validaXml(String _xml) {

try {
Document d = generaDocument(_xml);

Validator v = generaValidatore( nomeFile );

//Validazione dell'xml
v.validate( new DOMSource( d ) );

System.out.println("-----> OK!");
}
catch (SAXException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
//catch (FactoryConfigurationError e) { e.printStackTrace(); }

}

/**
*
* @param xml
* @return
*/
private static Document generaDocument(String xml) {
Document d = null;

try {
//Creazione documento da stringa xml
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
d = parser.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
return d;
}
}

/**
*
* @return
*/
private static Validator generaValidatore(String nomeFile) {
Validator v = null;

try {
//aggiunto da me
String schemaLang = "http://www.w3.org/2001/XMLSchema";
//Creazione dello schema di validazione dell'xml
//SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
SchemaFactory schemaFactory = SchemaFactory.newInstance( schemaLang );
//schemaFactory.setErrorHandler( myErrorHandler );
Schema schemaXSD = schemaFactory.newSchema( new File ( nomeFile ) );
v = schemaXSD.newValidator();
} catch (SAXException e) {
e.printStackTrace();
}
finally {
return v;
}
}



}

File xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:bks="urn:books">

<xsd:element name="books" type="bks:BooksForm"/>

<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book"
type="bks:BookForm"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string" />
<xsd:element name="title" type="xsd:string" />
<xsd:element name="genre" type="xsd:string" />
<xsd:element name="price" type="xsd:float" />
<xsd:element name="review" type="xsd:string" />
<xsd:element name="pub_date" type="xsd:date" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>


Grazie in anticipo
Piccolognu