sábado, 16 de janeiro de 2016

Wx - Leitura de arquivos XML com um Registro e Multiplos registros








http://forum.pcsoft.fr/pt-BR/pcsoft.br.windev/864-leitura-arquivos-xml-com-registro-multiplos-registros-extracao/read.awp

 


http://doc.windev.com/?3075002&lang=en-US&productversion=06A200067s
http://doc.windev.com/?3081015&name=xmlexecutexpath_function
http://doc.windev.com/?3081026&lang=en-US&productversion=06A200067s
http://help.windev.com/en-US/?3081039&name=xmlnamespace_function
http://help.windev.com/en-US/?3081026&name=xmlelementname_function
http://help.windev.com/en-US/?3081030&name=XMLNext
http://help.windev.com/en-US/?1000019167&name=XMLFindNamespaceByName
http://help.windev.com/en-US/search2.awp?q=XML%20&origin=searchbox&start=2
http://forum.pcsoft.fr/fr-FR/pcsoft.br.windev/856-procedure-rotasgooglemaps-retorna-dois-enderecos-tempo-estimado-distancia/read.awp
http://forum.pcsoft.fr/fr-FR/pcsoft.us.windevmobile/1064-wsandroidiosreadxml-read-xml-android-ios/read.awp?lastview


Fontes de Dados Fornecido por Adriano boller


http://www.screencast.com/t/xUzlgYjNDNby



http://www.screencast.com/t/sPyLGnKxfa



http://www.screencast.com/t/6C4OCWcW



http://www.screencast.com/t/V0APuTWU




//Global do Projeto
arrXml_Tags is array of strings //Nome das Tags

arrXml_TagResult is array of strings //Conteudo das Tags Resultado


Procedure LocalizarTagsEmXml(XML_Retorno is string)

//Variaveis
nXposicaoinicial is int = 0
nXposicaofinal is int = 0
arrXml_Tag is array of strings

X is int = 0

//Exemplo
//<Xml>
//<Retorno>
//<NoXml ID="3"> // <------------------------ motivo do erro
//<login_id>3</login_id>
//<login_email>teste@teste.com</login_email>
//<login_nome>Usuario teste</login_nome>
//<login_senha>123</login_senha>
//<login_status>1</login_status>
//<login_admin>0</login_admin>
//</NoXml> // <------------------------ motivo do erro
//<Total>1</Total>
//</Retorno>
//</Xml>


//iOS - Apple (iPhone / iPad / iPod)
IF IniOSMode() = True THEN

NoXml is string
i is int = 1
NoXml = XMLExtractString(XML_Retorno,"NoXml",i)

Add(arrXml_Tag,"login_id")
Add(arrXml_Tag,"login_email")
Add(arrXml_Tag,"login_nome")
Add(arrXml_Tag,"login_senha")
Add(arrXml_Tag,"login_status")
Add(arrXml_Tag,"login_admin")

FOR EACH stag OF arrXml_Tag

temp is string = XMLExtractString(NoXml,stag)
Add(arrXml_TagResult, temp )

END

//Android
ELSE IF InAndroidMode() = True

XmlDoc is XMLDocument
XmlDoc = XMLOpen(XML_Retorno, fromString)
NoXml is xmlNode

FOR EACH NoXml OF XmlDoc.Xml.Retorno on NoXml

Add(arrXml_TagResult, NoXml.login_id)
Add(arrXml_TagResult, NoXml.login_email)
Add(arrXml_TagResult, NoXml.login_nome)
Add(arrXml_TagResult, NoXml.login_senha)
Add(arrXml_TagResult, NoXml.login_status)
Add(arrXml_TagResult, NoXml.login_admin)

END

END

RESULT arrXml_TagResult


//Exemplo 2
Procedure LocalizarTagsEmXml(XML_Retorno is string)

//Variaveis
nXposicaoinicial is int = 0
nXposicaofinal is int = 0
arrXml_Tag is array of strings

X is int = 0

