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

时间:2022-09-15 20:07:55

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

我是甲骨文的新手,而且我 - 也许是微不足道的 - 选择中的一个问题。 (我正在使用Oracle 10g Express Edition)。

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

我有一个带字段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>

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:

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

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?

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

6 个解决方案

#1


27  

Try this instead:

试试这个:

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

#2


3  

Try using xmltype.createxml(xml).

尝试使用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.

希望它可以帮到某人。

#3


0  

this query runs perfectly in my case

在我的情况下,此查询运行完美

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

#4


0  

You can achieve with below queries

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

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

    从mytab中选择extract(xmltype(xml),'// fax / text()')。getStringVal();

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

    从mytab中选择extractvalue(xmltype(xml),'// fax');

#5


0  

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.

您可以尝试从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);

Then use the below methods to extract node value

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

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

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

Refer more about DBMS_XMLDOM methods here.

请在此处详细了解DBMS_XMLDOM方法。

#6


0  

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

#1


27  

Try this instead:

试试这个:

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

#2


3  

Try using xmltype.createxml(xml).

尝试使用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.

希望它可以帮到某人。

#3


0  

this query runs perfectly in my case

在我的情况下,此查询运行完美

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

#4


0  

You can achieve with below queries

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

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

    从mytab中选择extract(xmltype(xml),'// fax / text()')。getStringVal();

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

    从mytab中选择extractvalue(xmltype(xml),'// fax');

#5


0  

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.

您可以尝试从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);

Then use the below methods to extract node value

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

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

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

Refer more about DBMS_XMLDOM methods here.

请在此处详细了解DBMS_XMLDOM方法。

#6


0  

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