How to: JAXP, Xerces 2.6.2 and XML Schema ?

Discussion in 'XML' started by joes, Sep 9, 2004.

  1. joes

    joes Guest

    Hello there

    I tried for several days to get a simple validation with xml schema &
    xerces working. Goal for me is tuse JAXP and not specific Xerces
    classes. I don't get the point what I am doing wrong. Could somebody
    help me? I didn't find any full example working on the net. Thank you
    for any hints!

    If I run the examples below, the parsers parses the file well, no
    vlaidation is occuring although the schema and xml file does not
    match.

    I tried with the JAXP standard procedure, it does not work, why ?

    File f = new File("dvd.xml");
    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    saxFactory.setNamespaceAware(true);
    saxFactory.setValidating(true);
    SAXParser saxParser = saxFactory.newSAXParser();
    saxParser.parse(f, new DefaultHandler());

    I tried with activating additional SAX Features, does also not work,
    why ?

    File f = new File("dvd.xml");
    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    saxFactory.setNamespaceAware(true);
    saxFactory.setValidating(true);
    saxFactory.setFeature("http://xml.org/sax/features/validation",true);
    saxFactory.setFeature("http://apache.org/xml/features/validation/schema",true);
    saxFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
    SAXParser saxParser = saxFactory.newSAXParser();
    saxParser.parse(f, new DefaultHandler());

    Here is my xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <dvd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="dvd.xsd" id="33" available="3">Matrix
    Revolutions</dvd>

    Here is my xsd:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="dvdi" type="xs:string"/>
    <xs:attribute name="id" type="xs:string"/>
    <xs:attribute name="available" type="xs:boolean"/>
    </xs:schema>
    joes, Sep 9, 2004
    #1
    1. Advertising

  2. (joes) writes:

    > Hello there
    >
    > I tried for several days to get a simple validation with xml schema &
    > xerces working. Goal for me is tuse JAXP and not specific Xerces
    > classes. I don't get the point what I am doing wrong. Could somebody
    > help me? I didn't find any full example working on the net. Thank you
    > for any hints!


    Before you call SAXParserFactory.newInstance() you need to configure
    the implementation lookup mechanism to use Xerces (the current JDK
    1.4.x default is to use the Crimson parser).

    One method (see the javadoc for the SAXParserFactory.newInstance()
    method) would be to set a system property, from the command line using
    -D, or in the code:

    System.setProperty("javax.xml.parsers.SAXParserFactory",
    "org.apache.xerces.jaxp.SAXParserFactoryImpl");

    You need to do this in addition to having the xerces jar files in your classpath.

    Nigel
    Nigel Whitaker, Sep 10, 2004
    #2
    1. Advertising

  3. joes

    joes Guest

    Thank you Nigel

    But this is not the case. I do have already the xerces activated for
    my application. By the way you have not to configure this by using
    system properties. You can use the Jar service Provider Service
    mechanism. Means you have just to include xerces in the class path
    that's all.

    I still not have a solution for my issue above, so has someone a
    solution how to activate xml schema validation in xerces by using only
    JAXP and not xerces specific classes ?

    thank you
    regards
    mark


    Nigel Whitaker <> wrote in message news:<>...
    > (joes) writes:
    >
    > > Hello there
    > >
    > > I tried for several days to get a simple validation with xml schema &
    > > xerces working. Goal for me is tuse JAXP and not specific Xerces
    > > classes. I don't get the point what I am doing wrong. Could somebody
    > > help me? I didn't find any full example working on the net. Thank you
    > > for any hints!

    >
    > Before you call SAXParserFactory.newInstance() you need to configure
    > the implementation lookup mechanism to use Xerces (the current JDK
    > 1.4.x default is to use the Crimson parser).
    >
    > One method (see the javadoc for the SAXParserFactory.newInstance()
    > method) would be to set a system property, from the command line using
    > -D, or in the code:
    >
    > System.setProperty("javax.xml.parsers.SAXParserFactory",
    > "org.apache.xerces.jaxp.SAXParserFactoryImpl");
    >
    > You need to do this in addition to having the xerces jar files in your classpath.
    >
    > Nigel
    joes, Sep 10, 2004
    #3
  4. joes

    joes Guest

    Ok I found the solution by myself...
    Please refer to:
    http://xml.apache.org/~edwingo/jaxp-ri-1.2.0-fcs/docs/samples.html#Schema

    After that I simple tried the following JAXP, SAX & DOM solutions
    which work.
    While this is not documented at the Xerces Homepage in detail, I don't
    expect that this is working in any further releases, so be careful.

    For SAX, you have to use especially the underlying xmlReader with the
    saxparser interface from sun it is not working.

    File f = new File("dvd.xml");
    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    saxFactory.setNamespaceAware(true);
    saxFactory.setValidating(true);
    saxFactory.setFeature("http://apache.org/xml/features/validation/schema",
    true );
    SAXParser saxParser = saxFactory.newSAXParser();
    XMLReader xmlReader = saxParser.getXMLReader();
    xmlReader.parse(f.getAbsolutePath());


    A DOM solution would look and work like this:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setAttribute("http://apache.org/xml/features/validation/schema",
    Boolean.TRUE);
    dbf.setNamespaceAware(true);
    dbf.setValidating(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.parse(f);

    don't give up...we are just at the beginning
    regards
    Mark Egloff





    (joes) wrote in message news:<>...
    > Hello there
    >
    > I tried for several days to get a simple validation with xml schema &
    > xerces working. Goal for me is tuse JAXP and not specific Xerces
    > classes. I don't get the point what I am doing wrong. Could somebody
    > help me? I didn't find any full example working on the net. Thank you
    > for any hints!
    >
    > If I run the examples below, the parsers parses the file well, no
    > vlaidation is occuring although the schema and xml file does not
    > match.
    >
    > I tried with the JAXP standard procedure, it does not work, why ?
    >
    > File f = new File("dvd.xml");
    > SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    > saxFactory.setNamespaceAware(true);
    > saxFactory.setValidating(true);
    > SAXParser saxParser = saxFactory.newSAXParser();
    > saxParser.parse(f, new DefaultHandler());
    >
    > I tried with activating additional SAX Features, does also not work,
    > why ?
    >
    > File f = new File("dvd.xml");
    > SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    > saxFactory.setNamespaceAware(true);
    > saxFactory.setValidating(true);
    > saxFactory.setFeature("http://xml.org/sax/features/validation",true);
    > saxFactory.setFeature("http://apache.org/xml/features/validation/schema",true);
    > saxFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
    > SAXParser saxParser = saxFactory.newSAXParser();
    > saxParser.parse(f, new DefaultHandler());
    >
    > Here is my xml:
    >
    > <?xml version="1.0" encoding="utf-8" ?>
    > <dvd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:noNamespaceSchemaLocation="dvd.xsd" id="33" available="3">Matrix
    > Revolutions</dvd>
    >
    > Here is my xsd:
    >
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    > <xs:element name="dvdi" type="xs:string"/>
    > <xs:attribute name="id" type="xs:string"/>
    > <xs:attribute name="available" type="xs:boolean"/>
    > </xs:schema>
    joes, Sep 10, 2004
    #4
  5. "joes" <> wrote in message
    news:...
    > A DOM solution would look and work like this:
    >
    > DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    > dbf.setAttribute("http://apache.org/xml/features/validation/schema",
    > Boolean.TRUE);
    > dbf.setNamespaceAware(true);
    > dbf.setValidating(true);
    > DocumentBuilder db = dbf.newDocumentBuilder();
    > db.parse(f);


    Sun's JAXP tutorial includes a more complete example along these lines. The
    section "Reading XML Data into a DOM" illustrates necessary imports and
    error handling. See URL:

    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM3.html

    Section "Validating with XML Schema" is at URL:

    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM8.html#wp76446

    /kmc
    Keith M. Corbett, Sep 11, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    9,857
  2. Markus
    Replies:
    1
    Views:
    1,528
    Markus
    Nov 23, 2005
  3. bugbear
    Replies:
    0
    Views:
    1,009
    bugbear
    Aug 28, 2003
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,251
    Stanimir Stamenkov
    Apr 25, 2005
  5. Arvin Portlock
    Replies:
    0
    Views:
    144
    Arvin Portlock
    Jun 20, 2005
Loading...

Share This Page