You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Mukul Gandhi <ga...@gmail.com> on 2008/06/16 11:05:45 UTC

Problem getting Schema URI with SAX Parser

Hi all,
   I am using Xerces-J 2.9.1 and JRE, 1.4.2_05.

I am trying to parse and validate a XML document with Xerces.

I supply XSD Schema to the parser as follows,

xsi:noNamespaceSchemaLocation="schemaFileName.xsd" (this is an
attribute at the root element of the instance document).

My Java program is as below (I have pasted only the relevant portions,
and not the complete program):

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
spf.setNamespaceAware(true);
SAXParser parser = spf.newSAXParser();
parser.setProperty(
"http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
parser.parse(new File(filename), this);

This works fine, and the program validates the XML document as expected.

But in this program, I want to get URI string (or the absolute file
path) of the Schema being used.

For this, I tried to change the program as follows:

//rest of the code here is same

parser.setProperty(
       "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
       "http://www.w3.org/2001/XMLSchema");
parser.parse(new File(filename), this);

String schmLoc = (String)
parser.getProperty("http://java.sun.com/xml/properties/jaxp/schemaSource");
System.out.println("schmLoc -->"+schmLoc);

But after running this program, I get the following error:

org.xml.sax.SAXNotRecognizedException: Property 'http://java.sun.com/xml/proper
ies/jaxp/schemaSource' is not recognized.
        at org.apache.xerces.parsers.AbstractSAXParser.getProperty(Unknown Sour
e)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.getProperty(Unkno
n Source)
        at org.apache.xerces.jaxp.SAXParserImpl.getProperty(Unknown Source)
        at SchmValidateSAX.parseFile(SchmValidateSAX.java:32)
        at SchmValidateSAX.main(SchmValidateSAX.java:17)

It seems, the XML parser does not set this property after it has
parsed and validated the XML document.

Can I achieve this with Xerces ...?

-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Problem getting Schema URI with SAX Parser

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
   Thanks for your reply and sharing your thoughts.

I'll attempt to write the ContentHandler for this, and would get back
to the list in case of doubts.

On 6/18/08, Michael Glavassevich <mr...@ca.ibm.com> wrote:
>
>
> Hi Mukul,
>
> It just doesn't work that way. Xerces has no business changing the value of
> "http://java.sun.com/xml/jaxp/properties/schemaSource".
> getProperty() must always return what the application set it to be (or null
> as the default) which isn't necessarily even a String (could be an
> InputStream, File, Object[], etc...). If you want to get the values of
> schema location hints in the document then you need to write a
> ContentHandler which does the work.
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Problem getting Schema URI with SAX Parser

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

It just doesn't work that way. Xerces has no business changing the value of
"http://java.sun.com/xml/jaxp/properties/schemaSource". getProperty() must
always return what the application set it to be (or null as the default)
which isn't necessarily even a String (could be an InputStream, File,
Object[], etc...). If you want to get the values of schema location hints
in the document then you need to write a ContentHandler which does the
work.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

"Mukul Gandhi" <ga...@gmail.com> wrote on 06/16/2008 12:12:45 PM:

