Jump to content
DJHasselmann

Gerar XML com a CHILKATXML no VB6 - Exemplos

Recommended Posts

DJHasselmann

Olá pessoal do fórum, estou chegando agora, boa tarde!

É o seguinte: Baixei a ChilKatXML.DLL, já esta instalada no VB6 SP6 a qual já fiz a devida referencia, só que não sei quais parametros seguir ou seja se é igual a base da MS XML 4 que é o DOMDocument ou não, por isso peço a quem puder enviar-me um codigo de exemplo fico grato, abaixo segue um fragmento do meu código atual utilizando a já mencionada ferramenta.

Public Sub Gera_XML()
  On Error GoTo errGera_XML

  Dim Nome As String, arquivo As Variant, XMLdoc As DOMDocument40
  Dim Cabec, Raiz, InfVer, oNode, iNode, Child, SubChild, SubChild1, SubChild2
  Dim x As Integer, namednodemap

  gMensagem = "Criando objeto XML"
  Set XMLdoc = CreateObject("MSXML2.DOMdocument.4.0")
  XMLdoc.async = False
  XMLdoc.validateOnParse = True
  XMLdoc.resolveExternals = False
  XMLdoc.preserveWhiteSpace = True

  gMensagem = "Criando Arquivo XML"
  Nome = XMLPath & c_NewArq
  arquivo = XMLdoc.Load(Nome)

  If arquivo = True Then
     Set XMLdoc = Nothing
     Exit Sub
  End If

  gMensagem = "Criando dados do XML (TAG's e CAMPOS"
  Set Raiz = XMLdoc.createElement("CTe")
  XMLdoc.appendChild (Raiz)
  Raiz.setAttribute "xmlns", "http://www.portalfiscal.inf.br/cte"
  Raiz.setAttribute = Nothing

  gMensagem = "Criando o cabecalho"
  Set Cabec = XMLdoc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
  XMLdoc.insertBefore Cabec, Raiz

  gMensagem = "Criando o 2º Nó"
  Set InfVer = XMLdoc.createElement("infCte")
  Raiz.appendChild (InfVer)
  InfVer.setAttribute "versao", "1.03"
  InfVer.setAttribute "id", c_Chave1

  '************************************** Nó Ide
  Set oNode = XMLdoc.createElement("ide")
  InfVer.appendChild oNode   'Acrescenta no XML

  Set iNode = XMLdoc.createElement("cUF")
  iNode.text = c_cUF
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("cCT")
  iNode.text = c_cCT
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("CFOP")
  iNode.text = c_CFOP
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("natOp")
  iNode.text = c_natOp
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("forPag")
  iNode.text = c_forPag
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("mod")
  iNode.text = c_Mod
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("serie")
  iNode.text = c_Serie
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("nCT")
  iNode.text = c_nCT
  oNode.appendChild iNode

  Set iNode = XMLdoc.createElement("dhEmi")
  iNode.text = c_dhEmi
  oNode.appendChild iNode

E assim por diante.

Obrigado!

Dilson

Share this post


Link to post
Share on other sites
YoungCoder

Aqui podes ver alguns exemplos da chilkat http://www.example-code.com/vb/xml.asp

por exemplo:

Dim xml1 As New ChilkatXml
xml1.Tag = "accrep:AccidentReport"
Dim xml2 As ChilkatXml
Set xml2 = xml1.NewChild("accrep:VehicleSection","")
Dim xml3 As ChilkatXml
Set xml3 = xml2.NewChild("accrep:Vehicle","")
xml3.NewChild2 "accrep:DriverOrPedestrian","D"
xml3.NewChild2 "accrep:Number","1"
Dim xml6 As ChilkatXml
Set xml6 = xml3.NewChild("accrep:Name","")
xml6.NewChild2 "accrep:First","John"
xml6.NewChild2 "accrep:Last","Doe"
xml6.NewChild2 "accrep:Middle","Robert"

Dim xml10 As ChilkatXml
Set xml10 = xml3.NewChild("accrep:Passengers","")
Dim xml11 As ChilkatXml
Set xml11 = xml10.NewChild("accrep:Name","")
xml11.NewChild2 "accrep:First","John"
xml11.NewChild2 "accrep:Last","Doe, Jr."
xml11.NewChild2 "accrep:Middle","Robert"