//Exemplo
//<Xml>
//<Retorno>
//<NoXml ID="3"> // <------------------------ motivo do erro deve ser um nome para todas as consultas nao mude NoXml
//<login_id>3</login_id>
//<login_email>teste@teste.com</login_email>
//<login_nome>Usuario teste</login_nome>
//<login_senha>123</login_senha>
//<login_status>1</login_status>
//<login_admin>0</login_admin>
//</NoXml> // <------------------------ motivo do erro deve ser um nome para todas as consultas nao mude NoXml
//<Total>1</Total>
//</Retorno>
//</Xml>


//iOS - Apple (iPhone / iPad / iPod)
IF IniOSMode() = True THEN

NoXml is string
i is int = 1
NoXml = XMLExtractString(XML_Retorno,"NoXml",i)

Add(arrXml_Tag,"login_id")
Add(arrXml_Tag,"login_email")
Add(arrXml_Tag,"login_nome")
Add(arrXml_Tag,"login_senha")
Add(arrXml_Tag,"login_status")
Add(arrXml_Tag,"login_admin")

XmlDoc is XMLDocument

XmlDoc = XMLOpen(XML_Retorno, fromString)

total is int = XMLExtractString(XML_Retorno,"Total") //5

LOOP (total)

FOR EACH stag OF arrXml_Tag on stag

temp is string = XMLExtractString(NoXml,stag)
Add(arrXml_TagResult, temp )

END

END

//Android
ELSE IF InAndroidMode() = True

XmlDoc is XMLDocument
XmlDoc = XMLOpen(XML_Retorno, fromString)
NoXml is xmlNode

FOR EACH NoXml OF XmlDoc.Xml.Retorno on NoXml

Add(arrXml_TagResult, NoXml.login_id)
Add(arrXml_TagResult, NoXml.login_email)
Add(arrXml_TagResult, NoXml.login_nome)
Add(arrXml_TagResult, NoXml.login_senha)
Add(arrXml_TagResult, NoXml.login_status)
Add(arrXml_TagResult, NoXml.login_admin)

END

END

RESULT arrXml_TagResult



//Exemplo 3

Procedure ArrayDeleteDuplicateBoller(ArrayResultado is array of strings)

x, NaoAchou is int = 0

ArrayResultadoNovo is array of strings

nTotal is int = ArrayCount(ArrayResultado)

LOOP (nTotal)

x += 1

NaoAchou = ArraySeek(ArrayResultadoNovo,asLinearFirst,ArrayResultado[x])

IF NaoAchou = -1

Add(ArrayResultadoNovo,ArrayResultado[x])

END

END

RESULT (ArrayResultadoNovo)



Procedure IdentificarTagsNoXml(XmlConteudo)

x, Inicial, Final, Tamanho is int = 0

Tag, Anterior is string = ""

IF XmlConteudo <> "" THEN

XmlConteudo = NoAccent(XmlConteudo)
XmlConteudo = Replace(XmlConteudo,CR,"")
XmlConteudo = Replace(XmlConteudo,Charact(13),"")
XmlConteudo = Replace(XmlConteudo,Charact(10),"")

Total is int = Length(XmlConteudo)

LOOP (Total)

x += 1

IF XmlConteudo[[x]] = "<"
Inicial = x+1
ELSE IF XmlConteudo[[x]] = ">"
Final = x
IF Final <= Inicial THEN
Final = 0
END
END

IF Inicial > 0 AND Final > 0 AND Final > Inicial

Tamanho = Final - Inicial

Tag = Middle(XmlConteudo,Inicial,Tamanho)

//IGNORAR AS TAGS DE FECHAMENTO E ID
IF Middle(Tag,1,1) = "/" OR PositionOccurrence(Upper(Tag),"ID=",firstRank,FromBeginning) > 0
Tag = ""
END

//VALIDA TAGS DO NÓ
IF Tag <> "" AND Tamanho > 0 AND Anterior <> Tag AND Upper(Tag) <> "XML" AND Upper(Tag) <> "RETORNO" AND Upper(Tag) <> "TOTAL"

