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;

 

posted @ 2015-12-28 17:43  MorePrograms  阅读(287)  评论(0编辑  收藏  举报