PL/SQL 中是否有将文本转换/编码为 XML 兼容文本的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/461970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:18:59  来源:igfitidea点击:

Is there a method in PL/SQL to convert/encode text to XML compliant text?

xmloracleplsql

提问by Bravax

I have a colleague who needs to convert text from a PL/SQL method into XML compliant text, as he is constructing a excel spreadsheet by updating a text template.

我有一位同事需要将文本从 PL/SQL 方法转换为符合 XML 的文本,因为他正在通过更新文本模板来构建 excel 电子表格。

Is there a method in PL/SQL to convert/encode text to XML compliant text?

PL/SQL 中是否有将文本转换/编码为 XML 兼容文本的方法?

回答by Powerlord

Well, if you just want to convert XML characters, you'll want to do something like...

好吧,如果你只是想转换 XML 字符,你会想要做一些像......

  outgoing_text := DBMS_XMLGEN.CONVERT(incoming_text)

Where outgoing_textand incoming_textare both VARCHAR2 or CLOB.

其中outgoing_textincoming_text都是 VARCHAR2 或 CLOB。

You can specify a second argument, but it defaults to DBMS_XMLGEN.ENTITY_ENCODE... it can also decode XML entities by passing DBMS_XMLGEN.ENTITY_DECODEas a second argument.

您可以指定第二个参数,但它默认为DBMS_XMLGEN.ENTITY_ENCODE... 它也可以通过DBMS_XMLGEN.ENTITY_DECODE作为第二个参数传递来解码 XML 实体。

回答by hamishmcn

Later versions of oracle have a built in XML packagefor manipulating XML data.
For example, is this the sort of thing your colleague wants to do?:

更高版本的 oracle 具有用于操作 XML 数据的内置XML 包
例如,这是您同事想做的事情吗?:

SELECT DBMS_XMLGEN.getXML('SELECT * FROM emp') FROM dual;

回答by Nick

In addition to dbms_xmlgen, you can use the sql method, xmlelement and then extract the value back out.

除了dbms_xmlgen,还可以使用sql方法,xmlelement然后把值提取回来。

select extract(xmlelement("mytest",my_variable),'mytest/text()') from dual;

The xmlelement function will make the text XML compliant, then using the extract function, it will extract the text out as is. (Note: The extractValue function will convert the text back to the non-XML compliant version.).

xmlelement 函数将使文本符合 XML,然后使用提取函数,它将按原样提取文本。(注意:extractValue 函数会将文本转换回不符合 XML 的版本。)。

回答by Rulas

hamischmcn option is easy and clear but it does not work in oracle forms, I tried a lot using that and nothing...you can generate the xml manually, using something like this

hamischmcn 选项简单明了,但它在 oracle 形式中不起作用,我尝试了很多使用它,但什么也没有......你可以使用类似这样的东西手动生成 xml

utl_file.put_line_nchar (file_id, 'xml version="1.0" encoding="utf-8"');

utl_file.put_line_nchar (file_id, 'xml version="1.0" encoding="utf-8"');

utl_file.put_line_nchar (file_id, 'Start');

utl_file.put_line_nchar (file_id, '开始');

--A FOR LOOP WHERE YOU DEFINE YOUR TAGS

--A FOR 循环,您可以在其中定义标签

utl_file.put_line_nchar (file_id, '/Start');

utl_file.put_line_nchar (file_id, '/Start');

Note: Editor does not allow me to put the < > signs in the xml and tags title

注意:编辑器不允许我在 xml 和标签标题中放置 < > 符号

回答by Jim Hudson

With respect to Rulas's issue on Oracle Forms, there are a lot of things Forms can't do (in various versions), where you need to use a database package.

关于Rulas在Oracle Forms上的问题,有很多Forms不能做的事情(在各种版本中),需要使用数据库包。

So write a database PL/SQL function that does the dbms_xmlgen and return a varchar2 or clob, depending on the size of your XML. Then you can call that function from Forms. when you get the data back into Forms, use text_io or webutil to push the XML file to Excel.

因此,编写一个执行 dbms_xmlgen 并返回 varchar2 或 clob 的数据库 PL/SQL 函数,具体取决于 XML 的大小。然后您可以从 Forms 调用该函数。当您将数据返回到 Forms 中时,使用 text_io 或 webutil 将 XML 文件推送到 Excel。

Or stay over on the database side and use utl_file to push the XML output to a directory where you can get at it.

或者留在数据库端并使用 utl_file 将 XML 输出推送到您可以获取的目录。