xml and xsd in java

Discussions

XML & Web services: xml and xsd in java

  1. xml and xsd in java (4 messages)

    How do I use java code for validating xmls using xsd ?
    I have an xml file and an xsd file.
    Can someone please let me know the java code that I can use to validate the xml with this xsd file ?
  2. xml and xsd in java[ Go to top ]

    Here is some code sample for validating an XML document with an XSD schema.

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    import javax.xml.XMLConstants;
    import org.w3c.dom.Document;
    import org.xml.sax.ErrorHandler;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    // ...
    // Exception handling is not shown.
    // The ErrorHandler implementation which could just do System.err dumps is not shown.

    // build an XSD-aware SchemaFactory
    SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );

    // hook up org.xml.sax.ErrorHandler implementation.
    schemaFactory.setErrorHandler( myErrorHandler );

    // get the custom xsd schema describing the required format for my XML files.
    Schema schemaXSD = schemaFactory.newSchema( new File ( "myschema.xsd" ) );

    // Create a Validator capable of validating XML files according to my custom schema.
    Validator validator = schemaXSD.newValidator();

    // Get a parser capable of parsing vanilla XML into a DOM tree
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    // parse the XML purely as XML and get a DOM tree represenation.
    Document document = parser.parse( new File( "myxml.xml" ) );

    // parse the XML DOM tree againts the stricter XSD schema
    validator.validate( new DOMSource( document ) );



    Nabil BENMIRA
  3. I have written the same code but,

    XMLConstants.W3C_XML_SCHEMA_NS_URI giving problem on JDK1.4

    giving Exception as IllegalArgumentException
    any help on this?

    Thanx in advance

    Shriniwas
  4. That is because the code snippet given here is for JDK 1.5
    I guess there should be a code equivalent for the same in JDK 1.4 also. Can anyone help out with the code for JDK 1.4 ?
  5. xml and xsd in java[ Go to top ]

    How do I use java code for validating xmls using xsd ?I have an xml file and an xsd file.Can someone please let me know the java code that I can use to validate the xml with this xsd file ?

    Thanks for replying..
    I am currently using jdk 1.4.
    Is there a way of doing the same thing using this version of Java ? Would be very helpful if the code could be explained. THanks