Visual Basic - Generar XML correctamente

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 21/04/2015 15:15:58
Hola alguna instruccion para agregar un nodo como la imgen que adjunto. Es un programa en vb en el cual tengo toda la estructura del xml pero no he logrado de crear el xml como el ejemplo. Me pueden ayudar.?.

Gracias y Saludos.

EJEMPLO
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 147
Ha disminuido 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Generar XML correctamente

Publicado por Juan Gilberto (323 intervenciones) el 21/04/2015 17:56:34
Para poder ayudarte necesitas explicar como le estas haciendo para generar el xml

1.- Estas utilizando un XSD? Probablemente esta erroneo o es una version vieja...

2.- Lo estas generando linea por linea?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 21/04/2015 20:24:28
Los estoy haciendo linea. Agregué. el codigo. Gracias por contestar.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Andres Leonardo
Val: 3.117
Oro
Ha mantenido su posición en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Generar XML correctamente

Publicado por Andres Leonardo (1798 intervenciones) el 21/04/2015 18:17:59
Podrias compartir al menos el codigo como lo haces para poderte ayudar mejor...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 21/04/2015 20:28:41
Les dejo el código. Está hecho a partir de un MSHFlexgrid de donde tomo los datos y de ahí creo el XML.Gracias por contestar. Saludos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Private Sub CmdGenerarXML_Click()
 
    Set objDom = New DOMDocument
    Set objModule = New DOMDocument
 
    Set XMLInstruccion = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
    objDom.appendChild XMLInstruccion
 
         Set objRootElem = objDom.createElement("BCE:Balanza")
 
         objRootElem.setAttribute "xmlns:BCE", "www.sat.gob.mx/esquemas/ContabilidadE/1_1/BalanzaComprobacion"
         objRootElem.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
         objRootElem.setAttribute "xsi:schemaLocation", "www.sat.gob.mx/esquemas/ContabilidadE/1_1/BalanzaComprobacion http://www.sat.gob.mx/esquemas/ContabilidadE/1_1/BalanzaComprobacion/BalanzaComprobacion_1_1.xsd"
         objDom.appendChild objRootElem
 
         Call XMLHeader
 
         Fila = 1
 
         With MSHFlexGrid1
              Do While .TextMatrix(Fila, 0) <> vbNullString
 
                'create new element and attribute by calling to CreateXMLBalanza function.
                objModule.loadXML (CreateXMLBalanza(.TextMatrix(Fila, 0), .TextMatrix(Fila, 1), .TextMatrix(Fila, 2), .TextMatrix(Fila, 3), .TextMatrix(Fila, 4)))
 
                Set objNode = objModule.firstChild
                objDom.documentElement.appendChild objNode
 
                Fila = Fila + 1
 
              Loop
 
              'Saves the xml document in c:\temp directory
                 NombreBalanza = "XXAX010101X01" + Combo2.Text + Combo1.Text + "B" + Combo3.Text + ".XML"
              objDom.Save (NombreBalanza)
              MsgBox " SE HA GENERADO EXISTODAMENTE EL ARCHIVO EN: " & NombreBalanza, vbInformation, "GENERAR BALANZA XML"
 
End Sub
 
Public Function XMLHeader()
 
         Set objDocAttribute = objDom.createAttribute("Version")
         objDocAttribute.nodeValue = "1.1"
         objRootElem.setAttributeNode objDocAttribute
 
         Set objDocAttribute = objDom.createAttribute("RFC")
         objDocAttribute.nodeValue = "XXAX010101X01"
         objRootElem.setAttributeNode objDocAttribute
 
         Set objDocAttribute = objDom.createAttribute("Mes")
         objDocAttribute.nodeValue = FrmBalanzaXML.Combo1.Text
         objRootElem.setAttributeNode objDocAttribute
 
         Set objDocAttribute = objDom.createAttribute("Anio")
         objDocAttribute.nodeValue = FrmBalanzaXML.Combo2.Text
         objRootElem.setAttributeNode objDocAttribute
 
         If OptionXML = 1 Then
            Set objDocAttribute = objDom.createAttribute("TipoEnvio")
            objDocAttribute.nodeValue = FrmBalanzaXML.Combo3.Text
            objRootElem.setAttributeNode objDocAttribute
 
            Set objDocAttribute = objDom.createAttribute("FechaModBal")
            objDocAttribute.nodeValue = "2015-04-30" 'FrmBalanzaXML.Combo3.Text
            objRootElem.setAttributeNode objDocAttribute
 
         End If
 
End Function
 
