Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Classic ASP XML Using ASP 3 and XML. See also the XML category for more XML discussions not relating to ASP. NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 2nd, 2010, 02:32 PM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Loop through xml

Im a bit stuck. Tried different things without result.
This is the xml i get from an application:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<wizard wizardid="Workflow1">
<action id="Uitleg2">Uitleg</action>
<action id="Uitleg1">Uitleg</action>
<action id="IfElseActivity1">IfElseActivity

<action id="IfElseBranchActivity1">IfElseBranchActivity
<action id="Uitleg3">Uitleg</action>
</action>
<action id="IfElseBranchActivity2">IfElseBranchActivity</action>
</action>
</wizard>
I want to loop trough this xml to read id and action (for example 'uitleg2' and 'uitleg') The xml can go a lot of levels deep. So I need a function to loop through the xml. This is what I have but, of course, does not work.

Code:
dim xml

Response.Buffer = True
Set xml =  server.createobject("Microsoft.xmldom")
xml.async = false
xml.load (Server.MapPath("test.xml"))
openXml()


function openXml()
	dim objChildNodes, strNode
	set objNode = XML.selectNodes("wizard/action")
    	For i = 0 To (objNode.length - 1)
        	Set objChildNodes = objNode(i)
				Readnode(objChildNodes)
		Next
End function

Function readNode(strNode2)
	on error resume next
	response.write(strNode2.nodename & " " & strNode2.text & " " & strNode2.getAttribute("id") & "<br>")
	on error goto 0
	
	If strNode2.hasChildNodes() then
        set oNodeList2 = strNode2.childnodes 
		For Each node2 In oNodeList2
				Readnode(node2)
		Next
	End if
End function
This is the result
Code:
action Uitleg Uitleg2
action Uitleg Uitleg1
action IfElseActivity IfElseBranchActivity Uitleg IfElseBranchActivity IfElseActivity1
action IfElseBranchActivity Uitleg IfElseBranchActivity1
action Uitleg Uitleg3
action IfElseBranchActivity IfElseBranchActivity2
This result is a bit funny...
Can someone help me out?

Last edited by hacking_mike; February 2nd, 2010 at 03:20 PM.
Reply With Quote
  #2 (permalink)  
Old February 2nd, 2010, 09:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Helps to format the XML readably...
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<wizard wizardid="Workflow1">
<action id="Uitleg2">Uitleg</action>
<action id="Uitleg1">Uitleg</action>
<action id="IfElseActivity1">
    IfElseActivity
    <action id="IfElseBranchActivity1">
        IfElseBranchActivity
        <action id="Uitleg3">Uitleg</action>
    </action>
    <action id="IfElseBranchActivity2">IfElseBranchActivity</action>
</action>
</wizard>
Okay, that's sensible.

So what *DO* you want the OUTPUT to be???

*IF* you intend to IGNORE the id values, then I would think that a reasonable result might be
Code:
Uitleg
Uitleg
IfElseActivity > IfElseBranchActivity > Uitleg
IfElseActivity > IfElseBranchActivity
But maybe that's not what you intended??
Reply With Quote
  #3 (permalink)  
Old February 3rd, 2010, 01:43 AM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Code:
I do want to have the id values back so:
uitleg2 > uitleg
uitleg1 > uitleg
Ifelseactivity1 > ifelseactivity
ifelsebranchactivity1 > ifelsebranchactivity
uitleg3 > uitleg
ifelsebranchactivity2 > ifelsebranchactivity
The i need to know the 'depth' of the children (is there a property for that?). I need to know that uitleg3 is a child of ifelsebranchactivity1 which is a child of ifelsebranchactivity

I hope this clears things up
Reply With Quote
  #4 (permalink)  
Old February 3rd, 2010, 04:32 AM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I want the output to be:
Code:
Uitleg Uitleg2
Uitleg Uitleg1
IfElseActivity IfElseActivity1
IfElseBranchActivity IfElseBranchActivity1
Uitleg Uitleg3
IfElseBranchActivity IfElseBranchActivity2
I also need to now the level of the children so that I know that IfElseBranchActivity1
and IfElseBranchActivity2 are children of IfElseActivity1 and Uitleg3 is a child of IfElseBranchActivity1.
Reply With Quote
  #5 (permalink)  
Old February 3rd, 2010, 02:08 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

But how do you want the data presented???

We could do this easily enough:
Code:
Uitleg2::Uitleg
Uitleg1::Uitleg
IfElseActivity1::IfElseActivity > IfElseBranchActivity1::IfElseBranchActivity > Uitleg3::Uitleg
IfElseActivity1::IfElseActivity > IfElseBranchActivity2::IfElseBranchActivity
Where I'm using :: to indicate id/text value pairs and > to indicate levels.
Change the delimiters to suit yourself.

Or we could do this:
Code:
(1)Uitleg2>Uitleg
(1)Uitleg1>Uitleg
(1)IfElseActivity1>IfElseActivity 
(2)IfElseBranchActivity1>IfElseBranchActivity 
(3)Uitleg3>Uitleg
(2)IfElseBranchActivity2>IfElseBranchActivity
That tells depth but doesn't say who is a child of whom.

Again, how should the full data be presented???




[/code]
Reply With Quote
  #6 (permalink)  
Old February 4th, 2010, 04:47 AM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default I think this will do it

Quote:
Originally Posted by Old Pedant View Post
But how do you want the data presented???

Code:
(1)Uitleg2>Uitleg
(1)Uitleg1>Uitleg
(1)IfElseActivity1>IfElseActivity 
(2)IfElseBranchActivity1>IfElseBranchActivity 
(3)Uitleg3>Uitleg
(2)IfElseBranchActivity2>IfElseBranchActivity
In the loop I want to create a dynamic form. With a case statement. So 'uitleg' means a textbox. With the value 'Uitleg2' I read in the database to have the initial value of the textbox.

