Published on InterSystems Developer Community (https://community.intersystems.com)

Home > Get the XML Subtree from XPATH DOM Resultobject

Question
Raghu Kodumuri · Jul 14, 2016

Get the XML Subtree from XPATH DOM Resultobject

Hello,

How to get XML subtree from XPATH.DOMResult object as Stream or CacheString.

Here is my classmethod. My system receives XML message which has nested structures. The requirement is to send one message at a time to the destination. I have created the XPATH document and used EvaluateExpression method and which returns XML.XPATH.Result of type DOM. I am able to read the dom and get value of the element and its text. But I am looking to send back the subtree.

Code:

ClassMethod GetNode(tData As %CacheString, tExperssion As %String, tIndex As %Integer) As %CacheString
{
#dim tNodeText As %CacheString

    // Create an XPATH Document instance from the stream of XML
    Set tSC=##class(%XML.XPATH.Document).CreateFromString(tData,.tDocument)
if tDocument=$$$NULLOREF $$$ThrowStatus($$$ERROR($$$GeneralError,"Could not create XPath Document from Stream"))
zw tDocument
//Evaluate the XPATH expression
    Set tSC=tDocument.EvaluateExpression("//DIAGNOSES",tExperssion_"["_tIndex_"]",.tResults)
    zw tResults
if tResults=$$$NULLOREF $$$ThrowStatus($$$ERROR($$$GeneralError,"XPATH Expression evaluation failed."))


// Retrieve the XPATH Result
//$$$TRACE("XML Type:"_tResults.GetAt(1).Type)
set tResult=tResults.GetAt(1)
zw tResult
//do tResult.Read()
         While tResult.Read()
            {   
                If tResult.NodeType="element"
                {
                    Write !,tResult.NodeType,": ",tResult.Name  
                    
                    If tResult.HasAttributes {
                        For tJ=1:1:tResult.AttributeCount
                        {
                            Do tResult.MoveToAttributeIndex(tJ)
                            Write !,?9,tResult.NodeType,": ",tResult.Name,?25," Value: ",tResult.Value
                        }
                    }
                } else {
                    
                    Write !,tResult.NodeType," : ",tResult.Name," Value: "
                
                    // Value can be a stream if result is greater than 32k in length
                    Set tValue=tResult.Value
                
                    If $IsObject(tValue){
                        Write ! Do tValue.OutputToDevice()
                    } else {
                        Write tValue
                    }
                }
                Write !
            }

zw tResult
set tNodeText = tResult.ValueGet()
$$$TRACE("Number of Node of "_tExperssion_":"_tNodeText)

//return the Node Text
Quit tNodeText
}

test Command:

set tData="<HHSOS><DIAGNOSES ><DIAGNOSIS_DATA><DIAGNOSIS_DATA_GUID>3762875</DIAGNOSIS_DATA_GUID><DIAGNOSIS_DATA_GUID>37628752</DIAGNOSIS_DATA_GUID></DIAGNOSIS_DATA><DIAGNOSIS_DATA></DIAGNOSIS_DATA><DIAGNOSIS_DATA></DIAGNOSIS_DATA><DIAGNOSIS_DATA_GUID>37628753</DIAGNOSIS_DATA_GUID></DIAGNOSES></HHSOS>"

set t=##class(ISG.KAH.Devero.Utils.ToHorizonFunctions).GetNode(tData,"DIAGNOSIS_DATA",1)

Thank you

Raghu

#Caché #XML #HealthShare

Source URL: https://community.intersystems.com/post/get-xml-subtree-xpath-dom-resultobject