Anterior = Tag

//DEBUG
//add(arrXml_Tags,Tag +" / "+ Inicial +" / "+ Final +" / "+ Tamanho )
//INFO(TAG)

Add(arrXml_Tags,Tag) //VAR GLOBAL: arrXml_Tags

END

END

END

END

//TARGET CODE
ArrayDeleteDuplicate(arrXml_Tags,tccIgnoreSpace)


//TARGET CODE


Procedure LocalizarTagsEmXml(XML_Retorno is string)

//Variaveis
nXposicaoinicial is int = 0
nXposicaofinal is int = 0

X is int = 0

//Exemplo
//<Xml>
//<Retorno>
//<NoXml ID="3"> // <------------------------ motivo do erro
//<login_id>3</login_id>
//<login_email>teste@teste.com</login_email>
//<login_nome>Usuario teste</login_nome>
//<login_senha>123</login_senha>
//<login_status>1</login_status>
//<login_admin>0</login_admin>
//</NoXml> // <------------------------ motivo do erro
//<Total>1</Total>
//</Retorno>
//</Xml>


//iOS - Apple (iPhone / iPad / iPod)
IF IniOSMode() = True THEN

NoXml is string
i is int = 1
NoXml = XMLExtractString(XML_Retorno,"NoXml",i)

//FUNCAO IDENTIFICA TAG
IdentificarTagsNoXml(XML_Retorno)

FOR EACH stag OF arrXml_Tags //VAR GLOBAL

temp is string = XMLExtractString(NoXml,stag)

Add(arrXml_TagResult, temp )

END

//Android
ELSE IF InAndroidMode() = True

T is int = 0

IdentificarTagsNoXml(XML_Retorno)//FUNCAO IDENTIFICA TAG

nQtde_tags is int = ArrayCount(arrXml_Tags)
TagRegistro is string
TagValor is string

LOOP (nQtde_tags)

T += 1

TagRegistro = arrXml_Tags[T]

TagValor = XMLExtractString(XML_Retorno,TagRegistro)

IF TagValor <> ""

Add(arrXml_TagResult,TagValor)

END

END


END

RESULT arrXml_TagResult



//Exemplo 4 do video

Procedure ArrayDeleteDuplicateBoller(ArrayResultado is array of strings)

x, NaoAchou is int = 0

ArrayResultadoNovo is array of strings

nTotal is int = ArrayCount(ArrayResultado)

LOOP (nTotal)

x += 1

NaoAchou = ArraySeek(ArrayResultadoNovo,asLinearFirst,ArrayResultado[x])

IF NaoAchou = -1

Add(ArrayResultadoNovo,ArrayResultado[x])

END

END

RESULT (ArrayResultadoNovo)



Procedure IdentificarTagsNoXml(XmlConteudo)

x, Inicial, Final, Tamanho is int = 0

Tag, Anterior is string = ""

IF XmlConteudo <> "" THEN

XmlConteudo = NoAccent(XmlConteudo)
XmlConteudo = Replace(XmlConteudo,CR,"")
XmlConteudo = Replace(XmlConteudo,Charact(13),"")
XmlConteudo = Replace(XmlConteudo,Charact(10),"")

Total is int = Length(XmlConteudo)

LOOP (Total)

x += 1

IF XmlConteudo[[x]] = "<"
Inicial = x+1
ELSE IF XmlConteudo[[x]] = ">"
Final = x
IF Final <= Inicial THEN
Final = 0
END
END

IF Inicial > 0 AND Final > 0 AND Final > Inicial

Tamanho = Final - Inicial

Tag = Middle(XmlConteudo,Inicial,Tamanho)

//IGNORAR AS TAGS DE FECHAMENTO E ID
IF Middle(Tag,1,1) = "/" OR PositionOccurrence(Upper(Tag),"ID=",firstRank,FromBeginning) > 0
Tag = ""
END

//VALIDA TAGS DO NÓ
IF Tag <> "" AND Tamanho > 0 AND Anterior <> Tag AND Upper(Tag) <> "XML" AND Upper(Tag) <> "RETORNO" AND Upper(Tag) <> "TOTAL"

