Solved

Simple question - need help with creating a new XML element using DOM

Posted on 2006-10-22
7
236 Views
Last Modified: 2008-03-06
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("BillingCycleData")  ----> this creates the <BillingCycleData />
 oXML.documentElement.appendChild(oXMLData1.cloneNode(true))  
 
 for HRi = 0 to UBound(results, 2)
 
  Set oXMLData = oXML.CreateElement("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    oXML.documentElement.appendChild(oXMLData.cloneNode(True))  
 next
 
 ' write New record  
 oXML.documentElement.appendChild(oXMLData1.cloneNode(True))   --->  this creates the closing <BillingCycleData />
 oXML.documentElement.appendChild(oXMLRec.cloneNode(True))  
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:SnowFlake
ID: 17784660
as long as your nodes don have children they will rende as <node />

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("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    //oXML.documentElement.appendChild(oXMLData.cloneNode(True))  
    oXMLData1.appendChild(oXMLData.cloneNode(True))  

 next

and drop the final line that as you commented "creates the closing <BillingCycleData />"

SnowFlake
0
 

Author Comment

by:Hojoformo
ID: 17784813
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("billingCycleData")
 oXML.documentElement.appendChild(oXMLData1.cloneNode(true))  --> Create opening  <billingCycleData> ????
 
 for HRi = 0 to UBound(results, 2)
 
  Set oXMLData = oXML.CreateElement("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    oXML.documentElement.appendChild(oXMLData.cloneNode(True))  
   
 next
 
 ' write New record  
 oXML.documentElement.appendChild(oXMLData1.cloneNode(True))   --> Create closing <billingCycleData />
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 17784935
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("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    oXML.documentElement.appendChild(oXMLData.cloneNode(True))  
 next

with

for HRi = 0 to UBound(results, 2)
 
  Set oXMLData = oXML.CreateElement("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    oXMLData1.appendChild(oXMLData.cloneNode(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
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:Hojoformo
ID: 17785025
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(oXMLData.cloneNode(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("billingCycleData")
     oXML.documentElement.appendChild(oXMLData1.cloneNode(true))  
 
 for HRi = 0 to UBound(results, 2)
 
  Set oXMLData = oXML.CreateElement("BillingCycleData")
    oXMLData.setAttribute "meterReadDate", results(BILL_DATE, HRi)
    oXMLData.setAttribute "revenue", results(BILL_AMOUNT, HRi)
    oXMLData.setAttribute "usuage", results(KILOWATT_USAGE, HRi)
    'oXML.documentElement.appendChild(oXMLData.cloneNode(True))  
    oXMLData1.appendChild(oXMLData.cloneNode(True))  
   
 next
0
 
LVL 15

Accepted Solution

by:
SnowFlake earned 500 total points
ID: 17785389
yes, of course,
as I noted on another one of your Q's
you are needlessly cloneing the nodes
whay happened in the example above is that you added to the document a clone of the node assigned to oXMLData1
and then the loop added the subnodes to the original node that is still assigned to oXMLData1

so change
 oXML.documentElement.appendChild(oXMLData1.cloneNode(true))
to
oXML.documentElement.appendChild(oXMLData1)

and also
oXMLData1.appendChild(oXMLData.cloneNode(True))
to
oXMLData1.appendChild(oXMLData)

although if you will leave the lastone you will still get the results you where looking for,
it is still bad for performance and there is no reason to do that as it is a node you just created.

You should use cloneNode when you need to create a copy of an existing node to prevent cross pointers.

SnowFlake
0
 

Author Comment

by:Hojoformo
ID: 17787528
Thank you very much SnowFlake for your help and patience.  
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 17787541
your welcome.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Oracle data into XML document 2 80
write xml in vb.net 2 49
XSLT 1.0 - Finding Substrings from the End of a String 3 50
Extracting nodes  using classical ASP 3 39
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question