473,795 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DOM CreateElement?

Using DOM, this is how I am appending data to an existing XML file
which is named AppendData.xml (the root element of AppendData.xml is
<ProductList> ):

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(Ser ver.MapPath("Ap pendData.xml"))

Dim eltProducts1 As XmlElement = xmlDoc.CreateEl ement("Products ")
Dim attProdID1 As XmlAttribute = xmlDoc.CreateAt tribute("ProdID 1")

eltProducts1.Se tAttributeNode( attProdID1)
eltProducts1.Se tAttribute("Pro dID1", "PID1")

Dim eltRoot1 As XmlElement = xmlDoc.Item("Pr oductList")
eltRoot1.Append Child(eltProduc ts1)

Dim eltName1 As XmlElement = xmlDoc.CreateEl ement("Name")
eltName1.InnerT ext = "Product1"
eltProducts1.Ap pendChild(eltNa me1)

Dim eltDescription1 As XmlElement =
xmlDoc.CreateEl ement("Descript ion")
eltDescription1 .InnerText = "Desc1"
eltProducts1.Ap pendChild(eltDe scription1)

Dim eltUnitPrice1 As XmlElement = xmlDoc.CreateEl ement("UnitPric e")
eltUnitPrice1.I nnerText = "250"
eltProducts1.Ap pendChild(eltUn itPrice1)

Dim eltProducts2 As XmlElement = xmlDoc.CreateEl ement("Products ")
Dim attProdID2 As XmlAttribute = xmlDoc.CreateAt tribute("ProdID 2")

eltProducts2.Se tAttributeNode( attProdID2)
eltProducts2.Se tAttribute("Pro dID2", "PID2")

Dim eltRoot2 As XmlElement = xmlDoc.Item("Pr oductList")
eltRoot2.Append Child(eltProduc ts2)

Dim eltName2 As XmlElement = xmlDoc.CreateEl ement("Name")
eltName2.InnerT ext = "Product2"
eltProducts2.Ap pendChild(eltNa me2)

Dim eltDescription2 As XmlElement =
xmlDoc.CreateEl ement("Descript ion")
eltDescription2 .InnerText = "Desc2"
eltProducts2.Ap pendChild(eltDe scription2)

Dim eltUnitPrice2 As XmlElement = xmlDoc.CreateEl ement("UnitPric e")
eltUnitPrice2.I nnerText = "200"
eltProducts2.Ap pendChild(eltUn itPrice2)

xmlDoc.Save(Ser ver.MapPath("Ap pendData.xml"))
End Sub
</script>

The above code successfully appends 2 sets of <Productsdata to the
AppendData.xml file which looks like this:

<ProductList>
<Products ProdID="PID1">
<Name>Product 1</Name>
<Description>De sc1</Description>
<UnitPrice>25 0</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product 2</Name>
<Description>De sc2</Description>
<UnitPrice>20 0</UnitPrice>
</Products>
</ProductList>

Note that in order to append 2 sets of <Productsdata to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Productsdata i.e. for the attribute ProdID, I used attProdID1 &
attProdID2, for the element Name, I used eltName1 & eltName2, for the
element Description, I used eltDescription1 & eltDescription2 & finally
for UnitPrice, I used eltUnitPrice1 & eltUnitPrice2. Had I used only 1
variable (say, attProdID for the attribute ProdID, eltName for the
element Name, eltDescription for the element Description & eltUnitPrice
for the element UnitPrice) to append the 2 sets of <Productsdata ,
only the 2nd set of <Productsdata would get appended to
AppendData.xml.

What I did like to know is - to append more than 1 set of <Products>
data to AppendData.xml, is there some other way out wherein using a
single variable for the attribute ProdID & elements Name, Description &
UnitPrice would create multiple sets of <Productsdata (by using
loops, maybe) instead of using different variables for the attribute
ProdID & elements Name, Description & UnitPrice to append each set of
<Productsdata (as shown in the ASPX code above)?

Thanks,

Arpan

Aug 23 '06 #1
3 2199


Arpan wrote:

The above code successfully appends 2 sets of <Productsdata to the
AppendData.xml file which looks like this:

<ProductList>
<Products ProdID="PID1">
<Name>Product 1</Name>
<Description>De sc1</Description>
<UnitPrice>25 0</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product 2</Name>
<Description>De sc2</Description>
<UnitPrice>20 0</UnitPrice>
</Products>
</ProductList>

