Question
· Dec 20, 2017

Xml how to get the record element Start and end

I am trying to read an xml document using %XML.TextReader and that's is all well and l can get my elements values  but would like to determine where the next record start on the xml without referring to the document path in essence would like to use the same method to read different xml docs. I would like to know if is there a way or a function that I can use to get the start and end of a record in xml  as I would to get the start and end element.

Typical example  l would like to get the highlighted paths please note this would need to go for any xml doc

<productListing title="ABC Products">
  <product> (GET THIS)
    <name>Product One</name>
    <description>Product One is an exciting new widget that will
      simplify your life.</description>
    <cost>$19.95</cost>
    <shipping>$2.95</shipping>
  </product> (or GET THIS)
 </productListing>
Discussion (6)1
Log in or sign up to continue

Because it's so easy I've added a solution for those readers that don't refuse to work with Caché Objects & Classes .

Class Definition

Class DC.productListing Extends (%RegisteredObject, %XML.Adaptor)                            
{                                                                                                                                                                          
Property product As %String(CONTENT = "MIXED", XMLPROJECTION = "CONTENT");
}                                                                                                                                                                          

Simple display method

ClassMethod Show(xml As %String(MAXLEN=""))
{
 set rd=##class(%XML.Reader).%New()
    ,sc=rd.OpenString(xml)
 do rd.Correlate("product","DC.productListing")
 while rd.Next(.obj) {
    write !,"######"_$i(cnt)_"#########",$replace(obj.product,$c(10),$C(13,10))
 }
}

Extended test data

USER>write xml
<productListing title="ABC Products">
 <product>
   <name>Product One</name>
   <description>Product One is an exciting new widget that will simplify your life.</description>
   <cost>$19.95</cost>
   <shipping>$2.95</shipping>
 </product>
 <product>
   <name>Product 2</name>
   <description>Product 2 is an exciti</description>
   <cost>$19.95</cost>
   <shipping>$2.95</shipping>
 </product>
 <product>
   <name>Product 3</name>
   <description>Product 3 is simplify your life.</description>
   <cost>$19.95</cost>
   <shipping>$2.95</shipping>
 </product>
</productListing>
USER>

Result

USER>do ##class(DC.productListing).Show(xml)
 
######1#########
  <name>Product One</name>
  <description>Product One is an exciting new widget that will simplify your life.</description>
  <cost>$19.95</cost>
  <shipping>$2.95</shipping>
 
######2#########
  <name>Product 2</name>
  <description>Product 2 is an exciti</description>
  <cost>$19.95</cost>
  <shipping>$2.95</shipping>
 
######3#########
  <name>Product 3</name>
  <description>Product 3 is simplify your life.</description>
  <cost>$19.95</cost>
  <shipping>$2.95</shipping>
 
USER>