Question
· Nov 27, 2023

How do I access value in a specific node/element in an XML-file?

Hello!
I am trying access each "name" tag in the xml-file down below and want to display them through the trace in the production:

<?xml version="1.0"?>
<staff>
  <doc type="consultant">
    <name first="David" last="Marston">Mr. Marston</name>
    <name first="David" last="Bertoni">Mr. Bertoni</name>
    <name first="Donald" last="Leslie">Mr. Leslie</name>
    <name first="Emily" last="Farmer">Ms. Farmer</name>
  </doc>

</staff>

 

The code I have so far is this:
Method OnRequest(pRequest As Ens.StreamContainer, Output pResponse As Ens.Response) As %Status
{ $$$LOGINFO("Inne i XmlFilterProcess")
    set filename = pRequest.OutputFilename
    set stream = pRequest.Stream
    $$$LOGINFO(stream.Read())

 set status=##class(%XML.XPATH.Document).CreateFromStream(stream,.mydoc)
 set status=mydoc.EvaluateExpression("/staff/doc/name","1",.myresults)
 set count = myresults.Count()
 $$$LOGINFO(count)
 
  for =1:1:count
 {
 set result = myresults.GetAt(i)
 $$$LOGINFO(result)
 }
    Quit status
}

Down below is what the log shows. But I want to see all the values for each "name" tag.
I would be really thankful aswell if someone had a better way of doing this process! :)

Thanks beforehand for all the answers! :)

Product version: IRIS 2023.2
Discussion (2)2
Log in or sign up to continue
Method OnRequest(pRequest As Ens.StreamContainer, Output pResponse As Ens.Response) As %Status
{ $$$LOGINFO("Inne i XmlFilterProcess")
    set filename = pRequest.OutputFilename
    set stream = pRequest.Stream
    $$$LOGINFO(stream.Read())

 set status=##class(%XML.XPATH.Document).CreateFromStream(stream,.mydoc)
 set status=mydoc.EvaluateExpression("/staff/doc/name","text()",.myresults)
 set count = myresults.Count()
 $$$LOGINFO(count)
 
  for i =1:1:count
 {
 set result = myresults.GetAt(i).Value
 $$$LOGINFO(result)
 }
    Quit status
}

You need to change this two lines:

set status=mydoc.EvaluateExpression("/staff/doc/name","1",.myresults)
set status=mydoc.EvaluateExpression("/staff/doc/name","text()",.myresults)

set result = myresults.GetAt(i)
set result = myresults.GetAt(i).Value

Enrico