This discussion is archived
9 Replies Latest reply: Apr 13, 2012 4:57 AM by 929890 RSS

Registering an XML schema using Java

929890 Newbie
Currently Being Moderated
I tried to register an XM schema via Java

private void registerXmlSchema(javax.jcr.Session session, String xmlSchemaURL, String xmlElement) {
     javax.jcr.Session s = (javax.jcr.Session)session;
     OracleNodeTypeManager ntm;
     try {
          ntm = (OracleNodeTypeManager) s.getWorkspace().getNodeTypeManager();
          ntm.unregisterXMLSchema(xmlSchemaURL);
System.out.println("XML Schema deregistered...");
          ntm.registerXMLSchema(xmlSchemaURL, null);
System.out.println("XML Schema registered...");
     } catch (RepositoryException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
     }
}

but I always get a warning: WARNING: Session-1: sys/schemas/PUBLIC/mySchema.xsd is not a global XML Schema...

...and a subsequent attempt to create a table referencing this XML schema results in an SQLException:
ORA-31000: Resource 'sys/schemas/PUBLIC/mySchema.xsd' is not an XDB schema document.

Isn't it possible to register an XML schema from within a Java application like I can do with PL/SQL:
...
DBMS_XMLSchema.registerSchema(
'mySchema.xsd',
xml_schema,
false,
enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);
...


My session_privs:
CREATE SESSION
ALTER SESSION
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE ANY DIRECTORY
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

My session_roles:
CONNECT
RESOURCE
AUDDB_RW_ROLE
HISDB_RW_ROLE
TMP_XML_ROLE
XDBADMIN

Edited by: 926887 on 11-Apr-2012 03:57

