Example 5-22: Posting XML-based news stories to another web server

CREATE OR REPLACE FUNCTION postNewsStory( story_headline VARCHAR2,
                                          story_source   VARCHAR2,
                                          story_url      VARCHAR2
                                        ) RETURN VARCHAR2 IS
  msg          VARCHAR2(32767);
  service_url  VARCHAR2(80);
  xml_response xmldom.DOMDocument;
  retval       VARCHAR2(10);
BEGIN
  -- This is the URL for the Post-a-New-Newstory Web Service
  service_url := 'http://xml/xsql/demo/insertxml/insertnewsstory.xsql';

  -- Prepare the XML document to post by "glueing" the values of
  -- the headline, news source, and url of the article into the
  -- XML message at the approrpriate places.
  msg := '<moreovernews>
           <article>
            <url>'|| story_url ||'</url>
            <headline_text>'|| story_headline ||'</headline_text>
            <source>'||story_source||'</source>
           </article>
          </moreovernews>';

  -- Post the xml document to the web service URL and get the Response
  xml_http.post(msg,service_url,xml_response);

  -- Check the response to see if it was a success.
  -- This service returns <xsql-status rows="1"/> if it was a success.
  IF xpath.test(xml_response,'/xsql-status/@rows="1"') THEN
    retval := 'Success';
  ELSE
    retval := 'Failed';
  END IF;

  -- Free the XML document
  xml.freeDocument(xml_response);

  -- Return the status
  RETURN retval;

EXCEPTION
  WHEN OTHERS THEN xml.freeDocument(xml_response); RAISE;
END;