Dim xml15 As ChilkatXml
Set xml15 = xml3.NewChild("accrep:Passengers","")
Dim xml16 As ChilkatXml
Set xml16 = xml15.NewChild("accrep:Name","")
xml16.NewChild2 "accrep:First","Sally"
xml16.NewChild2 "accrep:Last","Doe"
xml16.NewChild2 "accrep:Middle","Crystal"

Dim xml20 As ChilkatXml
Set xml20 = xml2.NewChild("accrep:Vehicle","")
xml20.NewChild2 "accrep:DriverOrPedestrian","D"
xml20.NewChild2 "accrep:Number","2"
Dim xml23 As ChilkatXml
Set xml23 = xml20.NewChild("accrep:Name","")
xml23.NewChild2 "accrep:First","John"
xml23.NewChild2 "accrep:Last","Public"
xml23.NewChild2 "accrep:Middle","Q"

Dim xml27 As ChilkatXml
Set xml27 = xml20.NewChild("accrep:Passengers","")
Dim xml28 As ChilkatXml
Set xml28 = xml27.NewChild("accrep:Name","")
xml28.NewChild2 "accrep:First","Billy"
xml28.NewChild2 "accrep:Last","Public"
xml28.NewChild2 "accrep:Middle","Bob"

Dim xml32 As ChilkatXml
Set xml32 = xml20.NewChild("accrep:Passengers","")
Dim xml33 As ChilkatXml
Set xml33 = xml32.NewChild("accrep:Name","")
xml33.NewChild2 "accrep:First","Kelly"
xml33.NewChild2 "accrep:Last","Public"
xml33.NewChild2 "accrep:Middle","Emily"

'  Display the XML:
Text1.Text = Text1.Text & xml1.GetXml() & vbCrLf

vai gerar o seguinte documento xml

<accrep:AccidentReport>
  <accrep:VehicleSection>
    <accrep:Vehicle>
      <accrep:DriverOrPedestrian>D</accrep:DriverOrPedestrian>
      <accrep:Number>1</accrep:Number>
      <accrep:Name>
        <accrep:First>John</accrep:First>
        <accrep:Last>Doe</accrep:Last>
        <accrep:Middle>Robert</accrep:Middle>
      </accrep:Name>
      <accrep:Passengers>
        <accrep:Name>
          <accrep:First>John</accrep:First>
          <accrep:Last>Doe, Jr.</accrep:Last>
          <accrep:Middle>Robert</accrep:Middle>
        </accrep:Name>
      </accrep:Passengers>
      <accrep:Passengers>
  <accrep:Name>
          <accrep:First>Sally</accrep:First>
          <accrep:Last>Doe</accrep:Last>
          <accrep:Middle>Crystal</accrep:Middle>
        </accrep:Name>
      </accrep:Passengers>
    </accrep:Vehicle>
    <accrep:Vehicle>
      <accrep:DriverOrPedestrian>D</accrep:DriverOrPedestrian>
      <accrep:Number>2</accrep:Number>
      <accrep:Name>
        <accrep:First>John</accrep:First>
        <accrep:Last>Public</accrep:Last>
        <accrep:Middle>Q</accrep:Middle>
      </accrep:Name>
      <accrep:Passengers>
        <accrep:Name>
          <accrep:First>Billy</accrep:First>
          <accrep:Last>Public</accrep:Last>
          <accrep:Middle>Bob</accrep:Middle>
        </accrep:Name>
      </accrep:Passengers>
      <accrep:Passengers>
  <accrep:Name>
          <accrep:First>Kelly</accrep:First>
          <accrep:Last>Public</accrep:Last>
          <accrep:Middle>Emily</accrep:Middle>
        </accrep:Name>
      </accrep:Passengers>
    </accrep:Vehicle>
  </accrep:VehicleSection>
</accrep:AccidentReport>


IIF(strQuestion = "Stupid",doSEARCH!,doHELP!)

Share this post


Link to post
Share on other sites
DJHasselmann

Valeu YoungCoder, muito obrigado pela força, qq coisa estou aqui, ok?

Att.

Dilson

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.