> Hi Michael,
>   Thanks for your reply and letting me know the correct property name.
>
> But I am still not able to achieve what I want to do.
>
> As per your suggestion I modified the Java program as below:
>
> [1]
> parser.parse(new File(filename), this);
> String schmLocation = (String)
> parser.getProperty("http://java.sun.com/xml/jaxp/properties/schemaSource
");
> System.out.println(schmLocation);
>
> But I get the output:
> null
>
> (the exception now doesn't come)
>
> It seems, Xerces doesn't set this property by taking input from the
> XML document's hint (i.e., xsi:noNamespaceSchemaLocation).
>
> But when I change the program to (and remove the hint,
> xsi:noNamespaceSchemaLocation from the XML document):
>
> [1]
> parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource
",
> "schemaFileName.xsd");
> parser.parse(new File(filename), this);
> String schmLocation = (String)
> parser.getProperty("http://java.sun.com/xml/jaxp/properties/schemaSource
");
> System.out.println(schmLocation);
>
> I get the output:
> schemaFileName.xsd
>
> and the document also get's validated properly.
>
> But I need to achieve what is written in code fragment, [1]. i.e., I
> want to supply the Schema file hint (i.e.,
> xsi:noNamespaceSchemaLocation) in the XML document, and retrieved the
> Schema URI from the Java parsing program.
>
> Could you kindly suggest, how this can be achieved with Xerces.
>
> On Mon, Jun 16, 2008 at 9:14 PM, Michael Glavassevich
> <mr...@ca.ibm.com> wrote:
> > Hi Mukul,
> >
> > The property name you're querying is incorrect. It should be
> > "http://java.sun.com/xml/jaxp/properties/schemaSource" not
> > "http://java.sun.com/xml/properties/jaxp/schemaSource".
> >
> > Thanks.
> >
> > Michael Glavassevich
> > XML Parser Development
> > IBM Toronto Lab
> > E-mail: mrglavas@ca.ibm.com
> > E-mail: mrglavas@apache.org
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Re: Problem getting Schema URI with SAX Parser

Posted by Mukul Gandhi <ga...@gmail.com>.
On Mon, Jun 16, 2008 at 9:42 PM, Mukul Gandhi <ga...@gmail.com> wrote:
> But when I change the program to (and remove the hint,
> xsi:noNamespaceSchemaLocation from the XML document):
>

  [1] - Sorry, this should be [2]

> parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
> "schemaFileName.xsd");
> parser.parse(new File(filename), this);
> String schmLocation = (String)
> parser.getProperty("http://java.sun.com/xml/jaxp/properties/schemaSource");
> System.out.println(schmLocation);


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Problem getting Schema URI with SAX Parser

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
  Thanks for your reply and letting me know the correct property name.

But I am still not able to achieve what I want to do.

As per your suggestion I modified the Java program as below:

[1]
parser.parse(new File(filename), this);
String schmLocation = (String)
parser.getProperty("http://java.sun.com/xml/jaxp/properties/schemaSource");
System.out.println(schmLocation);

But I get the output:
null

(the exception now doesn't come)

It seems, Xerces doesn't set this property by taking input from the
XML document's hint (i.e., xsi:noNamespaceSchemaLocation).

But when I change the program to (and remove the hint,
xsi:noNamespaceSchemaLocation from the XML document):

[1]
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
"schemaFileName.xsd");
parser.parse(new File(filename), this);
String schmLocation = (String)
parser.getProperty("http://java.sun.com/xml/jaxp/properties/schemaSource");
System.out.println(schmLocation);

I get the output:
schemaFileName.xsd

and the document also get's validated properly.

But I need to achieve what is written in code fragment, [1]. i.e., I
want to supply the Schema file hint (i.e.,
xsi:noNamespaceSchemaLocation) in the XML document, and retrieved the
Schema URI from the Java parsing program.

Could you kindly suggest, how this can be achieved with Xerces.

On Mon, Jun 16, 2008 at 9:14 PM, Michael Glavassevich
<mr...@ca.ibm.com> wrote:
> Hi Mukul,
>
> The property name you're querying is incorrect. It should be
> "http://java.sun.com/xml/jaxp/properties/schemaSource" not
> "http://java.sun.com/xml/properties/jaxp/schemaSource".
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org


-- 
Regards,
Mukul Gandhi

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Problem getting Schema URI with SAX Parser

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

The property name you're querying is incorrect. It should be
"http://java.sun.com/xml/jaxp/properties/schemaSource" not
"http://java.sun.com/xml/properties/jaxp/schemaSource".

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

"Mukul Gandhi" <ga...@gmail.com> wrote on 06/16/2008 05:05:45 AM:

> Hi all,
>    I am using Xerces-J 2.9.1 and JRE, 1.4.2_05.
>
> I am trying to parse and validate a XML document with Xerces.
>
> I supply XSD Schema to the parser as follows,
>
> xsi:noNamespaceSchemaLocation="schemaFileName.xsd" (this is an
> attribute at the root element of the instance document).
>
> My Java program is as below (I have pasted only the relevant portions,
> and not the complete program):
>
> import javax.xml.parsers.SAXParser;
> import javax.xml.parsers.SAXParserFactory;
>
> SAXParserFactory spf = SAXParserFactory.newInstance();
> spf.setValidating(true);
> spf.setNamespaceAware(true);
> SAXParser parser = spf.newSAXParser();
> parser.setProperty(
> "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
> "http://www.w3.org/2001/XMLSchema");
> parser.parse(new File(filename), this);
>
> This works fine, and the program validates the XML document as expected.
>
> But in this program, I want to get URI string (or the absolute file
> path) of the Schema being used.
>
> For this, I tried to change the program as follows:
>
> //rest of the code here is same
>
> parser.setProperty(
>        "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
>        "http://www.w3.org/2001/XMLSchema");
> parser.parse(new File(filename), this);
>
> String schmLoc = (String)
>
parser.getProperty("http://java.sun.com/xml/properties/jaxp/schemaSource");
> System.out.println("schmLoc -->"+schmLoc);
>
> But after running this program, I get the following error:
>
> org.xml.sax.SAXNotRecognizedException: Property 'http://java.sun.
> com/xml/proper
> ies/jaxp/schemaSource' is not recognized.
>         at org.apache.xerces.parsers.AbstractSAXParser.
> getProperty(Unknown Sour
> e)
>         at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.
> getProperty(Unkno
> n Source)
>         at org.apache.xerces.jaxp.SAXParserImpl.getProperty(Unknown
Source)
>         at SchmValidateSAX.parseFile(SchmValidateSAX.java:32)
>         at SchmValidateSAX.main(SchmValidateSAX.java:17)
>
> It seems, the XML parser does not set this property after it has
> parsed and validated the XML document.
>
> Can I achieve this with Xerces ...?
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org