Question
· Apr 9, 2020

How to get value from %XML.Text reader

I'm having trouble trying to navigate XML files USING the %XML.TextReader. I have read https://cedocs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GXML_textreader#GXML_textreader_creating_method
but I still don't really understand. 

 

I have the following block 

while textreader.Read()
    {
       
    set practices= practices_textreader.Name_":"
        If textreader.Value'="" 
        {
            set practices= practices_textreader.Value_" "
            }
            elseset practices= practices_"NOVALUE" _" "}
            
        }         write practices

The xml for all intents and purposes is 

<ENDPOINT>
            <ID>1</ID>
            <NAME>Test</NAME>
            <NACS>111111</NACS>

</ENDPOINT>

The output i get is 

NACS:NOVALUE     :111111    

Why is the value not attached to the textreader.Name for the name NACS. The XML is valid xml from an api call.

]

Discussion (2)1
Log in or sign up to continue

%XML.TextReader traverses XML nodes. Each node can have different type. NACS is node of type Element. It does not have any value. Node 111111 is of type Chars. However it does not have any name. Thus the output that you see.

Hint: change line in your code as follows to see types of nodes :

set practices = practices_textreader.Name_"("_textreader.NodeType_"):"

Take a look here for data model of XML: https://www.w3.org/XML/Datamodel.html

See also class reference for %XML.TextReader. Particularly for Value property. It explains what this property holds depending on type of node.

The code below does want you want.

    While textreader.Read() {
        If textreader.NodeType = "element"  {  
            Set elementName=textreader.Name  
        }
        ElseIf textreader.NodeType = "chars" {
            Set elementValue=textreader.Value
            Set practices=practices_elementName_":"_elementValue_" "
        }
    }
    Write practices