' ----------------------------------------------------------------------------------
Public Function CreateXMLBalanza(strNumCta As String, strSaldoIni As String, _
    strDebe As String, strHaber As String, strSaldoFin As String) As String
 
   Set objDomBal = New DOMDocument
 
   Set objRootElem = objDomBal.createElement("Ctas")
   objDomBal.appendChild objRootElem
 
   Set objMbrAttribute = objDomBal.createAttribute("NumCta")
   objMbrAttribute.nodeValue = strNumCta
   objRootElem.setAttributeNode objMbrAttribute
 
   Set objMbrAttribute = objDomBal.createAttribute("SaldoIni")
   objMbrAttribute.nodeValue = strSaldoIni
   objRootElem.setAttributeNode objMbrAttribute
 
   Set objMbrAttribute = objDomBal.createAttribute("Debe")
   objMbrAttribute.nodeValue = strDebe
   objRootElem.setAttributeNode objMbrAttribute
 
   Set objMbrAttribute = objDomBal.createAttribute("Haber")
   objMbrAttribute.nodeValue = strHaber
   objRootElem.setAttributeNode objMbrAttribute
 
   Set objMbrAttribute = objDomBal.createAttribute("SaldoFin")
   objMbrAttribute.nodeValue = strSaldoFin
   objRootElem.setAttributeNode objMbrAttribute
 
   CreateXMLBalanza = objDomBal.xml
 
End Function
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 147
Ha disminuido 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Generar XML correctamente

Publicado por Juan Gilberto (323 intervenciones) el 22/04/2015 18:31:24
Prueba con lo siguiente:
1
2
Set objRootElem = objDomBal.createElement("BCE:Ctas")
   objDomBal.appendChild objRootElem
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 23/04/2015 18:46:36
Ya lo había intentado así. Me sale el siguiente error que adjunto en la imagen y en la parte de codigo que aparece en la misma imagen.

With MSHFlexGrid1
Do While .TextMatrix(Fila, 0) <> vbNullString
objModule.loadXML (CreateXMLBalanza(.TextMatrix(Fila, 0), .TextMatrix(Fila, 1), .TextMatrix(Fila, 2), .TextMatrix(Fila, 3), .TextMatrix(Fila, 4)))

Set objNode = objModule.firstChild
objDom.documentElement.appendChild objNode----------AQUI MARCA EL ERROR


ERROR2
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 147
Ha disminuido 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Generar XML correctamente

Publicado por Juan Gilberto (323 intervenciones) el 24/04/2015 17:44:25
Es que lo que tienes que crear es un nuevo NODO al documento y asignarle el Namespace http://www.sat.gob.mx/cfd/3,

esto evita que en cada nodo que creemos nos aparezca un atributo xmlns vacio.

objDOM.createNode(1, ·BCE:Ctas, "http://www.sat.gob.mx/cfd/3")
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 24/04/2015 22:20:13
Gracias por constestar. Le puse lo siguiente:

Set objNode = objModule.createNode(NODE_ELEMENT, "BCE:Ctas", "www.sat.gob.mx/esquemas/ContabilidadE/1_1/BalanzaComprobacion")
objDom.documentElement.appendChild objNode

Lo que me falta ahora es que ya no m,e pone los los atributos del nodo.

Me hace todo el recorrido del MSHFlexgrid.

FALTA-ATT
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 147
Ha disminuido 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Generar XML correctamente

Publicado por Juan Gilberto (323 intervenciones) el 25/04/2015 19:48:44
Ok
Te falta asignar los atributos a las 'Ctas'... esto es despues de crear el nodo pero antes del append... para que cuando des el append ya lleve sus atibutos...


objNode.setAttribute "NumCta", strNumCta


...y asi con cada atributo dandole su nombre y valor correspondiente
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
sin imagen de perfil

Generar XML correctamente

Publicado por Ninho (14 intervenciones) el 27/04/2015 20:10:18
Ya quedó. Lo dejé como lo habías comentado anteriormente en la función Public Function CreateXMLBalanza de modificar:

1
2
Set objRootElem = objDomBal.createElement("BCE:Ctas")
objDomBal.appendChild objRootElem

Pero tuve que cambiar en el comando Private Sub CmdGenerarXML_Click() lo siguiente:
1
2
3
objModule.loadXML (CreateXMLBalanza(.TextMatrix(Fila, 0), .TextMatrix(Fila, 1), .TextMatrix(Fila, 2), .TextMatrix(Fila, 3), .TextMatrix(Fila, 4)))
Set objNode = objModule.firstChild
objDom.documentElement.appendChild objNode

Por esto:
1
2
objModule.loadXML (CreateXMLCatalogo(.TextMatrix(Fila, 0), .TextMatrix(Fila, 1), .TextMatrix(Fila, 2), .TextMatrix(Fila, 3), .TextMatrix(Fila, 4), .TextMatrix(Fila, 5)))
objDom.documentElement.appendChild objRootElem

Muchas gracias Juan Gilberto.
Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar