Salesforce

How to parse XML document received via HTTP client ?

« Go Back

Information

 
TitleHow to parse XML document received via HTTP client ?
URL NameHow-to-parse-XML-document-received-via-HTTP-client
Article Number000158348
EnvironmentProduct: OpenEdge
Version: 11.5.1, 11.6.x, 11.7.x, 12.x
OS: All supported platforms
Other: OpenEdge.Net.HTTP.HttpClient
Question/Problem Description
How to parse an XML object retrieved from a REST query? 
When invoking a REST service that returns a response in XML format, how to process the result ?
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
Assuming the response has the correct content types set etc.:

- The HTTP client will return the XML content loaded in an X-DOCUMENT object (= DOM tree). In this case Response:Entity will be of type OpenEdge.Core.WidgetHandle.
The X-DOCUMENT can be navigated as-is.

- To use an alternate parsing method (SAX-READER or dataset/temp-table READ-XML method), save the XML from this DOM tree to a temporary file or variable (MEMPTR or LONGCHAR). Then use that as input for the parser or choice. (All the methods involved have type of source/target and the source/target as the first 2 parameters)  For example:
USING OpenEdge.Core.*.
USING OpenEdge.Net.HTTP.*.
USING OpenEdge.Net.HTTP.Lib.ClientLibraryBuilder.
USING OpenEdge.Net.URI.

DEFINE VARIABLE oRequest     AS IHttpRequest         NO-UNDO.
DEFINE VARIABLE oResponse    AS IHttpResponse        NO-UNDO.
DEFINE VARIABLE oURI         AS URI                  NO-UNDO.
DEFINE VARIABLE oRequestBody AS OpenEdge.Core.String NO-UNDO.
DEFINE VARIABLE hXMLHandle   AS HANDLE               NO-UNDO.
DEFINE VARIABLE lcXML        AS LONGCHAR             NO-UNDO.
DEFINE VARIABLE hSAXReader   AS HANDLE               NO-UNDO.

// Build the http request here
<http request code>

// Execute the request and receive the response  
oResponse = ClientBuilder:Build()
                         :Client:Execute(oRequest).

IF oResponse:StatusCode <> 200 THEN
DO:
    MESSAGE "http error: " oResponse:StatusCode VIEW-AS ALERT-BOX. 
    RETURN ERROR "Request Error: " + STRING(oResponse:StatusCode).
END.
ELSE
DO:
    hXMLHandle = CAST(oResponse:Entity,WidgetHandle):Value.
    // Save the response to a LONGCHAR
    hXMLHandle:SAVE('LONGCHAR',lcXML).

    // XML response, Parse with SAX parser
    IF oResponse:ContentType = "text/xml" THEN
    DO:
        /* Create the SAX-reader object */
        CREATE SAX-READER hSAXReader.
        /* Tell the SAX-READER which XML file to parse. */
        hSAXReader:SET-INPUT-SOURCE("LONGCHAR", lcXML).
        hSAXReader:SAX-PARSE( ).
        DELETE OBJECT hSAXReader.
    END.  
END.

PROCEDURE StartElement:
    DEFINE INPUT PARAMETER namespaceURI AS CHARACTER NO-UNDO.
    DEFINE INPUT PARAMETER localName AS CHARACTER NO-UNDO.
    DEFINE INPUT PARAMETER qname AS CHARACTER NO-UNDO.
    DEFINE INPUT PARAMETER hAttributes AS HANDLE NO-UNDO.
    
    /* Which element in the XML is being read? */
    MESSAGE "StartElement : " qName VIEW-AS ALERT-BOX.
END PROCEDURE.

- If using the READ-XML approach, it's also possible to directly specify an X-Document or X-noderef handle directly to load from. This can be used to avoid writing the XML to a temporary location, in addition using an X-noreref object allows reading from XML fragments instead of the full document, which can be helpful if the full document is in a structure the READ-XML can't handle correctly.


If content types are not set correctly on the response:
- In most cases the Response:will be presented in a LONGCHAR. In this case  Response:Entity will be of type OpenEdge.Core.String. This can be loaded into the parser of choice.
Workaround
Notes
References to Other Documentation:
Progress Article(s):

 Is there an HTTP client for OpenEdge?
 
Keyword Phrase
Last Modified Date1/25/2023 1:57 PM

Powered by