阅读量:0
要查询XMLType数据,您可以使用Oracle数据库中的XMLQuery函数
- 首先,创建一个包含XMLType列的表:
CREATE TABLE xml_data ( id NUMBER PRIMARY KEY, xml_content XMLType );
- 向表中插入一些包含XML数据的记录:
INSERT INTO xml_data (id, xml_content) VALUES ( 1, XMLType('<book> <title>Book Title</title> <author>Author Name</author> <publisher>Publisher Name</publisher> <price>49.99</price> </book>') ); COMMIT;
- 使用XMLQuery查询XML数据:
SELECT id, XMLQuery('/book/title/text()' PASSING xml_content RETURNING CONTENT) AS title, XMLQuery('/book/author/text()' PASSING xml_content RETURNING CONTENT) AS author, XMLQuery('/book/publisher/text()' PASSING xml_content RETURNING CONTENT) AS publisher, XMLQuery('/book/price/text()' PASSING xml_content RETURNING CONTENT) AS price FROM xml_data;
这将返回以下结果:
ID | TITLE | AUTHOR | PUBLISHER | PRICE --------------------------------------------------------- 1 | Book Title | Author Name | Publisher Name | 49.99
在此示例中,我们使用/book/*
XPath表达式来提取book元素下的所有子元素(title、author、publisher和price)的文本内容。通过调整XPath表达式,您可以根据需要查询XML数据。