Question Krishnaveni Kapu · Apr 26, 2024

How to read XML data in a routing rule

I am receiving an XML input to my router and in the routing rule I need to traverse through the xml data , apply a for loop and fetch data.

How can I do that ?

Input :

<?xml version="1.0" encoding="UTF-8"?>
<Input>
   <data>
      <Ids>
         <Id>
            <Type>A</Type>
            <Value>123</Value>
         </Id>
         <Id>
            <Type>B</Type>
            <Value>456</Value>
         </Id>
      </Ids>
   </data>
</Input>

I would like to read 123 from above xml in a rule ..

Product version: IRIS 2023.3

Comments

Julius Kavay · Apr 26, 2024

The one way is to use %XML.TextReader

ClassMethod Reader(str, pth, ByRef val)
{
	kill val
	set val=0// Adjust the method name below to your needs// ParseString(), ParseStream(), ParseFile() if##class(%XML.TextReader).ParseString(str,.rdr) {
		while rdr.Read() {
			if rdr.NodeType="chars",rdr.Path=pth set val($i(val))=rdr.Value
		}
		quit1
	}
	quit0
}

set str="<?xml version=""1.0"" encoding=""UTF-8""?><Input><data><Ids><Id><Type>A</Type><Value>123</Value></Id><Id><Type>B</Type><Value>456</Value></Id></Ids></data></Input>"write##class(yourClass).Reader(str,"/Input/data/Ids/Id/Value",.val) --> 1zw val -->
val=2
val(1)=123
val(2)=456

The other way is to use %XML.Reader() and correlate to a class which describes your XML structure

0
Krishnaveni Kapu  Apr 26, 2024 to Julius Kavay

Thanks for your reply.

I wants to read in a routing rule when condition.(not in a class method)

0
Padmaja Konduru  Apr 26, 2024 to Krishnaveni Kapu

You can try like this in routing rule Document.SourceConfigName.

0