Note that in order to append 2 sets of <Productsdata to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Productsdata i.e. for the attribute ProdID, I used attProdID1 &
attProdID2, for the element Name, I used eltName1 & eltName2, for the
element Description, I used eltDescription1 & eltDescription2 & finally
for UnitPrice, I used eltUnitPrice1 & eltUnitPrice2. Had I used only 1
variable (say, attProdID for the attribute ProdID, eltName for the
element Name, eltDescription for the element Description & eltUnitPrice
for the element UnitPrice) to append the 2 sets of <Productsdata ,
only the 2nd set of <Productsdata would get appended to
AppendData.xml.
I don't see any problem using one variable for each element type as long
as you create each element as needed and insert it e.g.

Dim Products As XmlElement
Dim Name As XmlElement
Dim Description As XmlElement
Dim UnitPrice As XmlElement

Products = xmlDoc.CreateEl ement("Products ")
Products.SetAtt ribute("ProdID" , "PID1")

Name = xmlDoc.CreateEl ement("Name")
Name.InnerText = "Product1"

Products.Append Child(Name)

Description = xmlDoc.CreateEl ement("Descript ion")
Description.Inn erText = "Desc1"

Products.Append Child(Descripti on)

UnitPrice = xmlDoc.CreateEl ement("UnitPric e")
UnitPrice.Inner Text = "250"

Products.Append Child(UnitPrice )

xmlDoc.Document Element.AppendC hild(Products)

Products = xmlDoc.CreateEl ement("Products ")
Products.SetAtt ribute("ProdID" , "PID2")

Name = xmlDoc.CreateEl ement("Name")
Name.InnerText = "Product2"

Products.Append Child(Name)

Description = xmlDoc.CreateEl ement("Descript ion")
Description.Inn erText = "Desc2"

Products.Append Child(Descripti on)

UnitPrice = xmlDoc.CreateEl ement("UnitPric e")
UnitPrice.Inner Text = "200"

Products.Append Child(UnitPrice )

xmlDoc.Document Element.AppendC hild(Products)

xmlDoc.Save(Ser ver.MapPath("fi le.xml"))

There is also no need to create attribute nodes if you simply use the
SetAttribute method on an XmlElement node anyway.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 23 '06 #2
Thanks Martin, your suggestions have really been very very helpful.
After going through your code, I realized that retrieving the root
element <ProductListusi ng

Dim eltRoot1 As XmlElement = xmlDoc.Item("Pr oductList"))

