XML decoding and encoding

示例Test.xml,内容如下

1
2
3
4
5
6
7
8
9
10
< staff content = "name and id">
     < member >
       < name >Arwen</ name >
     < eno >123</ eno >
   </ member >
   < member >
       < name >Tom</ name >
     < eno >456</ eno >
   </ member >  
</ staff >

 =============================================================
xml decode
使用xml decode方法将xml文件解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
decode_list_enterprise_request(xmldom.item (xmldom.getElementsByTagName (req_domdoc , 'staff' ), 0));
 
FUNCTION decode_staff (p_node IN DBMS_XMLDOM.DOMNode ) RETURN
staff  IS
l_obj staff ;
l_children DBMS_XMLDOM.DOMNodeList ;
l_length INTEGER ;
l_i INTEGER ;
l_child DBMS_XMLDOM.DOMNode ;
l_name VARCHAR2 (32767);
BEGIN
     l_obj  := staff();
     l_children  := DBMS_XMLDOM.getChildNodes (p_node );
     l_length  := DBMS_XMLDOM.getLength (l_children );
     l_i  := 0;
     WHILE  l_i  < l_length
     LOOP
         BEGIN
             l_child  := DBMS_XMLDOM.item (l_children , l_i );
             IF  DBMS_XMLDOM.getNodeType (l_child )!= DBMS_XMLDOM.ELEMENT_NODE  THEN
                 BEGIN l_i  := l_i  + 1; CONTINUE ; END ; END IF;
             l_name  := DBMS_XMLDOM.
                        getLocalName (DBMS_XMLDOM.makeElement (l_child ));
             IF  l_name  = 'member' THEN
                 BEGIN l_obj .member. add (decode_member(l_child )); END ;
             END IF;
             l_i  := l_i  + 1;
         END ;
     END LOOP;
     RETURN l_obj ;
END ;
 
FUNCTION decode_member (p_node IN DBMS_XMLDOM.DOMNode ) RETURN
member  IS
l_obj member ;
l_children DBMS_XMLDOM.DOMNodeList ;
l_length INTEGER ;
l_i INTEGER ;
l_child DBMS_XMLDOM.DOMNode ;
l_name VARCHAR2 (32767);
BEGIN
     l_obj  := member();
     l_children  := DBMS_XMLDOM.getChildNodes (p_node );
     l_length  := DBMS_XMLDOM.getLength (l_children );
     l_i  := 0;
     WHILE  l_i  < l_length
     LOOP
         BEGIN
             l_child  := DBMS_XMLDOM.item (l_children , l_i );
             IF  DBMS_XMLDOM.getNodeType (l_child )!= DBMS_XMLDOM.ELEMENT_NODE  THEN
                 BEGIN l_i  := l_i  + 1; CONTINUE ; END ; END IF;
             l_name  := DBMS_XMLDOM.
                        getLocalName (DBMS_XMLDOM.makeElement (l_child ));
             IF  l_name  = 'name' THEN
                 BEGIN l_obj . name := decode_string(l_child ); END ;
             END IF;
             IF  l_name  = 'eno' THEN
                 BEGIN l_obj .eno := decode_string(l_child ); END ;
             END IF;
             l_i  := l_i  + 1;
         END ;
     END LOOP;
     RETURN l_obj ;
END ;
 
FUNCTION decode_string (p_node IN DBMS_XMLDOM.DOMNode ) RETURN VARCHAR2  IS
l_children DBMS_XMLDOM.DOMNodeList ;
l_length INTEGER ;
l_i INTEGER ;
l_child DBMS_XMLDOM.DOMNode ;
l_char_data DBMS_XMLDOM.DOMCharacterData ;
BEGIN
     l_children  := DBMS_XMLDOM.getChildNodes (p_node );
     l_length  := DBMS_XMLDOM.getLength (l_children );
     l_i  := 0;
     WHILE  l_i  < l_length
     LOOP
         BEGIN
             l_child  := DBMS_XMLDOM.item (l_children , l_i );
             IF  DBMS_XMLDOM.getNodeType (l_child )
             = DBMS_XMLDOM.TEXT_NODE  THEN
                 BEGIN
                     l_char_data  := DBMS_XMLDOM.makeCharacterData (l_child );
                     RETURN DBMS_XMLDOM.getData (l_char_data );
                 END ;
             END IF;
             l_i  := l_i  + 1;
         END ;
     END LOOP;
     RETURN '' ;
END ;

  =============================================================
xml encode

