Search This Blog

Thursday, February 21, 2013

XMLType


XMLType is a system-defined type for handling XML data.

You can create columns of XMLType and insert XML documents into it.


Functions and Procedures of XMLType

XMLType(xmlData) -- Constructor

   The actual data in the form of a CLOB, REF cursor, VARCHAR2 or object type.


declare
xmlclob clob:=
'<?xml version="1.0"?>
  <Family>
    <name> kiran </name>
    <name> vishali </name>
  </Family>';
testxmltype xmltype:=xmltype(xmlclob);
begin
  dbms_output.put_line(testxmltype.getclobval());
end;




createXML()
   for creating and returning an XMLType instance.
   The string and clob parameters used to pass in the date must contain well-formed and valid XML documents



existsNode()
   Checks if the node exists.
   If the XPath string is NULL or the document is empty, then a value of 0 is returned, otherwise returns 1.

   Given an XPath expression, checks if the XPath applied over the document can return any valid nodes.

declare
xmlclob clob:=
'<?xml version="1.0"?>
  <Family>
    <name> kiran </name>
    <name> vishali </name>
  </Family>';
testxmltype xmltype:=xmltype(xmlclob);
begin
  dbms_output.put_line(testxmltype.getclobval());
 
  if testxmltype.existsNode('/Family/name')=1 then
    dbms_output.put_line('node exists');
  else
    dbms_output.put_line('node does not exists');
  end if;
 
  if testxmltype.existsNode('//name')=1 then
    dbms_output.put_line('node exists');
  else
    dbms_output.put_line('node does not exists');
  end if;

end;


extract()

   Extracts an XMLType fragment and returns an XMLType instance containing the result nodes.
   If the XPath does not result in any nodes, then returns NULL.


declare
xmlclob clob:=
'<?xml version="1.0"?>
  <Family>
    <name> kiran </name>
    <name> vishali </name>
  </Family>';
testxmltype xmltype:=xmltype(xmlclob);
newxmltype xmltype;
begin
  dbms_output.put_line(testxmltype.getclobval());
 
  newxmltype:=testxmltype.extract('//name');
 
  dbms_output.put_line(newxmltype.getclobval());
 
  newxmltype:=testxmltype.extract('//name/text()');
 
  dbms_output.put_line(newxmltype.getclobval());
 
end;


isFragment()
   Determines if the XMLType instance corresponds to a well-formed document, or a fragment.
   Returns 1 or 0 indicating if the XMLType instance contains a fragment or a well-formed document.
   A fragment is a XML instance, which has more than one root element.


getClobVal()
   Returns a CLOB containing the seralized XML representation

getNumVal()
   Returns a numeric value, formatted from the text value pointed to by the XMLType instance

getStringVal()
   Returns the document as a string.
   Returns s string containing the seralized XML representation, or in case of text nodes, the text itself.

getRootElement()
   Gets the root element of the XMLType instance. Returns NULL if the instance is a fragment.


No comments:

Post a Comment