Hojoformo
asked on
Simple question - need help with creating a new XML element using DOM
I am trying to understand how to create new XML elements using DOM. I figured out how to create new elements but the opening element I am creating has a closing tag (/) on it. See the BillingCycleData element below. What am I doing wrong?
<BillingCycleData /> ??? i need to get rid of the "/"
<BillingCycleData meterReadDate="200609" revenue="140.98" usuage="1348" />
<BillingCycleData />
Here is my code
Set oXMLData1 = oXML.CreateElement("Billin gCycleData ") ----> this creates the <BillingCycleData />
oXML.documentElement.appen dChild(oXM LData1.clo neNode(tru e))
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin gCycleData ")
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen dChild(oXM LData.clon eNode(True ))
next
' write New record
oXML.documentElement.appen dChild(oXM LData1.clo neNode(Tru e)) ---> this creates the closing <BillingCycleData />
oXML.documentElement.appen dChild(oXM LRec.clone Node(True) )
<BillingCycleData /> ??? i need to get rid of the "/"
<BillingCycleData meterReadDate="200609" revenue="140.98" usuage="1348" />
<BillingCycleData />
Here is my code
Set oXMLData1 = oXML.CreateElement("Billin
oXML.documentElement.appen
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen
next
' write New record
oXML.documentElement.appen
oXML.documentElement.appen
ASKER
Hi SnowFlake,
Thanks for the quick response. The "<billingCycleData />" and "<BillingCycleData> " need to be two separate elements. The code I posted did not reflect the lower case "b" for the "<billingCycleData />" element I am trying to create. Here is what how my XML file looks now:
<?xml version="1.0" ?>
- <BillingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apogee.net/WebService">
<billingCycleData /> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
<billingCycleData />
</BillingData>
I am only having a problem with the closing tag on the 1st occurance of <billingCycleData />.
Set oXMLData1 = oXML.CreateElement("billin gCycleData ")
oXML.documentElement.appen dChild(oXM LData1.clo neNode(tru e)) --> Create opening <billingCycleData> ????
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin gCycleData ")
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen dChild(oXM LData.clon eNode(True ))
next
' write New record
oXML.documentElement.appen dChild(oXM LData1.clo neNode(Tru e)) --> Create closing <billingCycleData />
Thanks for the quick response. The "<billingCycleData />" and "<BillingCycleData> " need to be two separate elements. The code I posted did not reflect the lower case "b" for the "<billingCycleData />" element I am trying to create. Here is what how my XML file looks now:
<?xml version="1.0" ?>
- <BillingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apogee.net/WebService">
<billingCycleData /> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
<billingCycleData />
</BillingData>
I am only having a problem with the closing tag on the 1st occurance of <billingCycleData />.
Set oXMLData1 = oXML.CreateElement("billin
oXML.documentElement.appen
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen
next
' write New record
oXML.documentElement.appen
again, this is becuase you are adding its child nodes as a children of the document.
if instead of
<billingCycleData /> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
<billingCycleData />
</BillingData>
you want
<billingCycleData> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
</billingCycleData>
</BillingData>
you need to add the BillingCycleData elements as children of the biliingCycleData node,
so
replace
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin gCycleData ")
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen dChild(oXM LData.clon eNode(True ))
next
with
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin gCycleData ")
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXMLData1.appendChild(oXML Data.clone Node(True) ) //<--- change was in this line
next
also note that you are not creating tags but rather Nodes in a tree.
so <billingCycleData /> is not a closing tag but a node that has no childNodes.
I hope this helps.
SnowFlake
if instead of
<billingCycleData /> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
<billingCycleData />
</BillingData>
you want
<billingCycleData> ?????? help me get rid of this closing tag
<BillingCycleData meterReadDate="200510" revenue="88.04" usuage="1093" />
<BillingCycleData meterReadDate="200509" revenue="118.94" usuage="1208" />
</billingCycleData>
</BillingData>
you need to add the BillingCycleData elements as children of the biliingCycleData node,
so
replace
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXML.documentElement.appen
next
with
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
oXMLData1.appendChild(oXML
next
also note that you are not creating tags but rather Nodes in a tree.
so <billingCycleData /> is not a closing tag but a node that has no childNodes.
I hope this helps.
SnowFlake
ASKER
I understand your response now. However, when I make this change, it does not create any of the child nodes for <billingCycleData />. I am sure this has something to do with oXMLData1.appendChild(oXML Data.clone Node(True) ). Any suggestions? my xml file looks like:
<?xml version="1.0" ?>
- <BillingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apogee.net/WebService">
<UtilityId />
<billingCycleData />
</BillingData>
dim oXMLData1
Set oXMLData1 = oXML.CreateElement("billin gCycleData ")
oXML.documentElement.appen dChild(oXM LData1.clo neNode(tru e))
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin gCycleData ")
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
'oXML.documentElement.appe ndChild(oX MLData.clo neNode(Tru e))
oXMLData1.appendChild(oXML Data.clone Node(True) )
next
<?xml version="1.0" ?>
- <BillingData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apogee.net/WebService">
<UtilityId />
<billingCycleData />
</BillingData>
dim oXMLData1
Set oXMLData1 = oXML.CreateElement("billin
oXML.documentElement.appen
for HRi = 0 to UBound(results, 2)
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
'oXML.documentElement.appe
oXMLData1.appendChild(oXML
next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much SnowFlake for your help and patience.
your welcome.
if you add children to that node it will render with an opening and closing tag.
you are just adding the BillingCycleData to the document as siblings of what you wanted to be the parents.
Set oXMLData = oXML.CreateElement("Billin
oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
//oXML.documentElement.app
oXMLData1.appendChild(oXML
next
and drop the final line that as you commented "creates the closing <BillingCycleData />"
SnowFlake