locked
Get data into cursor from XML file RRS feed

  • Question

  • Hi,

     

    I'm trying to open XML file's, but i´m not abble to open.

     

    Can someone give any help.

     

    Thanks,

    Tuesday, April 8, 2008 8:28 AM

Answers

  •  Luis Moura wrote:

    I mean read it from a xml file and pass data into cursor, i have found an exemple but for my case i can't get it to read all the tag's:

     

    ************************************************************************************

    ** The following will create the Microsoft XML parser and will load

    ** the XML document specified in the textbox

    ************************************************************************************

    PUBLIC oxmldoc

    oxmldoc = CREATEOBJECT('msxml2.domdocument')

    oxmldoc.ASYNC = .F. && False

    IF EMPTY(THISFORM.txtxmldoc.VALUE)

      THISFORM.txtxmldoc.VALUE = GETFILE('xml')

    ENDIF

     

    but i only can reach de second level of tag's, ex: in tag 'parceiros' e get the value 'SU 5550000022223' and so on. it i sould get the tag 'tipo' value 'SU', tag 'ean' value '5550000022223'

     

    If you have a copy of MegaFox: 1002 Things You Wanted to Know About Extending VFP, there is a data-driven class in Chapter 17 that does something similar to what you want. You may also be able to do this more easily using an xmlAdapter, but I have not used it very much myself. You do not say which version of VFP you are using - the xmlAdapter is available in versions 8 & 9.

    Thursday, April 10, 2008 9:28 PM

