2

我正在使用 Oracle 11g ( 11.1.0.7.0 ),我必须创建一个查询的 XML 文件。我在 Forms 6 中使用这个文件,正是用户希望能够在按下表单中的按钮时创建 XML 文件,所以我有一个 PL/SQL 包,它创建一个带有 XML 数据的 CLOB 文件服务器和 Forms 6i 我逐行读取此文件行( UTL_FILE.FOPEN 读取它, UTL_FILE.GET_LINE 读取行)并使用 TEXT_IO.PUT_LINE 在客户端计算机上写入文件。这很好用,但我对 XML 文件有疑问。现在它看起来像这样(值是示例!):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<ShoeShop>
  <Article>
    <Artnumber>12345</Artnumber>
    <Artdesc>Black Shoes</Artdesc>
  </Article>
  <Article>
    <Artnumber>12346</Artnumber>
    <Artdesc>White Shoes</Artdesc>
  </Article>
</ShoeShop>

好的。我想创建一个如下所示的 XML 文件,但我不知道怎么做!我是 SQL/PLSQL 的新手,我学习到 2 个月,在此之前我使用过 Progress 4GL。所以在进行中,我把我想做的事情称为“嵌套”,但我不知道如何用 SQL/PLSQL 来实现它。XML 文件的示例我想如何获取它:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<ShoeShop>
  <Article="12345">
    <Artdesc>Black Shoes</Artdesc>
  </Article="12345">
  <Article="12346">
    <Artdesc>White Shoes</Artdesc>
  </Article="12346">
</ShoeShop>

创建 XML 文件的代码片段与第一个示例类似:

PROCEDURE XML_TO_CLOB( pi_Query      IN VARCHAR2,
                       pi_ParentNode IN VARCHAR2,
                       pi_ChildNode  IN VARCHAR2 ) IS
  qryCtx DBMS_XMLGEN.ctxHandle;
  cResult CLOB;
BEGIN
-- Create new Context for the Query
  qryCtx := DBMS_XMLGEN.newContext( pi_Query );

-- Set Parent and Child Node
  DBMS_XMLGEN.setRowSetTag( qryCtx, pi_ParentNode );
  DBMS_XMLGEN.SetRowTag( qryCtx, pi_ChildNode );

-- setNullHandling to show Tag also when the value is NULL
  DBMS_XMLGEN.setNullHandling( qryCtx, DBMS_XMLGEN.EMPTY_TAG );

-- getXML in CLOB
  cResult := DBMS_XMLGEN.getXML( qryCtx );

-- Put encoding to the "Header"
  cResult := REPLACE( cResult, '<?xml version="1.0"?>', '<?xml version="1.0" encoding="ISO-8859-1" ?>' );

-- Close Context
  DBMS_XMLGEN.closeContext( qryCtx );

-- Write the CLOB to a file on the server to work with the data in Forms 6i
  DBMS_XMSLPROCESSOR.CLOB2FILE( cResult, 'ExampleDir', 'Example.xml' );
END;

非常感谢,

莎拉

4

1 回答 1

1

There are some examples of using DBMS_XMLGEN to generate nested documents in the documentation. Check out example 17-27.

The Oracle solution involves using user-defined types to specify the shape of the document, including columns which will be included as attributes rather than elements. This might be more infrastructure than you were expecting.

于 2012-09-25T13:52:23.240 回答