拼接XML
- function ApplyDocToXML(docNo in varchar2) return clob is
- CURSOR c1(dN varchar2) IS
- SELECT ITEM_ID as DOCUMENT_ID,
- DOC_NAME as FILE_NAME,
- DOCUMENT_NAME,
- DOC_FILE as FILE_CONTENT
- FROM SupApplyDoc
- WHERE DOC_NO = dN;
- docItem c1%rowtype;
- fileClob clob;
- tempClob clob;
- resClob clob;
- begin
- dbms_lob.createtemporary(tempClob, FALSE, dbms_lob.call);
- dbms_lob.append(tempClob,
- '<?xml version="1.0" encoding="GBK"?><DOCUMENTDATA>');
- open c1(docNo);
- loop
- fetch c1
- into docItem;
- exit when c1%notfound;
- dbms_lob.append(tempClob,
- '<DOCUMENT><DOCUMENT_ID>' || docItem.document_id ||
- '</DOCUMENT_ID><DOCUMENT_NAME>' ||
- docItem.document_name || '</DOCUMENT_NAME><FILE_NAME>' ||
- docItem.file_name ||
- '</FILE_NAME><FILE_CONTENT><![CDATA['); -- CDATA 子集一定要这样写
- if docItem.file_content is not null then
- fileClob := Centit_LOB.BlobToBase64(docItem.file_content);
- dbms_lob.append(tempClob, fileClob);
- --insert into tmp_lob (n,vn,cv,bv) values(nc.nextval,docItem.file_name,fileClob,docItem.file_content);
- end if;
- dbms_lob.append(tempClob, ']]></FILE_CONTENT></DOCUMENT>'); --
- end loop;
- if c1%isopen then
- close c1;
- end if;
- dbms_lob.append(tempClob, '</DOCUMENTDATA>');
- --write_clob_to_file(result,'testApp.xml');
- --9i 无法将Clob写入文件,用一个临时表测试,这个表用后删除,见表语句如下
- /*
- create table tmp_lob (n number(10) not null primary key,vn varchar2(250), cv clob, bv blob);
- create sequence nc ;
- */
- --insert into tmp_lob (n,cv) values(nc.nextval,result);
- dbms_output.put_line('执行完成');
- resClob := tempClob;
- dbms_lob.freetemporary(tempClob);
- return resClob;
- end;
解析XML
- procedure XMLToApplyDoc(docNo in varchar2, xmlFile in clob) is
- theXmlDoc xmldom.DOMDocument;
- theDocElt xmldom.DOMElement;
- docNodeList xmldom.DOMNodeList;
- docItem xmldom.DOMNode;
- --tempElt xmldom.DOMElement;
- tempNodeList xmldom.DOMNodeList;
- tempNode xmldom.DOMNode;
- i number;
- len number;
- j number;
- clen number;
- fileNode xmldom.DOMNode;
- fileCon clob;
- fileClob clob;
- parser xmlparser.Parser;
- docRec SupApplyDoc%rowtype;
- nodeName varchar2(256);
- XMLParseError EXCEPTION;
- --PRAGMA EXCEPTION_INIT( XMLParseError, -20100 );
- begin
- parser := xmlparser.newParser;
- --dbms_output.put_line('初始化成功');
- xmlparser.parseClob(parser, xmlFile);
- --dbms_output.put_line('解释XML成功');
- theXmlDoc := xmlparser.getDocument(parser);
- xmlparser.freeParser(parser);
- if xmldom.IsNull(theXmlDoc) then
- return;
- end if;
- theDocElt := xmldom.getDocumentElement(theXmlDoc);
- docNodeList := xmldom.GETELEMENTSBYTAGNAME(theDocElt, 'DOCUMENT'); -- '*' 表示所有元素,包括根元素
- len := xmldom.getLength(docNodeList);
- -- 遍历所有元素
- for i in 0 .. len - 1 loop
- docItem := xmldom.item(docNodeList, i);
- --tempElt := xmldom.makeElement(docItem);
- tempNodeList := xmldom.getChildNodes(docItem);
- docRec.Doc_No := docNo;
- clen := xmldom.getLength(tempNodeList);
- for j in 0 .. clen - 1 loop
- tempNode := xmldom.item(tempNodeList, j);
- nodeName := xmldom.getNodeName(tempNode);
- --情况结构中的内容
- docRec.Doc_File := null;
- --dbms_output.put_line(nodeName);
- --这儿录入字段对应关系
- CASE nodeName
- WHEN 'DOCUMENT_ID' THEN
- -- 必需要加一个xmldom.getFirstChild,Oracle中的DOM将Value作为Node的一个子Node
- docRec.Item_Id := xmldom.getNodeValue(xmldom.getFirstChild(tempNode));
- --dbms_output.put_line(docRec.Item_Id);
- WHEN 'DOCUMENT_NAME' THEN
- docRec.Document_Name := xmldom.getNodeValue(xmldom.getFirstChild(tempNode));
- --dbms_output.put_line(docRec.Document_Name);
- WHEN 'FILE_NAME' THEN
- docRec.Doc_Name := xmldom.getNodeValue(xmldom.getFirstChild(tempNode));
- --dbms_output.put_line(docRec.Doc_Name);
- WHEN 'FILE_CONTENT' THEN
- begin
- fileNode := xmldom.getFirstChild(tempNode);
- if not xmldom.IsNull(fileNode) then
- dbms_lob.createtemporary(fileCon, FALSE, dbms_lob.call);
- xmldom.writeToClob(xmldom.getFirstChild(tempNode), fileCon);
- fileClob := fileCon; --xmldom.getNodeValue(xmldom.getFirstChild(tempNode)) ;-- fileCon;
- dbms_lob.freetemporary(fileCon);
- docRec.Doc_File := Centit_LOB.Base64ToBlob(fileClob);
- --dbms_output.put_line('Clob len: '||to_char(dbms_lob.getlength(fileClob))||' Blob len: '||to_char(dbms_lob.getlength(docRec.Doc_File )));
- end if;
- end;
- END CASE; end loop;
- --dbms_lob.write
- --dbms_output.put_line(docRec.Doc_Name);
- --write_clob_to_file(fileCon,docRec.Doc_Name);
- if (docRec.Doc_File is not null) then
- insert into tmp_lob
- (n, vn, cv, bv)
- values
- (nc.nextval, docRec.Doc_Name, fileClob, docRec.Doc_File);
- end if;
- -- 将附件插入到数据库中
- end loop;
- null;
- xmlparser.freeParser(parser);
- dbms_output.put_line('解释XML完成');
- --EXCEPTION
- --WHEN XMLParseError THEN
- -- xmlparser.freeParser(parser);
- --dbms_output.put_line('解释XML出错');
- --记录XML分析错误
- end;