JAXB und Validierung

Nikolaj

Mitglied
Hallo zusammen,

ich habe ein Problem mit der Validierung meiner eingelesenen XML Dokumente.
Meinen XML Dokument setzt sich aus 3 Dokumenten zusammen. 2 werden per <xi:include> eingebunden.

Die includes machten am Anfang Probleme, habe das aber dann auf diesem Weg gelöst:
Code:
JAXBContext jc = JAXBContext.newInstance(Catalog.class.getPackage().getName());
        UnmarshallerHandler unmarshallerHandler = jc.createUnmarshaller().getUnmarshallerHandler();

        // create a sax parser
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);

        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
        // resolves the included xml-files
        XIncludeFilter includer = new XIncludeFilter();
        includer.setParent(xmlReader);

        // write the resolved document to the UnmarshallerHandler if the 'parse()' method is called
        includer.setContentHandler(unmarshallerHandler);

        includer.parse(xmlFile);

        Data data = (Data) unmarshallerHandler.getResult();

Wie kann ich nun mein XML-Dokument ( hier: xmlFile ) gegen mein xsd bzw. die generierten Klassen validieren?
Ich möchte einfach ausschließen dass irgendwelche anderen XML Files eingelesen werden und dann irgendwelche Exceptions fliegen.
Komm grade nicht dahinter...

Gruß
Niko
 
Ok, habe es jetzt so gelöst:

Code:
        // Generate the JAXB Context and the unmarshaller
        JAXBContext jc = JAXBContext.newInstance(Catalog.class.getPackage().getName());
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        // The unmarshallerHandler holds the content of the xml document incl. the included documents
        UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();

        // Validation of the xml documents
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // TODO: Schemaauswahl auch in GUI packen? Jedesmal auswählen?
        Schema schema = sf.newSchema(new File("src/catalog/catalog.xsd"));
        unmarshaller.setSchema(schema);

        // create a sax parser
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);

        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
        // resolves the included xml-files
        XIncludeFilter includer = new XIncludeFilter();
        includer.setParent(xmlReader);

        // write the resolved document to the UnmarshallerHandler if the 'parse()' method is called
        includer.setContentHandler(unmarshallerHandler);

        includer.parse(xmlFile);

        Data data = (Data) unmarshaller.unmarshal(new JAXBSource(jc, unmarshallerHandler.getResult()));

Was mich daran ziemlich stört ist dass ich das *.xsd Schema angeben muss.
Wie kann ich denn an das drankommen.
Da ich ja sowieso nen SAXParser drüber laufen lasse, sollte ich doch irgendwie an das "*.xsd" drankommen, oder?

Gruß
Niko
 
JAXB generiert die Klassen und fügt die Validierung über Annotations hinzu.

PHP:
JAXBContext jc = JAXBContext.newInstance( "primer.po" );
# An Unmarshaller instance is created.

Unmarshaller u = jc.createUnmarshaller();
# The default JAXB Unmarshaller ValidationEventHandler is enabled to send to validation warnings and errors to system.out. The default configuration causes the unmarshal operation to fail upon encountering the first validation error.

u.setValidating( true );

Ansonsten, guck dir die Beispiele von Sun: http://java.sun.com/webservices/docs/2.0/tutorial/doc/JAXBUsing3.html#wp89378
 
http://java.sun.com/developer/EJTechTips/2006/tt0128.html hat gesagt.:
Where JAXB 2.0 differs from JAXB 1.0 in the unmarshalling process is validation. In JAXB 1.0 you could validate source data against an associated schema as part of the unmarshalling process by specifying:

unmarshaller.setValidating(true);

Validation capabilities have been expanded in JAXB 2.0 through the use of the JAXP 1.3 Schema Validation Framework. To use this enhanced level of validation, you first create an instance of the SchemaFactory for the W3C XML Schema 1.0 language:

SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

The SchemaFactory instance is used to create a new JAXP 1.3 Schema object:

Schema schema = sf.newSchema(new File("po.xsd"));

Then you use the setSchema method to identify the JAXP 1.3 Schema object (in this case, the po.xsd) that should be used to validate subsequent unmarshal operations. If you pass null into this method, it disables validation.

u.setSchema(schema);
Ich habe das Problem, dass die Methode
Code:
setValidating(boolean)
deprecated ist.
 
Hi,

aus dem Artikel kann ich leider auch nichts passendes herausziehen.
Auch hier wird immer ein Schema in Form von
Code:
Schema schema = sf.newSchema(new File("po.xsd"));
angegeben.

Warum überhaupt muss ich das Schema hier angeben, wenn ich mir doch bereits die Klassen aus einem XSD generiert habe?
Ist mir irgendwie nicht ganz klar. Ich sollte doch dann eigentlich alle Informationen in meinen JAVA Klassen haben, oder nicht?

Aber nochmal zum Thema:
Es kann doch nicht so schwer sein das Attribut xsi:noNamespaceSchemaLocation meinem XML Dokument zu entlocken, oder? Steh auf dem Schaluch.
 
Zurück