views:

68

answers:

4
+1  Q: 

XML with XSD JAVA

Hi All,

I have been assigned a work to validate a XML against an XSD and if everthing passed will parse the XML so that can import the same in to my system.

My Qyestion is whats the best way to validate an XML against the XSD and which is the best API for parsing the XML in to my domain object.

Looking for valuable suggestions

Thnaks in advance

A: 

JAXB is the Java standard XML parsing library: http://www.oracle.com/technetwork/articles/javase/index-140168.html. It comes bundled with Java SE 6.

Have a look at the tutorial. It's easy to register a schema file with your Unmarshaler/Marshaler and validate.

MikeG
No, just turn on "validate" in a parser. It's not at the OXM point yet.
duffymo
+1 for suggesting JAXB, since the second part of the question clearly asks about OXM.
Blaise Doughan
+1 from me also..will explore JAXB i was some what sutre about XML parsing but i m working first time over validationg XML against XSD
umesh
Wow, TWO downvotes? EDIT: Nevermind, forgot a downvote is -2. Anyway, thanks for the upvotes, Blaise and umesh.
MikeG
+1  A: 

You can use DOM or SAX parsers for these operation.

EDIT Here some example with sax parsing

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);

    SAXParser parser = factory.newSAXParser();
    parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
          "http://www.w3.org/2001/XMLSchema");

    XMLReader reader = parser.getXMLReader();

    reader.setErrorHandler(new ErrorHandler(){
          public void warning(SAXParseException e) throws SAXException {
                System.out.println(e.getMessage());
            }

            public void error(SAXParseException e) throws SAXException {
                System.out.println(e.getMessage());
            }

            public void fatalError(SAXParseException e) throws SAXException {
                System.out.println(e.getMessage());
            }

    });
    reader.parse(new InputSource("document.xml"));

(I'd took source from here)

Stas
I prefer DOm only for small files but since XML files can vary in size to quite extend i thik DOM will not perform as per the expectation
umesh
@umesh: I add some example with SAX parsing.
Stas
Hm.. I forgot add example. Here it is/
Stas
+1  A: 

Part 1 - Validate XML

You can use the javax.xml.validation APIs for this.

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
URL schemaURL = // The URL to your XML Schema; 
Schema schema = sf.newSchema(schemaURL); 
Validator validator = schema.newValidator();
validator.validate(xml);

Part 2 - OXM

Regarding the second part of your question, the best API for parsing XML into the domain object is JAXB. JAXB is a specification with multiple implementations. I lead the MOXy JAXB implementation that contains useful extensions such as XPath based mapping.

You could always do the validation during the conversion of XML to objects:

JAXBContext jc = JAXBContext.newInstance(Customer.class);
Umarshaller unmarshaller = jc.createUnmarshaller();

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
URL schemaURL = // The URL to your XML Schema; 
Schema schema = sf.newSchema(schemaURL); 
unmarshaller.setSchema(schema);

Customer customer = (Customer) unmarshaller.unmarshal(xml);
Blaise Doughan
Blaise i thik you have pointed me to the right direction will explore these options Thanks for the suggestion
umesh
Happy to help. Once you are convinced this is the answer please consider marking it "accepted".
Blaise Doughan
A: 

You can use Castor, that generate Java classes for your XSD. So you can convert your XML to objects and vice versa in few lines of code.

http://www.castor.org/1.3/index.html

franklins
You can integrate it with Eclipse:http://xdoclipse.sourceforge.net/presence/projects/castor/index.html
franklins