Edited by: 926887 on 11-Apr-2012 04:43
  • 1. Re: Registering an XML schema using Java
    odie_63 Guru
    Currently Being Moderated
    Did you register it first (with local => false) in the database?

    http://docs.oracle.com/cd/E11882_01/appdev.112/e23094/xdb_jcr.htm#CHDBIHDB
  • 2. Re: Registering an XML schema using Java
    929890 Newbie
    Currently Being Moderated
    I hoped that I can do exactly this from within my Java application (using JCR or so), can't I?
    However, even when I registered the XML schema this way:

    DBMS_XMLSchema.registerSchema(
    'mySchema.xsd',
    xml_schema,
    false,
    enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);

    I get the same warning and subsequent exception.

    Does OracleNodeTypeManager.registerXMLSchema(xmlSchemaURL, null) not do the same as DBMS_XMLSchema.registerSchema(...) do? Is there no way to do DBMS_XMLSchema.registerSchema(...) functionality from within my Java application?

    I recognized that the parameters required from DBMS_XMLSchema.registerSchema(...) cannot be set by using OracleNodeTypeManager.registerXMLSchema(xmlSchemaURL, null) but I hope there is a way to provide OracleNodeTypeManager with the required data.
  • 3. Re: Registering an XML schema using Java
    odie_63 Guru
    Currently Being Moderated
    926887 wrote:
    However, even when I registered the XML schema this way:

    DBMS_XMLSchema.registerSchema(
    'mySchema.xsd',
    xml_schema,
    false,
    enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);

    I get the same warning and subsequent exception.
    OK, that's strange.

    At least, is the schema correctly registered in the database after you run DBMS_XMLSchema.registerSchema?
    select * from all_xml_schemas where schema_url = 'mySchema.xsd';
    Does OracleNodeTypeManager.registerXMLSchema(xmlSchemaURL, null) not do the same as DBMS_XMLSchema.registerSchema(...) do?
    No, it doesn't. That's two separate things.
    Read a little more from the documentation if you're not sure about the difference.
  • 4. Re: Registering an XML schema using Java
    929890 Newbie
    Currently Being Moderated
    At least, is the schema correctly registered in the database after you run DBMS_XMLSchema.registerSchema?
    select * from all_xml_schemas where schema_url = 'mySchema.xsd';
    Yes, when I execute
    DBMS_XMLSchema.registerSchema(
    'mySchema.xsd', 
    xml_schema, 
    false, 
    enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA);
    select * from all_xml_schemas where schema_url = 'mySchema.xsd';
    gets the desired result:

    OWNER SCHEMAURL LOCAL SCHEMA INTOBJNAME QUALSCHEMAURL HIERTYPE BINARY SCHEMAID HIDDEN
    xxx mySchema.xsd NO (null) XDvVLL9iMnbhTgRAAUT7eouA== mySchema.xsd RESMETADATA NO BINARY, 16 Bytes NO

    (How can I use tabs here???)

    Is there a possibility to register an XML schema from a Java application as I intend to do???
  • 5. Re: Registering an XML schema using Java
    odie_63 Guru
    Currently Being Moderated
    Is there a possibility to register an XML schema from a Java application as I intend to do???
    Sorry if I'm slow to understand but you didn't make it clear :)
    So basically what you're asking has nothing to do with using JCR? You only want to register a schema just like DBMS_XMLSCHEMA, from Java?

    PL/SQL is the way to go in this case anyway.
    As far as I know, there's no API to do that in Java. At the very least you can call the PL/SQL procedure from Java but that's about it.

    May I ask why you need it?
    Typically, a schema registration should be part of a deployment process (using SQL and PL/SQL scripts), and doesn't need to be instrumented by application code.
  • 6. Re: Registering an XML schema using Java
    929890 Newbie
    Currently Being Moderated
    I want to migrate XML container (and erspective documents as well) from Berkeley XML DB to Oracle XML DB and I intend to register an XML schema to be used as well programmatically.

    As you suggested I tried to use a CallableStatement to invoke DBMS_XMLSchema.registerSchema from within my Java application.
    However , with xmlSchemaURL = http://xmlns.oracle.com/xdb/schemas/RTIDB/mySchema.xsd

    cs = conn.prepareCall("{call DBMS_XMLSchema.registerSchema("?, ?, TRUE, enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA)}");

    results in a
    {quote}
    java.sql.SQLException: ORA-31153: Cannot create schema URL with reserved prefix "http://xmlns.oracle.com/xdb/schemas/"
    ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 3
    ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 14
    ORA-06512: at line 1
    {quote}

    I have to change the schema URL to be global: *sys/schemas/PUBLIC/mySchema.xsd* and then the 'TRUE' in the statement isn't considered to be local. t's public.

    Can I register an XML schema this way but for a certain user/schema (local)?

    Using
    {quote}

    cs = conn.prepareCall("{call DBMS_XMLSchema.registerSchema("
    //                         + "?, ?, TRUE, enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA)}");
                             + "?, ?, ?, ?, ?, ?, ?, enablehierarchy=>DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA)}");
                   cs.registerOutParameter(1, java.sql.Types.VARCHAR);
                   cs.setString(1, xmlSchemaURL);
                   cs.registerOutParameter(2, java.sql.Types.VARCHAR);
                   cs.setString(2, xsdBuffer.toString());
                   cs.registerOutParameter(3, java.sql.Types.VARCHAR);
                   cs.setString(3, "TRUE");
                   cs.registerOutParameter(4, java.sql.Types.VARCHAR);
                   cs.setString(4, "TRUE");
                   cs.registerOutParameter(5, java.sql.Types.VARCHAR);
                   cs.setString(5, "FALSE");
                   cs.registerOutParameter(6, java.sql.Types.VARCHAR);
                   cs.setString(6, "FALSE");
                   cs.registerOutParameter(7, java.sql.Types.VARCHAR);
                   cs.setString(7, "THEUSER");
    {quote}

    results in
    {quote}
    java.sql.SQLException: ORA-06550: line 1, column 7:
    PLS-00306: wrong number or types of arguments in call to 'REGISTERSCHEMA'
    ORA-06550: line 1, column 7:
    PL/SQL: Statement ignored
    {quote}

    using Boolean instead of String/VARCHAR results in *java.sql.SQLException: Invalid column type: 16*

    Mybe it's more pragmatical to register the XSD manually.
  • 7. Re: Registering an XML schema using Java
    MarcoGralike Oracle ACE Director
    Currently Being Moderated
    One question and one remark

    - WHY are you using "DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA" ???

    - You are NOT allowed, or said otherwise it is really bad practise, or even differently Oracle asks you not to register schema's under THEIR reserved namespace (http://xmlns.oracle.com/xdb/schemas) or the privileged by Oracle */SYS URL*. Invent your own, for example, http://www.mycompany.com/appx/version/1.0/schemas/myschema.xsd or use an alternative.
  • 8. Re: Registering an XML schema using Java
    929890 Newbie
    Currently Being Moderated
    I am new to that Oracle XML and XML Schema stuff and, of course, I was looking for self-explaining examples. So it came that I used examples as I fonud them without informing myself about "DBMS_XMLSCHEMA.ENABLE_HIERARCHY_RESMETADATA". :-o

    Thank you, Marco, for your hint to invent an own namespace. After defining an own namespace it seems to work as intended. :-))
  • 9. Re: Registering an XML schema using Java
    929890 Newbie
    Currently Being Moderated
    Well, it looks good but I have one remaining related question:
    When I register the XSD this way attempts to store an invalid XML document I get an exception. That's fine.
    But in opposite to registering the XML schema using SQL+ it seems as if no Object Types will be created. How can I achieve this?
    Using SQL+ immediately after schema registration the Object Types become visible.
    Using Java (CallableStatement) the schema gets registered but the Object Types are not created. I can't see them after registration using dbVisualizer.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points