(as shown in the code in post #1) isn't necessary as well.

One last question please - what's the difference between a Node & an
Element in XML? Or are they one & the same?

Thanks once again,

Regards,

Arpan

Martin Honnen wrote:
Arpan wrote:

The above code successfully appends 2 sets of <Productsdata to the
AppendData.xml file which looks like this:

<ProductList>
<Products ProdID="PID1">
<Name>Product 1</Name>
<Description>De sc1</Description>
<UnitPrice>25 0</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product 2</Name>
<Description>De sc2</Description>
<UnitPrice>20 0</UnitPrice>
</Products>
</ProductList>

Note that in order to append 2 sets of <Productsdata to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Productsdata i.e. for the attribute ProdID, I used attProdID1 &
attProdID2, for the element Name, I used eltName1 & eltName2, for the
element Description, I used eltDescription1 & eltDescription2 & finally
for UnitPrice, I used eltUnitPrice1 & eltUnitPrice2. Had I used only 1
variable (say, attProdID for the attribute ProdID, eltName for the
element Name, eltDescription for the element Description & eltUnitPrice
for the element UnitPrice) to append the 2 sets of <Productsdata ,
only the 2nd set of <Productsdata would get appended to
AppendData.xml.

I don't see any problem using one variable for each element type as long
as you create each element as needed and insert it e.g.

Dim Products As XmlElement
Dim Name As XmlElement
Dim Description As XmlElement
Dim UnitPrice As XmlElement

Products = xmlDoc.CreateEl ement("Products ")
Products.SetAtt ribute("ProdID" , "PID1")

Name = xmlDoc.CreateEl ement("Name")
Name.InnerText = "Product1"

Products.Append Child(Name)

Description = xmlDoc.CreateEl ement("Descript ion")
Description.Inn erText = "Desc1"

Products.Append Child(Descripti on)

UnitPrice = xmlDoc.CreateEl ement("UnitPric e")
UnitPrice.Inner Text = "250"

Products.Append Child(UnitPrice )

xmlDoc.Document Element.AppendC hild(Products)

Products = xmlDoc.CreateEl ement("Products ")
Products.SetAtt ribute("ProdID" , "PID2")

Name = xmlDoc.CreateEl ement("Name")
Name.InnerText = "Product2"

Products.Append Child(Name)

Description = xmlDoc.CreateEl ement("Descript ion")
Description.Inn erText = "Desc2"

Products.Append Child(Descripti on)

UnitPrice = xmlDoc.CreateEl ement("UnitPric e")
UnitPrice.Inner Text = "200"

Products.Append Child(UnitPrice )

xmlDoc.Document Element.AppendC hild(Products)

xmlDoc.Save(Ser ver.MapPath("fi le.xml"))

There is also no need to create attribute nodes if you simply use the
SetAttribute method on an XmlElement node anyway.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 24 '06 #3


Arpan wrote:

One last question please - what's the difference between a Node & an
Element in XML? Or are they one & the same?
There are many different views and models and specifications. The XML
specification itself <http://www.w3.org/TR/REC-xml/does not talk about
nodes at all but object models do.
In the DOM data model and in the XPath data model there is a base type,
the node, and there are specializations of the node type.
So any element in the DOM is a node, an element node, but there are
other types of nodes, for instance text nodes, attribute nodes,
processing instruction nodes, comment nodes, the document node.
In the .NET DOM object model/class hierarchy that is reflected, there is
an abstract base class XmlNode from which several other node types are
derived
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfsystemxmlx mlnodeclasshier archy.asp>
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfsystemxmlx mllinkednodecla sshierarchy.asp >

As said, the XPath data model also knows nodes in general and
specializations of that general node type, like element nodes, attribute
nodes, text nodes, comment nodes, processing instruction nodes. There
are however some differences between how XPath models certain things in
an XML document and how the DOM models the same things. For instance XML
namespace declarations you find in XML document with namespace e.g.
<element xmlns="http://example.com/ns1">
or
<element xmlns:xlink="ht tp://www.w3.org/1999/xlink"
xlink:href="htt p://example.org/2006/ex1">
are simply modelled as attribute nodes in the DOM while the XPath data
model does not have those namespace declarations as attributes but
rather introduces special namespace nodes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
5200
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to calculate the totals in each row. when i try however, i receive the error: "Error: 'elements' is null or not an object"
2
42873
by: kie | last post by:
hello, when i create elements and want to assign events to them, i have realised that if the function assigned to that element has no parameters, then the parent node values can be attained. e.g. aTextBox=document.createElement('input'); aTextBox.onchange=calculateOneRow2;
1
21729
by: Weston C | last post by:
After noticing some problems with a script, I tried testing various bits in the squarefree javascript shell ( http://www.squarefree.com/shell/shell.html ) and noticed this: var anchor = document.createElement("a"); anchor undefined but:
4
4285
by: sg_maat | last post by:
I have a little problem with createElement(and msie). I was experimenting a bit with createElement and made the following script: <script> var tmp = document.createElement("div"); tmp.setAttribute("name", "tmpdiv2"); tmp.setAttribute("id", "calendar"); var image = document.createElement("img"); image.setAttribute("src", "img/ok.gif");
1
1607
by: JS | last post by:
The document object has a method called createElement. Can I use this method to create a new Drop/down list, like: var sel = document.createElement('SELECT'); I have seen it used like this: opt = document.createElement('OPTION');
9
1787
by: Andrew Poulos | last post by:
When I run the following code in FF 1.5 and 2, and IE 6 on my computer the table and image appears. When I run it on the client's XP machine it appears under FF 1.5 but under IE 6 the window is blank but IE appears to be in some sort of loop (after about 10 seconds I get the 'not responding' message in the title bar and have to close IE). What is it that causes it to behave differently on different computers? var t =...
3
4013
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to add another row. That seems to be working fine. How do I pull the information from the input boxes...
7
2527
by: Tarik Monem | last post by:
Why am I having so much trouble with using DOM in IE & Opera to create, then remove an Object & Embedded element? Here's the code that works in Firefox (Mac/Win/Linux) and Safari, but not on IE or Opera: var myfl_div = document.createElement('myfldiv'); myfl_div.setAttribute('style','position:absolute; left:100px; top:200px; width:960px; height: 560px; background-color: transparent;z-index: 5;'); function myFl_obj_emb() {
23
6635
by: vunet | last post by:
It is recommended by some sources I found to create IFrames in IE using document.createElement('<iframe src="#">') instead of document.createElement('iframe'). Why and what browser versions to use it? IE5 or IE6? Thanks
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.