Question Thembelani Mlalazi · 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>

Comments

Fabian Haupt · Dec 20, 2017

So you're saying you don't want to just use xpath for that?

0
Thembelani Mlalazi  Dec 20, 2017 to Fabian Haupt

@Fabian Haupt I need help if Xpath will achieve what I am trying to do  so be it what I want is something generic for any XML to be processed if Xpath Has a generic way fine

0
Eduard Lebedyuk  Dec 20, 2017 to Thembelani Mlalazi

Can you show us your code?

To use XML reader you need to call Correlate method, which correlates any specified XML element to class.

0
Thembelani Mlalazi  Dec 20, 2017 to Eduard Lebedyuk

@eduard.lebeduk I am not using %XML.Reader hence the reason I need to find the alternative of reading the record open and close element I am trying to do away with creating object classes

0
Robert Cemper  Dec 20, 2017 to Thembelani Mlalazi

what's wrong with Caché Objects and Classes ?

I remember, you had a similar fear some time ago wink

0
Robert Cemper · Dec 20, 2017

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>
0