1
votes

im usin "DOMDocument60" with VB6, i need to generate a XML file, but i'm having a problem trying to add "Attibutes" to a subnode. This is the file that generates my code:

<myroot>
<MyNode SIZE="10">
<SubNode/>
</MyNode>
</myroot>

And this is what i need:

<myroot>
<MyNode SIZE="10">
**<SubNode CODE="0000" ID="XXX" OTHER="XXX"/>**
</MyNode>
</myroot>

This is the code (is based in http://msdn.microsoft.com/en-us/library/ms760231%28v=vs.85%29.aspx):

Private Function CrearDOM() Dim dom Set dom = New DOMDocument60 dom.async = False dom.validateOnParse = False dom.resolveExternals = False dom.preserveWhiteSpace = True Set CrearDOM = dom End Function

Public Sub Crear_XML()

Set dom = CrearDOM

' Encabezado de XML
Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'")
dom.appendChild node
Set node = Nothing

    Dim MyRoot
    Set MyRoot = dom.createElement("MasRequest")
MyRoot.appendChild dom.createTextNode(vbNewLine + vbTab)

    Set node = dom.createElement("MyNode")
    Set attr = dom.createAttribute("SIZE") 
    attr.Value = 10
    node.setAttributeNode attr
    Set attr = Nothing

        Set Nodo_Sub = dom.createDocumentFragment
        Nodo_Sub.appendChild dom.createTextNode(vbNewLine + vbTab + vbTab)
        Nodo_Sub.appendChild dom.createElement("SubNode")

    node.appendChild Nodo_Droga

MyRoot.appendChild node
End Sub

Thanks for the help.

1
Please show the code you already have so that it can be corrected.jac
sorry, but I'm new to the forum, so better re edit the original postAlx

1 Answers

3
votes

Your problem is you are not adding any attributes to the sub node after creating it. I worked up an example based on your code, but I changed some only because it is how I am used to doing it.

Private Function CrearDOM()
    Dim dom As DOMDocument60

    Set dom = New DOMDocument60
    With dom
        .async = False 
        .validateOnParse = False
        .resolveExternals = False
        .preserveWhiteSpace = True
    End With
    Set CrearDOM = dom
End Function

Public Sub Crear_XML()
    Dim dom As DOMDocument60
    Dim MyRoot As IXMLDOMNode
    Dim node
    Dim childNode As IXMLDOMNode
    Dim attr

    Set dom = New DOMDocument60
    With dom
        .async = False
        .validateOnParse = False
        .resolveExternals = False
        .preserveWhiteSpace = True
    End With

    Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'")
    dom.appendChild node
    Set node = Nothing

    Set MyRoot = dom.createElement("MasRequest")
    MyRoot.appendChild dom.createTextNode(vbNewLine + vbTab)

    Set node = dom.createElement("MyNode")
    Set attr = dom.createAttribute("SIZE")
    attr.Value = 10
    node.setAttributeNode attr
    Set attr = Nothing

    'create the child node'
    Set childNode = dom.createElement("SubNode")
    Set attr = dom.createAttribute("CODE")
    attr.Value = "0000"
    childNode.Attributes.setNamedItem attr
    Set attr = dom.createAttribute("ID")
    attr.Value = "XXX"
    childNode.Attributes.setNamedItem attr
    Set attr = dom.createAttribute("OTHER")
    attr.Value = "XXX"
    childNode.Attributes.setNamedItem attr
    node.appendChild childNode
    MyRoot.appendChild node
    dom.appendChild MyRoot

End Sub

The code above generated this for me. (I added the line feeds and indentation.)

<?xml version="1.0"?>
<MasRequest>
    <MyNode SIZE="10">
        <SubNode CODE="0000" ID="XXX" OTHER="XXX"/>
    </MyNode>
</MasRequest>