Link to home
Start Free TrialLog in
Avatar of svenkatapuram
svenkatapuram

asked on

create XML file from VB 6.0 application

Hi,

i have to create a XML file in my application using VB 6.0. I have some values in the address fields (in the application) like street, city etc.

The XML file format should be like this;

<?xml version="1.0" encoding="utf-8" ?>
<AddressVerificationInput InputStyle="Standard" Language="Spanish">    
        <AddressElement Type="Street">200 Chest nut Street</AddressElement>
        <AddressElement Type="City">Philadelphia</AddressElement>
        <AddressElement Type="State">PA</AddressElement>  
        <AddressElement Type="Zip">12345</AddressElement>
</AddressVerificationInput>

Thanks
Avatar of fds_fatboy
fds_fatboy

Is AddressVerificationInput  going to be your root?

Can you have more than one Address? If so, I assume that it is one per AddressVerificationInput, therefore you need a root node. If so, what is this going to be called?
The Microsoft XML Object Library can be used to create XML files.  Try the following:

    Dim oXML, oProcessingInformation, oAddressNode, oAddressNodeAttribute, oAddressElement, oAddressElementAttribute

    Set oXML = CreateObject("MSXML2.DOMDocument")

    'ADD VERSIONING/CODEPAGE HEADER
    Set oProcessingInformation = oXML.CreateProcessingInstruction("xml", " version=""1.0"" encoding=""UTF-8""")
    oXML.AppendChild oProcessingInformation

    'CREATE ADDRESSVERIFICATIONINPUT ELEMENT
    Set oAddressNode = oXML.CreateElement("AddressVerificationInput")

        'CREATE INPUTSTYLE ATTRIBUTE
        Set oAddressNodeAttribute = oXML.CreateAttribute("InputStyle")
        oAddressNodeAttribute.Text = "Standard"
        oAddressNode.Attributes.SetNamedItem oAddressNodeAttribute

        'CREATE LANGUAGE ATTRIBUTE
        Set oAddressNodeAttribute = oXML.CreateAttribute("Language")
        oAddressNodeAttribute.Text = "Spanish"
        oAddressNode.Attributes.SetNamedItem oAddressNodeAttribute

        'CREATE STREET ADDRESSELEMENT
        Set oAddressElement = oXML.CreateElement("AddressElement")
        oAddressElement.Text = "200 Chestnut Street"

            'CREATE TYPE ATTRIBUTE
            Set oAddressElementAttribute = oXML.CreateAttribute("Type")
            oAddressElementAttribute.Text = "Street"
            oAddressElement.Attributes.SetNamedItem oAddressElementAttribute

        oAddressNode.AppendChild oAddressElement

        'CREATE CITY ADDRESSELEMENT
        Set oAddressElement = oXML.CreateElement("AddressElement")
        oAddressElement.Text = "Philadelphia"

            'CREATE TYPE ATTRIBUTE
            Set oAddressElementAttribute = oXML.CreateAttribute("Type")
            oAddressElementAttribute.Text = "City"
            oAddressElement.Attributes.SetNamedItem oAddressElementAttribute

        oAddressNode.AppendChild oAddressElement

        'CREATE STATE ADDRESSELEMENT
        Set oAddressElement = oXML.CreateElement("AddressElement")
        oAddressElement.Text = "PA"

            'CREATE TYPE ATTRIBUTE
            Set oAddressElementAttribute = oXML.CreateAttribute("Type")
            oAddressElementAttribute.Text = "State"
            oAddressElement.Attributes.SetNamedItem oAddressElementAttribute

        oAddressNode.AppendChild oAddressElement

        'CREATE ZIP ADDRESSELEMENT
        Set oAddressElement = oXML.CreateElement("AddressElement")
        oAddressElement.Text = "12345"

            'CREATE TYPE ATTRIBUTE
            Set oAddressElementAttribute = oXML.CreateAttribute("Type")
            oAddressElementAttribute.Text = "Zip"
            oAddressElement.Attributes.SetNamedItem oAddressElementAttribute

        oAddressNode.AppendChild oAddressElement

    oXML.AppendChild oAddressNode
    oXML.Save "c:\test.xml"

    Set oXML = Nothing
    Set oAddressNode = Nothing
    Set oAddressNodeAttribute = Nothing
    Set oAddressElement = Nothing
    Set oAddressElementAttribute = Nothing

If you are only writing one <AddressVerificationInput /> per file, you can use the above.  Otherwise, create another node and use AppendChild to add all of the <AddressVerificationInput /> nodes to it, then use oXML.AppendChild on that root node.

HTH

J.
Gissa chance. I was waiting for a reply.
ASKER CERTIFIED SOLUTION
Avatar of fds_fatboy
fds_fatboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice wrapper, fds...
Please could you split the points, because jimbobmcgee's solution had the salient code. Mine just wrapped up similar code.