Archived Forum Post

Index of archived forum posts

Question:

XML Create subnodes off of a child node

Jun 29 '16 at 12:07

I'vd successfully used your email, ftp, and zip products for years. Now i'm trying to create an XML document using the ActiveX latest version of Chilkat. I do not know how to create a subnode off a Child node. Ex: create the address, city, statecode, postalcode, countrycode inside child2 ContactAddress and PhoneNumber inside child2 Phone.

<contact contacttype="Both"> <contactaddress>

1234 Oak Street
<city>Tallahassee</city> <statecode>FL</statecode> <postalcode>32304</postalcode> <countrycode>USA</countrycode> </contactaddress> <phone> <phonenumber>9995551212</phonenumber> </phone> </contact>

I've attached my current XML output, the XML i'm trying to model, and the FoxPro code i'm currently using...

Thanks...

My OutPut:

<batchdataset xmlns="http://www.slclearinghouse.com/XMLSchema/SLBatchFiling" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaversion="1.4" submissiontype="CHSLFiling"> <reportingentity> <nationalproducernumber>12345</nationalproducernumber> <emailaddress>testemail@sampledomain.com</emailaddress> </reportingentity> <contact contacttype="Both"> <firstname>Brian</firstname> <lastname>Smith</lastname> <emailaddress>testemail@sampledomain.com</emailaddress> <contactaddress/> <phone/> <fax>8505551213</fax> </contact> </batchdataset>

What I’m trying to duplicate (child subNode highlighted in Yellow):

<batchdataset xmlns="http://www.slclearinghouse.com/XMLSchema/SLBatchFiling" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaversion="1.4" submissiontype="CHSLFiling"> <reportingentity> <nationalproducernumber>12345</nationalproducernumber> <emailaddress>smith@slclearinghouse.com</emailaddress> </reportingentity> <contacts> <contact contacttype="Both"> <firstname>Brian</firstname> <lastname>Smith</lastname> <emailaddress>testemail@sampledomain.com</emailaddress> <contactaddress>
1234 Oak Street
<city>Tallahassee</city> <statecode>FL</statecode> <postalcode>32304</postalcode> <countrycode>USA</countrycode> </contactaddress> <phone> <phonenumber>8505551212</phonenumber> </phone> <fax>8505551213</fax> </contact> </contacts>

My Code in FoxPro:

LOCAL loXml LOCAL loChildNode

loXml = CreateObject('Chilkat_9_5_0.Xml') loXml1= CreateObject('Chilkat_9_5_0.Xml') loXml.Tag = "BatchDataSet" loXml.AddAttribute("xmlns","http://www.slclearinghouse.com/XMLSchema/SLBatchFiling") loXml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance") loXml.AddAttribute("SchemaVersion","1.4") loXml.AddAttribute("SubmissionType","CHSLFiling") loChildNode = loXml.NewChild("ReportingEntity","") loChildNode.NewChild2("NationalProducerNumber",uplxml1.v1cust1) loChildNode.NewChild2("EmailAddress",ALLTRIM((uplxml1.v1email1)))

loChildNode = loXml.NewChild("Contact","") loChildNode.AddAttribute("ContactType","Both") loChildNode.NewChild2("FirstName",ALLTRIM((uplxml1.firstname))) loChildNode.NewChild2("LastName",ALLTRIM((uplxml1.lastname))) loChildNode.NewChild2("EmailAddress",ALLTRIM((uplxml1.v1email1))) loChildNode.NewChild2("ContactAddress","") Need to do subNode here… loChildNode.NewChild2("Phone","") Need to do subNode here… loChildNode.NewChild2("Fax",ALLTRIM((uplxml1.v1fax1)))


Accepted Answer

I've typed up a quick example

LOCAL loXml as Chilkat_v9_5_0.ChilkatXml, loChildNode1, loChildNode2
loxml = CREATEOBJECT('Chilkat_9_5_0.Xml')
loxml.Tag = "BatchDataSet"
loChildNode1 = loXml.NewChild("Contact","")
loChildNode2 = loChildNode1.NewChild("ContactAddress", "")
loChildNode2.NewChild2("subNode","value")
?loXml.GetXml()

This is the output I get from it

<?xml version="1.0" encoding="utf-8" ?>
<BatchDataSet>
    <Contact>
        <ContactAddress>
            <subNode>value</subNode>
        </ContactAddress>
    </Contact>
</BatchDataSet>


Answer

I believe you would use NewChild to create the ContactAddress node. Then you will be able to continue adding new child nodes to it.

Tracy