473,587 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with iteration of IXMLDOMNodeList

I have a problem that I can't work out. I have a script in ASP using VBScript that act quite funny when editing and xml file. Here is my explenation

I have the following xml-fil

<root><elemen t type="type1">el ement 1</element><elemen t type="type1">el ement 2</element><elemen t type="type1">el ement 3</element><elemen t type="type2">el ement 4</element><elemen t type="type2">el ement 5</element></root

I wanted to order them by type, by creating a new node for each type in the root element and then inserting the corresponding elements into the type node. The output should be looking like this

<root><type1><e lement type="type1">el ement 1</element><elemen t type="type1">el ement 2</element><elemen t type="type1">el ement 3</element></type1><type2><e lement type="type2">el ement 4</element><elemen t type="type2">el ement 5</element></type2></root

Now, when doing this I first got all the element nodes into an IXMLDOMNodeList , I used the method .getElementsByT agName( "element" ). No problem there. Since I don't like to use "For X = 0 To Y" I used "For Each X In Y" instead, where Y was the IXMLDOMNodeList
Then, within the loop i took the current Nodes parent and appended a child to it with the type as the nodeName, if the type was different from the last nodes type. Then I kept adding current node to the nodes parents last child, which resulted in me moving the elements into the correct type node

When doing this the first time i printed the length of the IXMLDOMNodeList at the beginning of the file, but when i removed it everything went crazy. It seems that when I dodn't check the length (either by responsing it or just by setting a "dumb" variable to it) the loop went on forever. I read that the IXMLDOMNodeList was live and that got me thinking that for each loop the referense to .getElementsByT agName was run and it would keep finding new ones. But that still dowesn't explain why it worked when geting the length of it first hand. I later on tried the "For X = 0 To IXMLDOMNodeList .length -1" approach and that worked fine since i called upon the length property
Then I got back to the "For Each X In Y" code and added a exact copy of the enumeration before the orgininal one, without doing anything, and that seemed to "lock" the length and worked fine. This got me really confused

The last thing I tried was to use the selectNodes method where i specifically got the "/root/element" nodes and that turned out great. When using "//element" as the XPath the script acted as when I use the .getElementsByT agName approach. I stuck with this just to get moving but I am still not up to speed what was going on. Is this a bug or is it supposed to re-run the search every iteration when using "For Each X In Y"

Please, put me at ease and explain this to me. I used DOMDocument.3.0

Many thanks in advance
Martin Danielson
Jul 19 '05 #1
1 4435
Martin Danielson wrote:
I have a problem that I can't work out. I have a script in ASP using
VBScript that act quite funny when editing and xml file. Here is my
explenation:

I have the following xml-file

<root><elemen t type="type1">el ement 1</element><elemen t
type="type1">el ement 2</element><elemen t type="type1">el ement
3</element><elemen t type="type2">el ement 4</element><elemen t
type="type2">el ement 5</element></root>

I wanted to order them by type, by creating a new node for each type
in the root element and then inserting the corresponding elements
into the type node.


Well, you are doing it the hard way. Here is a function (actually, two
functions) from my code library that I wrote for re-ordering an xml
document. See if you can adapt it to your needs:

function SortXMLdocBySin gleElement(pxml Doc,psEl, _
psType, psDirection)
'************** *************** *************** ********
'Pass an xml document, an element name, a datatype name
'(Integer, String, Long, Double, Single, or Date) and a direction
'(ASC or DESC). Usage example:
' set xmldoc=SortXMLd ocBySingleEleme nt(xmldoc, _
' "MyElement","St ring","ASC")
'************** *************** *************** ********
dim i, j,lChildNodes, vCurVal, vNewVal, oRoot
dim oReplacedNode, oNode, oNewNode
dim xmldoc
set xmldoc = pxmldoc
set oRoot = xmldoc.document element
lChildNodes=oRo ot.childnodes.l ength

for i = 0 to lChildNodes - 2
set oNode = oRoot.childnode s(i)
vCurVal = ConvertToSpecif iedType(oNode.s electsinglenode (psEl).text,psT ype)
for j = i + 1 to lChildNodes - 1
set oNewNode = oRoot.childnode s(j)
vNewVal =
ConvertToSpecif iedType(oNewNod e.selectsinglen ode(psEl).text, psType)
if psDirection = "ASC" Then
if vNewVal < vCurVal then
set oReplacedNode =
oRoot.insertbef ore(oRoot.child nodes(j),oRoot. childnodes(i))
vCurVal = vNewVal
end if
else
if vNewVal > vCurVal then
set oReplacedNode =
oRoot.insertbef ore(oRoot.child nodes(j),oRoot. childnodes(i))
vCurVal = vNewVal
end if
end if
next
next
Set SortXMLdocBySin gleElement = xmldoc
end function

function ConvertToSpecif iedType(pvData, psType)
Select Case psType
case "Integer": ConvertToSpecif iedType = CInt(pvData)
case "String": ConvertToSpecif iedType = CStr(pvData)
case "Long": ConvertToSpecif iedType = CLng(pvData)
case "Double": ConvertToSpecif iedType = CDbl(pvData)
case "Single": ConvertToSpecif iedType = CSng(pvData)
case "Date": ConvertToSpecif iedType = CDate(pvData)
end select
end function
There are also techniques using xsl transformations that can make short work
of this. A Google search should turn up some examples.

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #2

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

Similar topics

35
3721
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ------------------------------------------------------------- PEP: 323
1
1739
by: marcel.vandendungen | last post by:
Hi, I'm using MSXML to select elements from a XML document and want to slice off the last part of an IXMLDOMNodeList. >>> import win32com.client >>> xmldoc = win32com.client.Dispatch('msxml.DOMDocument') >>> xmldoc.load('file.xml') True >>> rgelem = xmldoc.selectNodes('/root/elem')
14
1839
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works, but I still get some errors when i try to compile it: stack.c: In function 'reverseFile'
1
1315
by: PaulF | last post by:
I am trying to do add a repeating XmlNode into and existing XML document and have had some problems. The base XML: <Property> <Premises> <Endorsement> <ShortWording/> <Wording/>
3
2745
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead of getting a list that should look like this
5
2470
by: BeruthialsCat | last post by:
First go with trying to import xml to a database and whilst i have managed to do what i want i find that the xml files we have here at work are gonna cause me problems. I have a function that imports any aml file, at the moment its importing to a hardcoded (name only) table but i can manage to work out how to take the xml file name and use that for the table name. My problem is some of the xml files do not follow the structural...
2
1813
by: Michael Russell | last post by:
Hi all, I'm trying to get a client working with my ASP.NET web service. The client's been working great up to this point with a SOAP Toolkit-generated service based on a VB COM library. On the client side, I'm calling the web service here: -- Dim soapClient As SoapClient30 Dim xmlList As IXMLDOMNodeList
14
2793
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that shows all the properties, weeks available, pricing, etc. for a particular area of Europe. The data varies widely depending on the region; at times there will be 50 properties, and at other times only a half dozen. The cross referencing of...
14
3776
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in C#? Because, usually in C# as you already know we use DLLImport and extern
0
8220
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
7981
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
8222
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
6632
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
5723
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
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2367
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
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.