'IfElseActivity' will be a dropdown. the IfElseBranchActivity's is the number of options in the dropdown and so on. So your example is ok. How do I achive this?
Reply With Quote
  #7 (permalink)  
Old February 4th, 2010, 03:24 PM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Please

Can someone help me out please. Tried for another day but still did not find the solution.
Reply With Quote
  #8 (permalink)  
Old February 4th, 2010, 08:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

It would have helped *A LOT* if you had simply SHOWN the <form> you want to produce from that HTML.

See, what you say *still* doesn't make sense.
Quote:
So 'uitleg' means a textbox. With the value 'Uitleg2'
FIne. Now what is the *NAME* of the textbox???
Is it supposed to be
Code:
<input name="uitleg" value="Uitleg2" />
or did you miswrite that??? Did you mean "name" when you wrote "value"?? That is:
Code:
<input name="Uitleg2"/>
???

PLEASE show the ACTUAL <form> that you want to have result from that XML.

This one, especially, makes no sense:
Code:
IfElseActivity1::IfElseActivity > IfElseBranchActivity1::IfElseBranchActivity > Uitleg3::Uitleg
??? You want a textbox *inside* a <select>??? I dont' get that.
Reply With Quote
  #9 (permalink)  
Old February 5th, 2010, 02:25 AM
Authorized User
 
Join Date: Feb 2010
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Try to explain

Quote:
Originally Posted by Old Pedant View Post
It would have helped *A LOT* if you had simply SHOWN the <form> you want to produce from that HTML.

See, what you say *still* doesn't make sense.

FIne. Now what is the *NAME* of the textbox???
Is it supposed to be
Code:
<input name="uitleg" value="Uitleg2" />
or did you miswrite that??? Did you mean "name" when you
The name for the textbox I will read from the database but the result will be something like this.

Code:
<input name="fromDatabase" value="Uitleg2" />
It is very complicated and I can't show you much as I'm still prototyping.
Can't you explain me how I can read the depth en how the text property only gives me the first "word".

Sorry that I can't make it very clear.

I read the xml > this gives me "uitleg2" > with the information from the database and the xml file I contruct a formfield.

Bottemline is: How do I get the information from the xml?
Reply With Quote
  #10 (permalink)  
Old February 5th, 2010, 06:07 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I'm still not sure exactly what to give you, but here:
Code:
<%
testxml = _
"<?xml version=""1.0"" encoding=""ISO-8859-1""?>" & vbNewLine & _
"<wizard wizardid=""Workflow1"">" & vbNewLine & _
"<action id=""Uitleg2"">Uitleg</action>" & vbNewLine & _
"<action id=""Uitleg1"">Uitleg</action>" & vbNewLine & _
"<action id=""IfElseActivity1"">" & vbNewLine & _
"    IfElseActivity" & vbNewLine & _
"    <action id=""IfElseBranchActivity1"">" & vbNewLine & _
"        IfElseBranchActivity" & vbNewLine & _
"        <action id=""Uitleg3"">Uitleg</action>" & vbNewLine & _
"    </action>" & vbNewLine & _
"    <action id=""IfElseBranchActivity2"">IfElseBranchActivity</action>" & vbNewLine & _
"</action>" & vbNewLine & _
"</wizard>"
 
Set xml = CreateObject("msxml2.DomDocument")
xml.LoadXML testxml
Set start = xml.SelectSingleNode("wizard") 
Recurse start, "", 0
Sub Recurse( node, path, level )
    Dim kids, kid, nname, nid
    If node.nodeTypeString = "text" Then
        Response.Write "(" & level & ") " & path & " = " & node.Text & "<br/>" & vbNewLine
    Else 
        nname = node.nodeName
        nid = node.Attributes(0).value
        Set kids = node.ChildNodes
        For Each kid In kids
            Recurse kid, path & "/" & node.nodeName & "[id=" & nid & "]", level+1
        Next
    End If
End Sub
%>
That code produces this output:
Code:
(2) /wizard[id=Workflow1]/action[id=Uitleg2] = Uitleg
(2) /wizard[id=Workflow1]/action[id=Uitleg1] = Uitleg
(2) /wizard[id=Workflow1]/action[id=IfElseActivity1] = IfElseActivity
(3) /wizard[id=Workflow1]/action[id=IfElseActivity1]/action[id=IfElseBranchActivity1] = IfElseBranchActivity
(4) /wizard[id=Workflow1]/action[id=IfElseActivity1]/action[id=IfElseBranchActivity1]/action[id=Uitleg3] = Uitleg
(3) /wizard[id=Workflow1]/action[id=IfElseActivity1]/action[id=IfElseBranchActivity2] = IfElseBranchActivity
The number at the front is the "depth" (level number) where the match was found.
The "path" then shows:
/nodeTagName[id=nodeId]
and, at the end, the text of the node, when found.

You should be able to remove the parts of that which you don't care about to get what you need.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through XML? Andy-7M XSLT 8 May 5th, 2007 06:03 AM
loop through a XML document and print out crmpicco Perl 1 November 13th, 2006 07:51 AM
Producing a loop of XML in CSV niceguycarl XSLT 0 February 6th, 2006 11:22 AM
Writing XML file within Loop (c#) rathbird General .NET 0 December 13th, 2004 04:26 PM
Loop in XML DOM? omallec XML 3 November 27th, 2003 08:16 AM



All times are GMT -4. The time now is 02:27 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.