PDA

View Full Version : Ler XML



watanabe
24-Feb-2021, 03:03 PM
Mestres boa tarde tudo bem ?
Estou tentando ler um xml...ainda estou apanhando rs.... Para criar foi fácil kkkk...estou há alguns meses programando em Dataflex mta coisa consigo resolver com a documentação...mas esse aqui ainda não consegui solucionar...


Queria ler os elementos codturma,dia,hor,codprof,coddisc e pegar seus valores


// já vi a documentação
https://docs.dataaccess.com/dataflexhelp/mergedProjects/VDFClassRef/cXMLDOMDocument.htm#XMLINDF


Tentei usando o código abaixo...mas creio que somente exibe os nós....



// código extraído da documentação e usando em uma view que criei...
Move 0 to iLineCount
Get FirstChild of hoRoot to hoNode // get the first node
While (hoNode<>0)
Increment iLineCount
Send Info_Box hoNode iLineCount
Get NextNode of hoNode to hoNode // now get next sibling node, destroy current node
Loop


Eis bendito xml



<IMPORT>
<CODESCOLA>001</CODESCOLA>
<CODTURNO>001</CODTURNO>
<NOMETURNO>Matutino</NOMETURNO>
<HORARIO>
<REGISTRO>
<CODTURMA>201639A</CODTURMA>
<DIA>SEG</DIA>
<HOR>01</HOR>
<CODPROF>13</CODPROF>
<CODDISC>19</CODDISC>
</REGISTRO>
<REGISTRO>
<CODTURMA>201639A</CODTURMA>
<DIA>SEG</DIA>
<HOR>02</HOR>
<CODPROF>13</CODPROF>
<CODDISC>19</CODDISC>
</REGISTRO>
</IMPORT>

Mike Peat
25-Feb-2021, 05:22 AM
Edwardo - apologies for answering in English

First, the XML you give has an error: the <TIME> element is never closed. If you fix that by changing it to <TIME /> it is OK.

Then this code in a "Basic Project" should work (I hope) if your XML file is in the workspace root called "Test.xml":


Use Windows.pkg
Use cApplication.pkg
Use Flexml.pkg

Object oApplication is a cApplication
End_Object

Procedure ReadXML String sXMLDoc
Handle hoXml hoRoot hoElem hoItem
Boolean bOK
Integer iElem
String sNodeName

Get Create (RefClass(cXMLDOMDocument)) to hoXml
Set psDocumentName of hoXml to sXMLDoc
Get LoadXMLDocument of hoXml to bOK

If not bOK Begin
Send Stop_Box ("Could not read XML from" * sXMLDoc) "XML Error"
Procedure_Return
End

Get DocumentElement of hoXml to hoRoot

If not hoRoot Begin
Send Stop_Box "Could not create document root" "XML Error"
Procedure_Return
End

Move 0 to iElem
Get FirstChild of hoRoot to hoElem

While hoElem

If (psNodeName(hoElem) = "REGISTRO") Begin
Increment iElem
Showln
Showln "REGISTRO " (String(iElem))
Get FirstChild of hoElem to hoItem

While hoItem
Get psNodeName of hoItem to sNodeName

Case Begin

Case (sNodeName = "CODTURMA")
Showln "CODTURMA: " (psText(hoItem))
Case Break

Case (sNodeName = "DIA")
Showln "DIA: " (psText(hoItem))
Case Break

Case (sNodeName = "HOR")
Showln "HOR: " (psText(hoItem))
Case Break

Case (sNodeName = "CODPROF")
Showln "CODPROF: " (psText(hoItem))
Case Break

Case (sNodeName = "CODDISC")
Showln "CODDISC: " (psText(hoItem))
Case Break

Case End

Get NextSibling of hoItem to hoItem
Loop

End

Get NextSibling of hoElem to hoElem
Loop

End_Procedure

Send ReadXML (psHome(phoWorkspace(ghoApplication)) + "Test.xml")
Inkey WindowIndex


Producing the output:



REGISTRATION 1
CODTURMA: 201639A
DAY:MON
HOR: 01
CODPROF: 13
CODDISC: 19

REGISTRATION 2
CODTURMA: 201639A
DAY: MON
HOR: 02
CODPROF: 13
CODDISC: 19


Mike

watanabe
25-Feb-2021, 08:33 AM
Mike thanks a lot again for clarifying my doubts .... it worked here ....

Mike Peat
25-Feb-2021, 09:01 AM
:)