FUNCTION encode_root_staff
(p_obj IN OUT staff ) RETURN DBMS_XMLDOM.DOMDocument  IS
l_response_node DBMS_XMLDOM.DOMNode ;
l_element DBMS_XMLDOM.DOMElement ;
l_node DBMS_XMLDOM.DOMNode ;
l_fcall_tmp DBMS_XMLDOM.DOMNode ;
BEGIN
    XmlResponse.response_doc  := DBMS_XMLDOM.newDOMDocument ();
    l_response_node := encode_staff (p_obj ,'staff');
    l_element  := DBMS_XMLDOM.makeElement (l_response_node );
    l_node  := DBMS_XMLDOM.makeNode (XmlResponse.response_doc );
    l_fcall_tmp  := DBMS_XMLDOM.appendChild (l_node , l_response_node );
    DBMS_XMLDOM.setAttribute (l_element , 'xmlns:ns2',
                              'http://edb.att.com/cci/v2.0')
    ;
    DBMS_XMLDOM.setAttribute (l_element , 'xmlns:ns1',
                              'http://cio.att.com/CommonHeader/v2')
    ;
    RETURN XmlResponse.response_doc ;
END;
 
FUNCTION encode_staff
(p_obj IN OUT staff , p_tag IN VARCHAR2 ) RETURN
DBMS_XMLDOM.DOMNode  IS
l_element DBMS_XMLDOM.DOMElement ;
l_node DBMS_XMLDOM.DOMNode ;
l_fcall_tmp DBMS_XMLDOM.DOMNode ;
BEGIN
    l_element  := DBMS_XMLDOM.createElement (XmlResponse.response_doc ,p_tag );
    l_node  := DBMS_XMLDOM.makeNode (l_element );
    IF  p_obj  IS NOT NULL THEN
        BEGIN
            IF  p_obj .member.count >0 THEN
            for i in 1..p_obj .member.count
            loop
                BEGIN
                    l_fcall_tmp  := DBMS_XMLDOM.appendChild (l_node ,encode_member(p_obj .member,'member'));
                    END;
            end loop;
            END IF;
        END;
    END IF;
    RETURN l_node ;
END;
 
FUNCTION encode_member
(p_obj IN OUT member , p_tag IN VARCHAR2 ) RETURN
DBMS_XMLDOM.DOMNode  IS
l_element DBMS_XMLDOM.DOMElement ;
l_node DBMS_XMLDOM.DOMNode ;
l_fcall_tmp_1 DBMS_XMLDOM.DOMNode ;
l_fcall_tmp DBMS_XMLDOM.DOMNode ;
BEGIN
    l_element  := DBMS_XMLDOM.createElement (XmlResponse.response_doc ,p_tag );
    l_node  := DBMS_XMLDOM.makeNode (l_element );
    IF  p_obj  IS NOT NULL THEN
        BEGIN
            IF  p_obj .name IS NOT NULL THEN
                BEGIN
                    l_fcall_tmp  := DBMS_XMLDOM.appendChild (l_node ,encode_string(p_obj .name,'name'));
                    END;
            END IF;
            IF  p_obj .eno IS NOT NULL THEN
                BEGIN
                    l_fcall_tmp  := DBMS_XMLDOM.appendChild (l_node ,encode_string(p_obj .eno,'eno'));
                    END;
            END IF;
        END;
    END IF;
    RETURN l_node ;
END;
 
FUNCTION encode_string (p_obj IN VARCHAR2 , p_tag IN VARCHAR2 ) RETURN
DBMS_XMLDOM.DOMNode  IS
l_node4 DBMS_XMLDOM.DOMNode ;
l_fcall_tmp_1 DBMS_XMLDOM.DOMNode ;
l_fcall_tmp DBMS_XMLDOM.DOMNode ;
BEGIN
    l_node4  := DBMS_XMLDOM.
                makeNode (DBMS_XMLDOM.createElement (XmlResponse.response_doc ,
                                                     p_tag ));
    IF  p_obj  IS NOT NULL THEN
        BEGIN
            l_fcall_tmp  := DBMS_XMLDOM.appendChild (l_node4 ,
                                                     DBMS_XMLDOM.
                                                     makeNode (DBMS_XMLDOM.
                                                               createTextNode (XmlResponse.
                                                                               response_doc ,
                                                                               p_obj )));
        END;
    ELSE
        BEGIN
            l_fcall_tmp_1  := DBMS_XMLDOM.appendChild (l_node4 ,
                                                       DBMS_XMLDOM.
                                                       makeNode (DBMS_XMLDOM.
                                                                 createTextNode (XmlResponse.
                                                                                 response_doc ,
                                                                                 ' ')));
        END;
    END IF;
    RETURN l_node4 ;
END;

 

转载于:https://www.cnblogs.com/Jeffrey-xu/p/5083291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值