Updated on 2024-05-07 GMT+08:00

DBE_XMLPARSER

API Description

The DBE_XMLPARSER API is used to deserialize XML character strings and convert the character strings that store XML documents to document nodes. Table 1 lists all APIs supported by the DBE_XMLPARSER advanced package.

The XMLParser data type can be used to store XMLParser data. The maximum number of XMLParser data records that can be stored is 16777215. The XMLPARSER data type can parse and create the DOMDocument node according to the input character string. The advanced package also provides the corresponding set and get APIs to perform operations on the constraint attributes of the parsing process.

When the DBE_XMLPARSER advanced package is used in the database whose character set is set to SQL_ASCII, an error is reported if characters beyond the ASCII range are input.

The DBE_XMLPARSER advanced package supports only the O-compatible mode.

Table 1 DBE_XMLPARSER parameters

API

Description

DBE_XMLPARSER.FREEPARSER

Frees a parser.

DBE_XMLPARSER.GETDOCUMENT

Obtains the parsed document node.

DBE_XMLPARSER.GETVALIDATIONMODE

Obtains the validation attribute.

DBE_XMLPARSER.NEWPARSER

Creates a parser instance.

DBE_XMLPARSER.PARSEBUFFER

Parses the VARCHAR string.

DBE_XMLPARSER.PARSECLOB

Parses the CLOB string.

DBE_XMLPARSER.SETVALIDATIONMODE

