473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element><element type="type2">element 4</element><element type="type2">element 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><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element></type1><type2><element type="type2">element 4</element><element type="type2">element 5</element></type2></root

Now, when doing this I first got all the element nodes into an IXMLDOMNodeList, I used the method .getElementsByTagName( "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 .getElementsByTagName 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 .getElementsByTagName 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 4414
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><element type="type1">element 1</element><element
type="type1">element 2</element><element type="type1">element
3</element><element type="type2">element 4</element><element
type="type2">element 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 SortXMLdocBySingleElement(pxmlDoc,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=SortXMLdocBySingleElement(xmldoc, _
' "MyElement","String","ASC")
'************************************************* ***
dim i, j,lChildNodes, vCurVal, vNewVal, oRoot
dim oReplacedNode, oNode, oNewNode
dim xmldoc
set xmldoc = pxmldoc
set oRoot = xmldoc.documentelement
lChildNodes=oRoot.childnodes.length

for i = 0 to lChildNodes - 2
set oNode = oRoot.childnodes(i)
vCurVal = ConvertToSpecifiedType(oNode.selectsinglenode(psEl ).text,psType)
for j = i + 1 to lChildNodes - 1
set oNewNode = oRoot.childnodes(j)
vNewVal =
ConvertToSpecifiedType(oNewNode.selectsinglenode(p sEl).text,psType)
if psDirection = "ASC" Then
if vNewVal < vCurVal then
set oReplacedNode =
oRoot.insertbefore(oRoot.childnodes(j),oRoot.child nodes(i))
vCurVal = vNewVal
end if
else
if vNewVal > vCurVal then
set oReplacedNode =
oRoot.insertbefore(oRoot.childnodes(j),oRoot.child nodes(i))
vCurVal = vNewVal
end if
end if
next
next
Set SortXMLdocBySingleElement = xmldoc
end function

function ConvertToSpecifiedType(pvData,psType)
Select Case psType
case "Integer": ConvertToSpecifiedType = CInt(pvData)
case "String": ConvertToSpecifiedType = CStr(pvData)
case "Long": ConvertToSpecifiedType = CLng(pvData)
case "Double": ConvertToSpecifiedType = CDbl(pvData)
case "Single": ConvertToSpecifiedType = CSng(pvData)
case "Date": ConvertToSpecifiedType = 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
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 ...
1
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 =...
14
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,...
1
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
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...
5
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...
2
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...
14
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...
14
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.