All replies

  • What do you mean exactly? You can open an XML file using notepad, it's a text file.

     

    VFP has XMLToCursor() function, XMLAdapter class for loading data from an XML file. However either are not capable reading all types of XML though the XML might be totally valid. You may need to use DOM parser or ADO DSO provider.

    ie: VFP can read XML like this w/o any problems:

     

    SET CENTURY on
    TEXT TO m.lcXML noshow
    <myData>
     <myrow myid="15" personname="Joe Doe" bdate="1964-01-30"/>
     <myrow myid="26" personname="Frank Smith" bdate="1940-07-02"/>
     <myrow myid="37" personname="Smith Brown" bdate="2000-02-29"/>
    </myData>
    endtext
    XMLTOCURSOR(m.lcXML,'myTest')
    BROWSE

     

     

    Tuesday, April 8, 2008 9:51 AM
  • Hi,

     

    thanks for your reply.

     

    I mean read it from a xml file and pass data into cursor, i have found an exemple but for my case i can't get it to read all the tag's:

     

    This is the way i am trying to read the XML file:

    --------------------------------------------------------------------

    ON ERROR *

    ************************************************************************************

    *THISFORM.txtXMLDoc.VALUE = 'weather.xml'

    *THISFORM.txtXMLDoc.VALUE = 'mini_ord.xml'

    ************************************************************************************

    ** The following will create the Microsoft XML parser and will load

    ** the XML document specified in the textbox

    ************************************************************************************

    PUBLIC oxmldoc

    oxmldoc = CREATEOBJECT('msxml2.domdocument')

    oxmldoc.ASYNC = .F. && False

    IF EMPTY(THISFORM.txtxmldoc.VALUE)

    THISFORM.txtxmldoc.VALUE = GETFILE('xml')

    ENDIF

     

    oxmldoc.LOAD(THISFORM.txtxmldoc.VALUE)

    IF oxmldoc.ParseError.ErrorCode = 0

    * messagebox("Succeeded")

    IF oxmldoc.readyState = 4

    **************************************************************************************

    ** Treeview Add Node information

    ** The NodeKey must be unique

    ** TreeView.nodes.add(WhoIsThisNodeRelatedTo, HowIsThisNodeRelated, NodeKey, NodeText)

    ** HowIsThisNodeRelated Parameters:

    ** tvwLast (1) The Node is placed after all other nodes at the same level of the

    ** node named in "relative"

    ** tvwNext (2) The Node is placed after the node named in "relative"

    ** tvwPrevious (3) The Node is placed before the node named in "relative"

    ** tvwChild (4) The Node becomes a child node of the node named in "relative"

    **************************************************************************************

    oTree = THISFORM.treeview1

    cParent = oxmldoc.DocumentElement.BaseName

    oTree.Nodes.CLEAR

    oTree.Nodes.ADD(,, cParent, cParent)

    iNodeKey = 10000

    iiNodeKey = 20000

    ** The following method for creating the tree nodes is hard-coded to create only 2 levels.

    WITH oxmldoc.DocumentElement.ChildNodes

    FOR icount = 0 TO .LENGTH - 1

    iNodeKey = iNodeKey + 1

    cNodeKey = "c"+ALLTRIM(STR(iNodeKey))

    THISFORM.pAddNode(cParent, 4, cNodeKey, .ITEM(icount).nodename) && Adds a tree node

    THISFORM.treeview1.Nodes(cNodeKey).TAG = .ITEM(icount).TEXT

    IF .ITEM(icount).HasChildNodes

    FOR iicount = 0 TO .ITEM(icount).ChildNodes.LENGTH - 1

    iiNodeKey = iiNodeKey + 1

    ccNodeKey = "cc"+ALLTRIM(STR(iiNodeKey))

    THISFORM.pAddNode(cNodeKey, 4, ccNodeKey, .ITEM(icount).ChildNodes(iicount).nodename)

    THISFORM.treeview1.Nodes(ccNodeKey).TAG = .ITEM(icount).ChildNodes(iicount).TEXT

    ENDFOR

    ENDIF

    ENDFOR

    ENDWITH

    ENDIF

    ELSE

    *MESSAGEBOX(oxmldoc.ParseError.reason) && This will display the error text from the MSXML object

    MESSAGEBOX("The document that you attempted to load may be an invalid or not-well-formed XML document.")

    THISFORM.txtxmldoc.VALUE = ""

    ENDIF

    THISFORM.treeview1.Nodes(1).expanded = .T. && This opens the tree to see the first level

    ON ERROR

     

    this is my xml file

    ---------------

    <?xml version="1.0" encoding="ISO_8859-1" standalone="yes"?>
    <encomenda>
     <cabecalho>
      <numero>106676624</numero>
      <tipo>NB</tipo>
      <datas>
       <encom>2008-04-02T00:00:00</encom>
       <entrega>2008-04-04T08:30:00</entrega>
      </datas>
      <parceiros>
       <tipo>SU</tipo>
       <ean>5550000022223</ean>
      </parceiros>
      <parceiros>
       <tipo>BY</tipo>
       <ean>5550000022220</ean>
      </parceiros>
      <parceiros>
       <tipo>IV</tipo>
       <ean>5550000022221</ean>
      </parceiros>
      <parceiros>
       <tipo>DP</tipo>
       <ean>5550000022222</ean>
      </parceiros>
      <moeda>EUR</moeda>
     </cabecalho>
     <linha>
      <numero>1</numero>
      <artigo>
       <ean>11111111111</ean>
       <codigo-interno>2220963</codigo-interno>
       <descricao>ALLUMETTES "CAPUCCINO" 125GR</descricao>
      </artigo>
      <quantidade>
       <encomendada>
        <qty>280.000</qty>
       </encomendada>
      </quantidade>
      <num-art-caixa>20</num-art-caixa>
      <imposto>
       <iva>
        <tx>21.00</tx>
       </iva>
      </imposto>
     </linha>
     <linha>
      <numero>2</numero>
      <artigo>
       <ean>5601489129241</ean>
       <codigo-interno>2736409</codigo-interno>
       <descricao>KIDS 100GR</descricao>
      </artigo>
      <quantidade>
       <encomendada>
        <qty>720.000</qty>
       </encomendada>
      </quantidade>
      <num-art-caixa>36</num-art-caixa>
      <imposto>
       <iva>
        <tx>21.00</tx>
       </iva>
      </imposto>
     </linha>
     <linha>
      <numero>3</numero>
      <artigo>
       <ean>5601489135150</ean>
       <codigo-interno>2811330</codigo-interno>
       <descricao>BOL.CRACKERS C/ SAL</descricao>
      </artigo>
      <quantidade>
       <encomendada>
        <qty>1260.000</qty>
       </encomendada>
      </quantidade>
      <num-art-caixa>7</num-art-caixa>
      <imposto>
       <iva>
        <tx>5.00</tx>
       </iva>
      </imposto>
     </linha>
     <linha>
      <numero>4</numero>
      <artigo>
       <ean>5601489136157</ean>
       <codigo-interno>2811336</codigo-interno>
       <descricao>BOL.CRACKER 50GR</descricao>
      </artigo>
      <quantidade>
       <encomendada>
        <qty>1260.000</qty>
       </encomendada>
      </quantidade>
      <num-art-caixa>7</num-art-caixa>
      <imposto>
       <iva>
        <tx>5.00</tx>
       </iva>
      </imposto>
     </linha>
     <linha>
      <numero>5</numero>
      <artigo>
       <ean>5601489134153</ean>
       <codigo-interno>2811344</codigo-interno>
       <descricao>BOL.CRACKER S/ SAL</descricao>
      </artigo>
      <quantidade>
       <encomendada>
        <qty>1260.000</qty>
       </encomendada>
      </quantidade>
      <num-art-caixa>7</num-art-caixa>
      <imposto>
       <iva>
        <tx>5.00</tx>
       </iva>
      </imposto>
     </linha>
    </encomenda>

    but i only can reach de second level of tag's, ex: in tag 'parceiros' e get the value 'SU 5550000022223' and so on. it i sould get the tag 'tipo' value 'SU', tag 'ean' value '5550000022223'

     

     

    Tuesday, April 8, 2008 10:49 AM
  •  Luis Moura wrote:

    I mean read it from a xml file and pass data into cursor, i have found an exemple but for my case i can't get it to read all the tag's:

     

    ************************************************************************************

    ** The following will create the Microsoft XML parser and will load

    ** the XML document specified in the textbox

    ************************************************************************************

    PUBLIC oxmldoc

    oxmldoc = CREATEOBJECT('msxml2.domdocument')

    oxmldoc.ASYNC = .F. && False

    IF EMPTY(THISFORM.txtxmldoc.VALUE)

      THISFORM.txtxmldoc.VALUE = GETFILE('xml')

    ENDIF

     

    but i only can reach de second level of tag's, ex: in tag 'parceiros' e get the value 'SU 5550000022223' and so on. it i sould get the tag 'tipo' value 'SU', tag 'ean' value '5550000022223'

     

    If you have a copy of MegaFox: 1002 Things You Wanted to Know About Extending VFP, there is a data-driven class in Chapter 17 that does something similar to what you want. You may also be able to do this more easily using an xmlAdapter, but I have not used it very much myself. You do not say which version of VFP you are using - the xmlAdapter is available in versions 8 & 9.

    Thursday, April 10, 2008 9:28 PM