Sets the validation attribute.

  • DBE_XMLPARSER.FREEPARSER

    Frees a given parser object.

    The stored procedure prototype of DBE_XMLPARSER.FREEPARSER is as follows:

    1
    2
        DBE_XMLPARSER.FREEPARSER (
          p     IN     parser);
    
    Table 2 DBE_XMLPARSER.FREEPARSER parameters

    Parameter

    Description

    p

    Parser object

    Example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    -- Create a parser and then release it.
    DECLARE
      l_parser dbe_xmlparser.parser;
      BEGIN
      l_parser := dbe_xmlparser.newparser();
    -- Directly release the l_parser instance.
      dbe_xmlparser.freeparser(l_parser);
    END;
    /
    

    Result: The operation is successful.

  • DBE_XMLPARSER.GETDOCUMENT

    Returns the root node of the DOM tree document constructed by the parser. This function can be called only after the document is parsed.

    The prototype of the DBE_XMLPARSER.GETDOCUMENT function is as follows:

    1
    2
    3
    DBE_XMLPARSER.GETDOCUMENT (
      p     IN     parser) 
     RETURN DOMDocument;
    
    Table 3 DBE_XMLPARSER.GETDOCUMENT parameters

    Parameter

    Description

    p

    Parser object

    • If the GETDOCUMENT function has no input parameter, an error is reported.
    • If the parser parameter of the GETDOCUMENT function is null, NULL is returned.
    • If the parser input by the GETDOCUMENT function has not parsed any document, NULL is returned.
    Example:
     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
    -- Create a parser to parse character strings and print the obtained document.
    DECLARE
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
      buffer varchar2 := 
    '<?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Donot forget me this weekend!</body>
    </note>';
      buffer2 varchar2;
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      dbe_xmlparser.PARSEBUFFER(l_parser, buffer);
      l_doc := dbe_xmlparser.getdocument(l_parser);
      -- The l_parser parses the character string and obtains the DOMDocument node through GETDOCUMENT.
      dbe_xmldom.writetobuffer(l_doc, buffer2);
      RAISE NOTICE '%', buffer2;
      -- Print the content in l_doc.
      dbe_xmlparser.freeparser(l_parser);
      dbe_xmldom.freedocument(l_doc);
    END;
    /
    

    Execution result:

    1
    2
    3
    4
    5
    6
    7
    NOTICE:  <?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Donot forget me this weekend!</body>
    </note>
    
  • DBE_XMLPARSER.GETVALIDATIONMODE

    Obtains the parsing validation mode of a specified parser. If DTD validation is enabled, TRUE is returned. Otherwise, FALSE is returned.

    The prototype of the DBE_XMLPARSER.GETVALIDATIONMODE function is as follows:

    1
    2
    3
    DBE_XMLPARSER.GETVALIDATIONMODE (
      p     IN     parser) 
     RETURN BOOLEAN;
    
    Table 4 DBE_XMLPARSER.GETVALIDATIONMODE parameters

    Parameter

    Description

    p

    Parser object

    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    -- Create a parser and use GETVALIDATIONMODE to check whether the parser validation mode is enabled.
    DECLARE 
      l_parser dbe_xmlparser.parser;
    BEGIN
      l_parser := dbe_xmlparser.newparser();
        if (dbe_xmlparser.GETVALIDATIONMODE(l_parser) = true) then
     RAISE NOTICE 'validation';
        else 
        RAISE NOTICE 'no validation';
      end if;
      dbe_xmlparser.freeparser(l_parser);
    END;
    /
    

    Execution result:

    1
    NOTICE:  validation
    
  • DBE_XMLPARSER.NEWPARSER

    Creates a parser object and returns a new parser instance.

    The prototype of the DBE_XMLPARSER.NEWPARSER function is as follows:

    1
    2
    DBE_XMLPARSER.NEWPARSER 
     RETURN Parser;
    
    Example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    -- Create a parser to parse character strings and then free the parser.
    DECLARE
      -- Create a parser.
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
      buffer varchar2(1000) := 
        '<?xml version="1.0" encoding="UTF-8"?>
        <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Donot forget me this weekend!</body>
        </note>';
      buffer2 varchar2(1000);
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      -- Parse the document and create a new DOM document.
      dbe_xmlparser.PARSEBUFFER(l_parser, buffer);
    
      dbe_xmlparser.freeparser(l_parser);
    END;
    /
    

    Result: The operation is successful.

  • DBE_XMLPARSER.PARSEBUFFER

    Parses XML documents stored in strings.

    The stored procedure prototype of DBE_XMLPARSER.PARSEBUFFER is as follows:

    1
    2
    3
    DBE_XMLPARSER.PARSEBUFFER (
       p     IN     parser,
       doc   IN VARCHAR2);
    
    Table 5 DBE_XMLPARSER.PARSEBUFFER parameters

    Parameter

    Description

    p

    Parser object

    doc

    A string that stores XML documents

    • The maximum length of a character string that can be parsed by the PARSEBUFFER function is 32767. If the length exceeds the maximum, an error is reported.
    • Different from the ORA database, this database supports only UTF-8 in terms of character encoding, and version can only be set to 1.0. If versions 1.0 to 1.9 are parsed, a warning appears but the execution is normal. For versions later than 1.9, an error is reported.
    • DTD validation differences:
      • !ATTLIST to type (CHECK|check|Check) "Ch..." reports an error because the default value "Ch..." is not an enumerated value in the brackets. However, the ORA database does not report this error.
      • <!ENTITY baidu "www.baidu.com">...... &Baidu;&writer reports an error because the letters are case sensitive. Baidu cannot correspond to baidu. However, the ORA database does not report this error.
    • Namespace validation difference: Undeclared namespace tags are parsed. However, the ORA database reports an error.
    • Difference in parsing XML predefined entities: &apos; and &quot; are parsed and translated into ' and ". However, predefined entities in ORA database are not translated into characters.
    Example:
     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
    -- Create a parser to parse character strings and print the obtained document.
    DECLARE
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
      buffer varchar2 := 
    '<?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Donot forget me this weekend!</body>
    </note>';
      buffer2 varchar2;
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      dbe_xmlparser.PARSEBUFFER(l_parser, buffer);
      l_doc := dbe_xmlparser.getdocument(l_parser);
     
      dbe_xmldom.writetobuffer(l_doc, buffer2);
      RAISE NOTICE '%', buffer2;
    
      dbe_xmlparser.freeparser(l_parser);
      dbe_xmldom.freedocument(l_doc);
    END;
    /
    

    Execution result:

    1
    2
    3
    4
    5
    6
    7
    NOTICE:  <?xml version="1.0" encoding="UTF-8"?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Donot forget me this weekend!</body>
    </note>
    
  • DBE_XMLPARSER.PARSECLOB

    Parses XML documents stored in a CLOB.

    The stored procedure prototype of DBE_XMLPARSER.PARSECLOB is as follows:
    1
    2
    3
    DBE_XMLPARSER.PARSECLOB (
       p     IN     parser,
       doc   IN CLOB);
    
    Table 6 DBE_XMLPARSER.PARSECLOB parameters

    Parameter

    Description

    p

    Parser object

    doc

    A CLOB that stores XML documents

    • PARSECLOB cannot parse CLOBs larger than 1 GB.
    • Different from the ORA database, this database supports only UTF-8 in terms of character encoding, and version can only be set to 1.0. If versions 1.0 to 1.9 are parsed, a warning appears but the execution is normal. For versions later than 1.9, an error is reported.
    • DTD validation differences:
      • !ATTLIST to type (CHECK|check|Check) "Ch..." reports an error because the default value "Ch..." is not an enumerated value in the brackets. However, the ORA database does not report this error.
      • <!ENTITY baidu "www.baidu.com">...... &Baidu;&writer reports an error because the letters are case sensitive. Baidu cannot correspond to baidu. However, the ORA database does not report this error.
    • Namespace validation difference: Undeclared namespace tags are parsed. However, the ORA database reports an error.
    • Difference in parsing XML predefined entities: &apos; and &quot; are parsed and translated into ' and ". However, predefined entities in ORA database are not translated into characters.

    Example:

     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
    -- Create a parser to parse character strings and print the obtained document.
    DECLARE
    l_clob clob :=
     '<?xml version="1.0" encoding="UTF-8"?>
      <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>this weekend!</body>
      </note>';
      -- Create a parser.
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
      buffer varchar2(1000);
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      -- Parse the document and create a new DOM document.
      dbe_xmlparser.parseclob(l_parser, l_clob);
      l_doc := dbe_xmlparser.getdocument(l_parser);
      dbe_xmldom.writetobuffer(l_doc, buffer);
      RAISE NOTICE '%',buffer;
     
      dbe_xmlparser.freeparser(l_parser);
      dbe_xmldom.freedocument(l_doc);
     
      END;
      /
    

    Execution result:

    1
    2
    3
    4
    5
    6
    7
    NOTICE:  <?xml version="1.0" encoding="UTF-8"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>this weekend!</body>
    </note>
    
  • DBE_XMLPARSER.SETVALIDATIONMODE

    Sets the parsing validation mode of a specified parser.

    The stored procedure prototype of DBE_XMLPARSER.SETVALIDATIONMODE is as follows:

    1
    2
    3
    DBE_XMLPARSER.SETVALIDATIONMODE(
      p     IN     parser) 
      yes   IN BOOLEAN);
    
    Table 7 DBE_XMLPARSER.SETVALIDATIONMODE parameters

    Parameter

    Description

    p

    Parser object

    yes

    Mode to be set:
    • TRUE: DTD validation is enabled.
    • FALSE: DTD validation is disabled.
    • If the input parameter yes of the SETVALIDATIONMODE function is null, the parsing validation mode of the parser is not changed.
    • By default, the DTD validation is enabled during parser initialization.

    Example 1:

     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
    -- Create a parser. The XML character string to be parsed does not match the DTD format.
    -- If setValidationMode is set to false, the string can be parsed. If setValidationMode is set to true, an error is reported during parsing.
    DECLARE
      l_clob clob := 
     '<!DOCTYPE note [
     <!ELEMENT note (to,from,heading,body)>
     <!ELEMENT to (#PCDATA)>
     <!ELEMENT from (#PCDATA)>
     <!ELEMENT heading (#PCDATA)>
     <!ELEMENT body (#PCDATA)>
     ]>
     <table>
     <name attr1="WEB" attr2="web2">African Coffee Table</name>
     <width>80</width>
     <length>120</length>
     </table>';
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
     buffer varchar2(1000);
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      -- Set it to false for parsing.
        dbe_xmlparser.setValidationMode(l_parser, false);
        dbe_xmlparser.parseclob(l_parser, l_clob);
        l_doc := dbe_xmlparser.getdocument(l_parser);
        dbe_xmldom.writetobuffer(l_doc, buffer);
        RAISE NOTICE '%', buffer;
        dbe_xmlparser.freeparser(l_parser);
        dbe_xmldom.freedocument(l_doc);
      END;
      /
    

    Execution result:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    NOTICE:  <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE note [
    <!ELEMENT note (to , from , heading , body)>
    <!ELEMENT to (#PCDATA)>
    <!ELEMENT from (#PCDATA)>
    <!ELEMENT heading (#PCDATA)>
    <!ELEMENT body (#PCDATA)>
    ]>
    <table>
     <name attr1="WEB" attr2="web2">African Coffee Table</name>
     <width>80</width>
     <length>120</length>
     </table>
    

    Example 2:

     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
    -- Create a parser. The XML character string to be parsed does not match the DTD format.
    -- An error is reported during parsing after setValidationMode is set to true.
    DECLARE
      l_clob clob := 
     '<!DOCTYPE note [
     <!ELEMENT note (to,from,heading,body)>
     <!ELEMENT to (#PCDATA)>
     <!ELEMENT from (#PCDATA)>
     <!ELEMENT heading (#PCDATA)>
     <!ELEMENT body (#PCDATA)>
     ]>
     <table>
     <name attr1="WEB" attr2="web2">African Coffee Table</name>
     <width>80</width>
     <length>120</length>
     </table>';
      l_parser dbe_xmlparser.parser;
      l_doc dbe_xmldom.domdocument;
     buffer varchar2(1000);
      BEGIN
      l_parser := dbe_xmlparser.newparser();
      -- Set it to true for parsing.
    --The XML character string does not match the DTD format. An error is expected.
        dbe_xmlparser.setValidationMode(l_parser, true);
        dbe_xmlparser.parseclob(l_parser, l_clob);
        l_doc := dbe_xmlparser.getdocument(l_parser);
        dbe_xmldom.writetobuffer(l_doc, buffer);
        dbe_xmlparser.freeparser(l_parser);
        dbe_xmldom.freedocument(l_doc);
      END;
      /
    

    Execution result:

    1
    2
    An error is reported during xmlparser parsing.
    ERROR:  invalid XML document