从 Oracle 表生成 PL/SQL 中的 XML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1821819/
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
Generate XML document in PL/SQL from Oracle tables
提问by FrustratedWithFormsDesigner
I have to generate XML documents in PL/SQL from some tables in Oracle. I have never done this before, and I have found there seem to be a few main ways to do it:
我必须从 Oracle 中的某些表生成 PL/SQL 中的 XML 文档。我以前从未这样做过,我发现似乎有几种主要的方法可以做到:
- xmldom API
- xml functions (such as xmlelement, xmlagg, xmlroot)
- dbms_xmlgen functions
- xmldom API
- xml 函数(例如 xmlelement、xmlagg、xmlroot)
- dbms_xmlgen 函数
There are 65 tables that will be referenced to generate a single document, and I will have to validate the output against an xsd. The documents will be generated in a batch (rather than on-demand) - I don't know if that makes a difference. Using Oracle 10g.
有 65 个表将被引用以生成单个文档,我必须根据 xsd 验证输出。文档将批量生成(而不是按需生成)- 我不知道这是否有区别。使用 Oracle 10g。
I was initially leaning towards using the xmldom package as it looked more flexible, but I am having trouble finding good examples or documentation for it, whereas the xml functions seem better documented and generally more popular. Is there a reason for this?
我最初倾向于使用 xmldom 包,因为它看起来更灵活,但我很难找到好的示例或文档,而 xml 函数似乎有更好的文档记录并且通常更受欢迎。是否有一个原因?
What approach do people generally recommend for this type of task?
人们通常为此类任务推荐什么方法?
采纳答案by kurosch
In my experience, DBMS_XMLGEN is good for quick and dirty data-to-xml translations, but I've never liked it much because you have to pass the SQL as a string. Plus, your control over element names and ROWSET/ROW structure is severely limited.
根据我的经验,DBMS_XMLGEN 适用于快速且脏的数据到 xml 的转换,但我从来不喜欢它,因为您必须将 SQL 作为字符串传递。此外,您对元素名称和 ROWSET/ROW 结构的控制受到严重限制。
The XML functions are extremely handy, and my favorite, if you're dealing with relatively simple structures. Once you get into multiple XMLAgg levels, for instance, I find it devolves quickly into a confusing mess.
如果您要处理相对简单的结构,XML 函数非常方便,也是我的最爱。例如,一旦您进入多个 XMLAgg 级别,我发现它很快就会变成一团混乱。
XMLDOM is the most flexible way to generate XML, especially if the structure is more complex or there is iterative logic involved. The main drawback here is that its essentially a wrapper around the Java DOM where most of the methods accept a DOMNode input, but PL/SQL doesn't support polymorphism directly, so you end up with a lot of explicit casts between DOMElement and DOMNode and vice versa, etc. Generally I create my own package of overloaded procedures to encapsulate all of this and make it a little less painful to work with.
XMLDOM 是最灵活的生成 XML 的方式,尤其是在结构更复杂或涉及迭代逻辑的情况下。这里的主要缺点是它本质上是 Java DOM 的包装器,其中大多数方法接受 DOMNode 输入,但 PL/SQL 不直接支持多态性,因此最终会在 DOMElement 和 DOMNode 之间进行大量显式转换,反之亦然,等等。通常我会创建自己的重载程序包来封装所有这些,并使其使用起来不那么痛苦。
回答by Doug Porter
Here is a good discussion of some options available to you:
这是对您可用的一些选项的很好的讨论:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4980337843276
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4980337843276
Also if you search for "Sean Dillon" on AskTom you will get a good collection of articles detailing various problems and solutions using XML within Oracle.
此外,如果您在 AskTom 上搜索“Sean Dillon”,您将获得大量文章,这些文章详细介绍了在 Oracle 中使用 XML 的各种问题和解决方案。
回答by Marius Burz
Well, I never used Oracle for generating any very complex XML document, but using DBMS_XMLDOC is quite easy.
好吧,我从未使用 Oracle 生成任何非常复杂的 XML 文档,但使用 DBMS_XMLDOC 非常容易。
You may see a skeleton here(and here, only the pl/sql code). Also use Google Code Search, there is surely something more to find.
Then there is the DBMS_XMLDOM referencewhich helps even if the documentation is rather dry.
您可能会在这里看到一个骨架(在这里,只有 pl/sql 代码)。也使用谷歌代码搜索,肯定有更多的东西可以找到。
然后是DBMS_XMLDOM 参考,即使文档相当枯燥也有帮助。
Make sure you also read these two posts:
http://www.liberidu.com/blog/?p=365
http://www.liberidu.com/blog/?p=369
确保您还阅读了这两篇文章:
http: //www.liberidu.com/blog/ ?p= 365
http://www.liberidu.com/blog/?p=369
Alternatively you may look into generating XML using a Java Stored Procedure.
或者,您可以考虑使用 Java 存储过程生成 XML。
回答by Adam Hawkes
I generally use the xml functions (XMLElement
, XMLForest
, etc.) because I have control over the XSD. I make the XSD generally match the structure of the documents, so the things match together nicely.
我通常使用 xml 函数(XMLElement
、XMLForest
等),因为我可以控制 XSD。我使 XSD 通常与文档的结构相匹配,因此它们很好地匹配在一起。
If the schema you're trying to work with is exceptionally complex or funkyyou should consider the DBMS_XMLGEN
or DOM
methods.
如果您尝试使用的模式非常复杂或时髦,您应该考虑使用DBMS_XMLGEN
orDOM
方法。