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</ AddressEle ment>
<AddressElement Type="State">PA</AddressEl ement>
<AddressElement Type="Zip">12345</AddressE lement>
</AddressVerificationInput >
Thanks
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 Type="State">PA</AddressEl
<AddressElement Type="Zip">12345</AddressE
</AddressVerificationInput
Thanks
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.DOMDo cument")
'ADD VERSIONING/CODEPAGE HEADER
Set oProcessingInformation = oXML.CreateProcessingInstr uction("xm l", " version=""1.0"" encoding=""UTF-8""")
oXML.AppendChild oProcessingInformation
'CREATE ADDRESSVERIFICATIONINPUT ELEMENT
Set oAddressNode = oXML.CreateElement("Addres sVerificat ionInput")
'CREATE INPUTSTYLE ATTRIBUTE
Set oAddressNodeAttribute = oXML.CreateAttribute("Inpu tStyle")
oAddressNodeAttribute.Text = "Standard"
oAddressNode.Attributes.Se tNamedItem oAddressNodeAttribute
'CREATE LANGUAGE ATTRIBUTE
Set oAddressNodeAttribute = oXML.CreateAttribute("Lang uage")
oAddressNodeAttribute.Text = "Spanish"
oAddressNode.Attributes.Se tNamedItem oAddressNodeAttribute
'CREATE STREET ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres sElement")
oAddressElement.Text = "200 Chestnut Street"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type ")
oAddressElementAttribute.T ext = "Street"
oAddressElement.Attributes .SetNamedI tem oAddressElementAttribute
oAddressNode.AppendChild oAddressElement
'CREATE CITY ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres sElement")
oAddressElement.Text = "Philadelphia"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type ")
oAddressElementAttribute.T ext = "City"
oAddressElement.Attributes .SetNamedI tem oAddressElementAttribute
oAddressNode.AppendChild oAddressElement
'CREATE STATE ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres sElement")
oAddressElement.Text = "PA"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type ")
oAddressElementAttribute.T ext = "State"
oAddressElement.Attributes .SetNamedI tem oAddressElementAttribute
oAddressNode.AppendChild oAddressElement
'CREATE ZIP ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres sElement")
oAddressElement.Text = "12345"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type ")
oAddressElementAttribute.T ext = "Zip"
oAddressElement.Attributes .SetNamedI tem 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.
Dim oXML, oProcessingInformation, oAddressNode, oAddressNodeAttribute, oAddressElement, oAddressElementAttribute
Set oXML = CreateObject("MSXML2.DOMDo
'ADD VERSIONING/CODEPAGE HEADER
Set oProcessingInformation = oXML.CreateProcessingInstr
oXML.AppendChild oProcessingInformation
'CREATE ADDRESSVERIFICATIONINPUT ELEMENT
Set oAddressNode = oXML.CreateElement("Addres
'CREATE INPUTSTYLE ATTRIBUTE
Set oAddressNodeAttribute = oXML.CreateAttribute("Inpu
oAddressNodeAttribute.Text
oAddressNode.Attributes.Se
'CREATE LANGUAGE ATTRIBUTE
Set oAddressNodeAttribute = oXML.CreateAttribute("Lang
oAddressNodeAttribute.Text
oAddressNode.Attributes.Se
'CREATE STREET ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres
oAddressElement.Text = "200 Chestnut Street"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type
oAddressElementAttribute.T
oAddressElement.Attributes
oAddressNode.AppendChild oAddressElement
'CREATE CITY ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres
oAddressElement.Text = "Philadelphia"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type
oAddressElementAttribute.T
oAddressElement.Attributes
oAddressNode.AppendChild oAddressElement
'CREATE STATE ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres
oAddressElement.Text = "PA"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type
oAddressElementAttribute.T
oAddressElement.Attributes
oAddressNode.AppendChild oAddressElement
'CREATE ZIP ADDRESSELEMENT
Set oAddressElement = oXML.CreateElement("Addres
oAddressElement.Text = "12345"
'CREATE TYPE ATTRIBUTE
Set oAddressElementAttribute = oXML.CreateAttribute("Type
oAddressElementAttribute.T
oAddressElement.Attributes
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice wrapper, fds...
Please could you split the points, because jimbobmcgee's solution had the salient code. Mine just wrapped up similar code.
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?