NOTE: Creating a new user is not strictly necessary,the user with the same (or greater) privileges as those listed in Step 1.
注意:必须建立一个新的用户方可执行。这个用户必须拥有跟步骤1同样的权限
Step 1: Creating the Database User like:
1.建立用户
- -- create the database user
- -- adjust tablespace names as necessary for your environment
- create user otnxml identified by demo
- default tablespace users
- temporary tablespace temp
- quota unlimited on users;
2.赋予权限
- -- grant system privileges and roles to the user
- grant create session,
- alter session,
- create table,
- create trigger,
- create type,
- create any directory,
- drop any directory,
- xdbadmin
- to otnxml;
create session—This privilege allows the user to create a session with the database. Without this privilege the user would not be able to connect (or log on) to the database. |
alter session—This privilege allows the user to alter attributes related to the session with the database. This is required to successfully execute the registerSchema procedure. |
create table—This privilege allows the user to create a table in the database. |
create trigger—This privilege allows the user to create a trigger on a table owned by the user. |
create type—This privilege allows the user to create a new type in the database. The XML elements will be persisted in the database using types. |
create any directory—This privilege allows the user to create a directory object. A directory object will be used to load the XML Instance Document into the database. This is a fairly powerful privilege and should be granted only to trusted users. Because directory objects reside in the system namespace within the database, the "any" keyword is required. There is no user-level "create directory" privilege. |
drop any directory—This privilege is the counterpart to the create any directory privilege. It allows the user to drop a directory object. Like the create any directory privilege, this privilege should be granted only to trusted users due to its system-level nature. |
xdbadmin role —This privilege allows the user to create directories in the XML DB engine |
Step 2: Creating the XML Schema
The xml file: d:/XML/Player.xml
- <?xml version="1.0"?>
- <Player xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="Player.xsd">
- <PlayerName>Steven Gerrard</PlayerName>
- <PlayerPosition>Midfield</PlayerPosition>
- <PlayerNumber>8</PlayerNumber>
- <PlayerClub>Localhost</PlayerClub>
- </Player>
XML Schemas define the elements of your XML files.
http://www.w3schools.com/schema/schema_simple.asp
根据xml文件,建立XML Schemas文件
The XML Schemas: d:/XML/Player.xsd
- <?xml version="1.0" ?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns:xdb="http://xmlns.oracle.com/xdb"
- xdb:storeVarrayAsTable="true" version="1.0">
- <xsd:element name="Player" xdb:defaultTable="PLAYER">
- <xsd:complexType xdb:SQLType="PLAYER_T">
- <xsd:sequence>
- <xsd:element name="PlayerName" type="xsd:string" minOccurs="1" xdb:SQLName="PLAYER_NAME" />
- <xsd:element name="PlayerPosition" type="xsd:string" minOccurs="1" xdb:SQLName="PLAYER_POSITION" />
- <xsd:element name="PlayerNumber" type="xsd:positiveInteger" minOccurs="1" xdb:SQLName="PLAYER_NUMBER" />
- <xsd:element name="PlayerClub" type="xsd:string" minOccurs="1" xdb:SQLName="PLAYER_CLUB" />
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
- </xsd:schema>
Step 3: Registering the XML Schema Document
1.建立xml路劲
- -- create the directory object that represents the directory2.
- -- where the schema and instance documents reside
- create directory OTNXML_DIR as ‘d:/XML’
2.注册XML Schemas文件
- -- register the xml schema with xml db
- begin
- dbms_xmlschema.registerSchema(
- schemaurl=>‘http://localhost:8080/home/OTNXML/source/xsd/Player.xsd’,
- schemadoc=>bfilename(‘OTNXML_DIR’,’Player.xsd’),
- local=>TRUE,
- gentypes=>TRUE,
- genbean=>FALSE,
- force=>FALSE,
- owner=>‘OTNXML’,
- csid=>nls_charset_id(‘AL32UTF8’)
- );
- end;
- /
注意:如果该注册信息错误,可以进行删除再建。以下是删除注册:
- BEGIN
- DBMS_XMLSCHEMA.deleteSchema(
- SCHEMAURL => ‘Player.xsd’,
- DELETE_OPTION => dbms_xmlschema.DELETE_CASCADE_FORCE);
- END;
- /
Step 4: Loading the XML Instance Document
- insert into player
- values(XMLType(bfilename(‘OTNXML_DIR’,’Player.xml’),nls_charset_id(‘AL32UTF8’) ));
- commit;
- select value(a).getClobVal() from player a ;
Results of the statement: 1 row selected. |
- select
- extractvalue (value(t),'/Player/PlayerName') PlayerName,
- extractvalue (value(t),'/Player/PlayerPosition') PlayerPosition,
- extractvalue (value(t),'/Player/PlayerNumber') PlayerNumber,
- extractvalue (value(t),'/Player/PlayerClub') PlayerClub
- from PLAYER t
Results of the statement: 1 row selected. |
复杂格式的xml文件处理
--------------------------------------------------------------------
--Player.xml----------------------------------------------------
--------------------------------------------------------------------
- <?xml version="1.0"?>
- <Player
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="Player.xsd">
- <H>
- <HName>aaa</HName>
- </H>
- <Player>
- <PlayerName>1a</PlayerName>
- <PlayerPosition>vxccvbvcv</PlayerPosition>
- <PlayerNumber>2</PlayerNumber>
- <PlayerClub>Localhost</PlayerClub>
- </Player>
- <Player>
- <PlayerName>2a</PlayerName>
- <PlayerPosition>vxccvbvcv</PlayerPosition>
- <PlayerNumber>2</PlayerNumber>
- <PlayerClub>Localhost</PlayerClub>
- </Player>
- <Player>
- <PlayerName>3a</PlayerName>
- <PlayerPosition>vxccvbvcv</PlayerPosition>
- <PlayerNumber>2</PlayerNumber>
- <PlayerClub>Localhost</PlayerClub>
- </Player>
- </Player>
--------------------------------------------------------------------
--Player.xsd的格式-------------------------------------------
--------------------------------------------------------------------
- <?xml version="1.0" ?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns:xdb="http://xmlns.oracle.com/xdb"
- xdb:storeVarrayAsTable="true"
- version="1.0">
- <xsd:element name="Player" type="Playertype" xdb:defaultTable="PLAYER"/>
- <xsd:complexType name="Playertype" xdb:SQLType="PLAYER_T">
- <xsd:sequence>
- <xsd:element name="Player" maxOccurs="unbounded" type="Playtype"/>
- <xsd:element name="H" maxOccurs="unbounded" type="Htype"/>
- </xsd:sequence>
- </xsd:complexType>
- <xsd:complexType name="Playtype">
- <xsd:sequence>
- <xsd:element name="PlayerName" type="xsd:string" minOccurs="1"
- xdb:SQLName="PLAYER_NAME" />
- <xsd:element name="PlayerPosition" type="xsd:string" minOccurs="1"
- xdb:SQLName="PLAYER_POSITION" />
- <xsd:element name="PlayerNumber" type="xsd:positiveInteger"
- minOccurs="1" xdb:SQLName="PLAYER_NUMBER" />
- <xsd:element name="PlayerClub" type="xsd:string" minOccurs="1"
- xdb:SQLName="PLAYER_CLUB" />
- </xsd:sequence>
- </xsd:complexType>
- <xsd:complexType name="Htype">
- <xsd:sequence>
- <xsd:element name="HName" type="xsd:string" minOccurs="1"
- xdb:SQLName="H_NAME" />
- </xsd:sequence>
- </xsd:complexType>
- </xsd:schema>
--------------------------------------------------------------------
--Player.xsd注册----------------------------------------------
--------------------------------------------------------------------
- BEGIN
- DBMS_XMLSCHEMA.deleteSchema(
- SCHEMAURL => 'Player.xsd',
- DELETE_OPTION => dbms_xmlschema.DELETE_CASCADE_FORCE);
- END;
- /
- begin
- dbms_xmlschema.registerSchema(
- schemaurl=>'Player.xsd',
- schemadoc=>bfilename('OTNXML_DIR','Player.xsd'),
- local=>TRUE,
- gentypes=>TRUE,
- genbean=>FALSE,
- force=>FALSE,
- owner=>'system',
- csid=>nls_charset_id('AL32UTF8')
- );
- end;
- /
--------------------------------------------------------------------
--XML表的操作----------------------------------------------
--------------------------------------------------------------------
- CREATE TABLE Player of XMLType
- XMLSCHEMA "Player.xsd"
- ELEMENT "Player";
- insert into player
- values(XMLType(bfilename('OTNXML_DIR','Player.xml'),nls_charset_id('AL32UTF8')));
- commit;
- Select
- extractValue(value(b),'/Player/H/HName') Hname,
- extractValue(value(b),'/Player/PlayerName') trName ,
- extractValue(value(b),'/Player/PlayerPosition') trPosition,
- extractValue(value(b),'/Player/PlayerNumber') trNumber,
- extractValue(value(b),'/Player/PlayerClub') trClub
- from
- Player, table(XMLSequence(extract(OBJECT_VALUE, '//Player'))) b
----当H为表头字段--------------------------------------------
- Select
- extractValue(OBJECT_VALUE,'/Player/H/HName') Hname,
- extractValue(value(b),'/Player/PlayerName') trName ,
- extractValue(value(b),'/Player/PlayerPosition') trPosition,
- extractValue(value(b),'/Player/PlayerNumber') trNumber,
- extractValue(value(b),'/Player/PlayerClub') trClub
- from
- Player, table(XMLSequence(extract(OBJECT_VALUE, '//Player'))) b
- ==等于==
- Select
- extractValue(value(a),'/Player/H/HName') Hname,
- extractValue(value(b),'/Player/PlayerName') trName ,
- extractValue(value(b),'/Player/PlayerPosition') trPosition,
- extractValue(value(b),'/Player/PlayerNumber') trNumber,
- extractValue(value(b),'/Player/PlayerClub') trClub
- from
- Player a, table(XMLSequence(extract(value(a), '//Player'))) b
- where extractValue(value(b),'/Player/H/HName') is null;
---------Update------------------------
- UPDATE Player p
- SET OBJECT_VALUE = updateXML(OBJECT_VALUE, '/Player/Player/PlayerName/text()', '21')
- WHERE extractValue(OBJECT_VALUE, '/Player/H/HName') = 'aaa';
- UPDATE Player
- SET OBJECT_VALUE = updateXML(OBJECT_VALUE, '/Player/H/HName/text()', 'wq')
- WHERE extractValue(OBJECT_VALUE, '/Player/H/HName') = 'aaa';
- UPDATE Player p
- SET OBJECT_VALUE = updateXML(OBJECT_VALUE,
- '/Player/Player/PlayerName/text()', '1221',
- '/Player/Player/PlayerPosition/text()','1234rr')
- WHERE existsNode(OBJECT_VALUE, '/Player/Player[PlayerName="1a"]') = 1;