Oracle 10g:从 XML(CLOB 类型)中提取数据(选择)

发布于 2024-10-15 19:46:43 字数 823 浏览 3 评论 0原文

我是 Oracle 的新手,我在选择中遇到了一个问题(也许是微不足道的)。 (我使用的是 Oracle 10g Express 版)。

我有一个带有 CLOB 字段的数据库:mytab.xml 该列有一个像这样的 XML:

<?xml version="1.0" encoding="iso-8859-1"?>
<info>
<id> 954 </id>
<idboss> 954 </idboss>
<name> Fausto </name>
<sorname> Anonimo </sorname>
<phone> 040000000 </phone>
<fax> 040000001 </fax>
</info>

我正在尝试执行“简单”选择来获取“传真”标签的值。但我有一点问题,我无法理解我的错误。例如:

select extract(xml, '//fax').getStringVal() from mytab;
ORA-00932: inconsistent datatypes: expected - got

select extract(xmltype(xml), '//fax').getStringVal() from mytab;
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.XMLTYPE", line 254

我也尝试过“extractvalue”,但我遇到了同样的问题。 我这样做哪里错了?

I'm new in Oracle and I've - maybe trivial - a problem in a select. (I'm using Oracle 10g Express Edition).

I've a DB with a field CLOB: mytab.xml
This column have an XML like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<info>
<id> 954 </id>
<idboss> 954 </idboss>
<name> Fausto </name>
<sorname> Anonimo </sorname>
<phone> 040000000 </phone>
<fax> 040000001 </fax>
</info>

I'm trying to do a 'simple' select to get, for example, the value of 'fax' tag. But I've a bit of problem and I'm not able to understand my error. For example:

select extract(xml, '//fax').getStringVal() from mytab;
ORA-00932: inconsistent datatypes: expected - got

select extract(xmltype(xml), '//fax').getStringVal() from mytab;
ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "SYS.XMLTYPE", line 254

I've tryed also with 'extractvalue', but I've the same problems.
where I'm wrong to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

静待花开 2024-10-22 19:46:43

试试这个:

select xmltype(t.xml).extract('//fax/text()').getStringVal() from mytab t

Try this instead:

select xmltype(t.xml).extract('//fax/text()').getStringVal() from mytab t
月下客 2024-10-22 19:46:43

尝试使用xmltype.createxml(xml)

就像,

select extract(xmltype.createxml(xml), '//fax').getStringVal() from mytab;

它对我有用。

如果你想进一步改进或操纵。

尝试这样的事情。

Select *
from xmltable(xmlnamespaces('some-name-space' as "ns", 
                                  'another-name-space' as "ns1",
                           ), 
                    '/ns/ns1/foo/bar'
                    passing xmltype.createxml(xml) 
                    columns id varchar2(10) path '//ns//ns1/id',
                            idboss varchar2(500) path '//ns0//ns1/idboss',
                            etc....

                    ) nice_xml_table

希望它能帮助某人。

Try using xmltype.createxml(xml).

As in,

select extract(xmltype.createxml(xml), '//fax').getStringVal() from mytab;

It worked for me.

If you want to improve or manipulate even further.

Try something like this.

Select *
from xmltable(xmlnamespaces('some-name-space' as "ns", 
                                  'another-name-space' as "ns1",
                           ), 
                    '/ns/ns1/foo/bar'
                    passing xmltype.createxml(xml) 
                    columns id varchar2(10) path '//ns//ns1/id',
                            idboss varchar2(500) path '//ns0//ns1/idboss',
                            etc....

                    ) nice_xml_table

Hope it helps someone.

謸气贵蔟 2024-10-22 19:46:43

这个查询在我的情况下完美运行

select xmltype(t.axi_content).extract('//Lexis-NexisFlag/text()').getStringVal() from ax_bib_entity t

this query runs perfectly in my case

select xmltype(t.axi_content).extract('//Lexis-NexisFlag/text()').getStringVal() from ax_bib_entity t
死开点丶别碍眼 2024-10-22 19:46:43

您可以使用以下查询来实现

  1. select extract(xmltype(xml), '//fax/text()').getStringVal() from mytab;

  2. select extractvalue(xmltype(xml), '//fax') from mytab;

You can achieve with below queries

  1. select extract(xmltype(xml), '//fax/text()').getStringVal() from mytab;

  2. select extractvalue(xmltype(xml), '//fax') from mytab;

Saygoodbye 2024-10-22 19:46:43

您可以尝试从 CLOB XML 创建 DBMS_XMLPARSER.parser 对象,并从中获取 DBMS_XMLDOM.DOMDocument 对象。然后使用DBMS_XMLDOM包方法获取任意节点的值。

   xml_            CLOB := 'X';
   p               DBMS_XMLPARSER.parser;
   doc_            DBMS_XMLDOM.DOMDocument;

      -- Convert the CLOB into a XML-document
      P := DBMS_XMLPARSER.newparser();
      -- Parse the clob and get the XML-document
      DBMS_XMLPARSER.parseclob(p, xml_);
      doc_ := DBMS_XMLPARSER.getDocument(p);

然后使用以下方法提取节点值

DBMS_XMLDOM.getElementsByTagName(doc_, 'NodeName');
DBMS_XMLDOM.GetNodeValue(node_obj_);

有关 DBMS_XMLDOM 方法的更多信息,请参阅此处

You can try creating DBMS_XMLPARSER.parser object from the CLOB XML and get a DBMS_XMLDOM.DOMDocument object from it. Then use DBMS_XMLDOM package methods to get the value of any node.

   xml_            CLOB := 'X';
   p               DBMS_XMLPARSER.parser;
   doc_            DBMS_XMLDOM.DOMDocument;

      -- Convert the CLOB into a XML-document
      P := DBMS_XMLPARSER.newparser();
      -- Parse the clob and get the XML-document
      DBMS_XMLPARSER.parseclob(p, xml_);
      doc_ := DBMS_XMLPARSER.getDocument(p);

Then use the below methods to extract node value

DBMS_XMLDOM.getElementsByTagName(doc_, 'NodeName');
DBMS_XMLDOM.GetNodeValue(node_obj_);

Refer more about DBMS_XMLDOM methods here.

远昼 2024-10-22 19:46:43

如果是:

<?xml version="1.0" encoding="iso-8859-1"?>
<info xmlns="http://namespaces.default" xmlns:ns2="http://namespaces.ns2" >
    <id> 954 </id>
    <idboss> 954 </idboss>
    <name> Fausto </name>
    <sorname> Anonimo </sorname>
    <phone> 040000000 </phone>
    <fax> 040000001 </fax>
</info>

查询:

Select *
from xmltable(xmlnamespaces(default 'http://namespaces.default'
                              'http://namespaces.ns2' as "ns",
                       ), 
                '/info'
                passing xmltype.createxml(xml) 
                columns id varchar2(10) path '/id',
                        idboss varchar2(500) path '/idboss',
                        etc....

                ) nice_xml_table

In case of :

<?xml version="1.0" encoding="iso-8859-1"?>
<info xmlns="http://namespaces.default" xmlns:ns2="http://namespaces.ns2" >
    <id> 954 </id>
    <idboss> 954 </idboss>
    <name> Fausto </name>
    <sorname> Anonimo </sorname>
    <phone> 040000000 </phone>
    <fax> 040000001 </fax>
</info>

Query :

Select *
from xmltable(xmlnamespaces(default 'http://namespaces.default'
                              'http://namespaces.ns2' as "ns",
                       ), 
                '/info'
                passing xmltype.createxml(xml) 
                columns id varchar2(10) path '/id',
                        idboss varchar2(500) path '/idboss',
                        etc....

                ) nice_xml_table
瑾兮 2024-10-22 19:46:43

如果 XML 存储在数据库表的 CLOB 字段中。例如,对于此 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Awmds>
    <General_segment>
        <General_segment_id>
        <Customs_office_code>000</Customs_office_code>
    </General_segment_id>
</General_segment>
</Awmds>

这是提取查询:

SELECT EXTRACTVALUE (
    xmltype (T.CLOB_COLUMN_NAME),
    '/Awmds/General_segment/General_segment_id/Customs_office_code')
    AS Customs_office_code
FROM TABLE_NAME t;

In case the XML store in the CLOB field in the database table. E.g for this XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Awmds>
    <General_segment>
        <General_segment_id>
        <Customs_office_code>000</Customs_office_code>
    </General_segment_id>
</General_segment>
</Awmds>

This is the Extract Query:

SELECT EXTRACTVALUE (
    xmltype (T.CLOB_COLUMN_NAME),
    '/Awmds/General_segment/General_segment_id/Customs_office_code')
    AS Customs_office_code
FROM TABLE_NAME t;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文