Anterior = Tag

//DEBUG
//add(arrXml_Tags,Tag +" / "+ Inicial +" / "+ Final +" / "+ Tamanho )
//INFO(TAG)

Add(arrXml_Tags,Tag) //VAR GLOBAL: arrXml_Tags

END

END

END

END

//TARGET CODE
ArrayDeleteDuplicate(arrXml_Tags,tccIgnoreSpace)


//TARGET CODE


Procedure LocalizarTagsEmXml(XML_Retorno is string)

//Variaveis
nXposicaoinicial is int = 0
nXposicaofinal is int = 0

X is int = 0

//Exemplo
//<Xml>
//<Retorno>
//<NoXml ID="3"> // <------------------------ motivo do erro
//<login_id>3</login_id>
//<login_email>teste@teste.com</login_email>
//<login_nome>Usuario teste</login_nome>
//<login_senha>123</login_senha>
//<login_status>1</login_status>
//<login_admin>0</login_admin>
//</NoXml> // <------------------------ motivo do erro
//<Total>1</Total>
//</Retorno>
//</Xml>


//iOS - Apple (iPhone / iPad / iPod)
IF IniOSMode() = True THEN

T, N is int = 0

RegistroXml is string

NoXml is string

i is int = 1

NoXml = XMLExtractString(XML_Retorno,"NoXml",i)

//FUNCAO IDENTIFICA TAG
IdentificarTagsNoXml(XML_Retorno)

nQtde_Registros is int = XMLExtractString(EDT_XML,"Total") //5

LOOP (nQtde_Registros)

T = 0

N += 1

RegistroXml = PegaNoXml(XML_Retorno,N)

NoXml = XMLExtractString(RegistroXml,"NoXml",i)

FOR EACH stag OF arrXml_Tags //VAR GLOBAL

temp is string = XMLExtractString(NoXml,stag)

Add(arrXml_TagResult, temp )

END

END



//Android
ELSE IF InAndroidMode() = True

T, N is int = 0

IdentificarTagsNoXml(XML_Retorno)//FUNCAO IDENTIFICA TAG

nQtde_tags is int = ArrayCount(arrXml_Tags)

TagRegistro, RegistroXml is string

TagValor is string

nQtde_Registros is int = XMLExtractString(EDT_XML,"Total") //5

LOOP (nQtde_Registros)

T = 0

N += 1

RegistroXml = PegaNoXml(XML_Retorno,N)

LOOP (nQtde_tags)

T += 1

TagRegistro = arrXml_Tags[T]

TagValor = XMLExtractString(RegistroXml,TagRegistro)

IF TagValor <> ""

Add(arrXml_TagResult,TagValor)

END

END

END

END

RESULT arrXml_TagResult


Procedure PegaNoXml(RetornoXml, NoXmlId) //outra procedure para facilitar, que nem dizia Jac, vamos por partes...

Inicio, Final, Tamanho is int = 0

NoXml is string = "<Xml><Retorno>"

IF RetornoXml <> "" AND NoXmlId > 0 THEN

Inicio = PositionOccurrence(RetornoXml,"<NoXml ID="+Charact(34)+NoXmlId+Charact(34)+">",firstRank,FromBeginning)

IF NoXmlId = 1
Final = PositionOccurrence(RetornoXml,"</NoXml>",firstRank,FromBeginning)
ELSE
Final = PositionOccurrence(RetornoXml,"</NoXml>",nextRank,FromBeginning)
END

Tamanho = Final - Inicio

NoXml += Middle(RetornoXml,Inicio,Tamanho)

END

NoXml += "</NoXml></Retorno></Xml>"

NoXml = Replace(NoXml,CR,"")
NoXml = Replace(NoXml,Charact(13),"")
NoXml = Replace(NoXml,Charact(10),"")

RESULT NoXml













Nenhum comentário:

Postar um comentário

Teste

Teste
teste