SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Long Beach, CA
    0 Post(s)
    0 Thread(s)

    xml dom help needed!!!!!!!!!

    I'm working with DOM for the first time and i'm trying to loop through the entire xml doc to pull the name/value pairs of each attribute per element. Here's a sample of the xml doc:
    <?xml version="1.0" encoding="ISO-8859-1" ?> 
      <child attribute1="attribute1val" attribute2="attribute2val"/> 
      <child attribute1="attribute1val" attribute2="attribute2val"/>  
    I want to start with the first child, pull the name/val pair of attribute1 and attribute2, then do the same for the next child and so on. I intend to store the name value pair in variables to be used in ASP. I know i have to do a for loop within a for loop but i'm getting my nodes/objects mixed up. Thanks in advance.

  2. #2
    SitePoint Wizard
    Join Date
    Jan 2001
    Milton Keynes, UK
    0 Post(s)
    0 Thread(s)
    This isn't exactly want you want but will probably be enough to get you going.

    The IterateNodes function is just a recursive function that takes a nodelist object as an argument and displays the details of all the nodes in the nodelist, including any child nodes, and the node attributes.

    If you get stuck post some code and I'll see if I can help.

    <%@Language = "VBScript" %>
    Option Explicit
    Const NODE_TEXT = 3
    Public Function IterateXML(xmlFile)
    	Dim strXMLPath
    	Dim blnLoaded
    	Dim objXMLDOM
    	Dim objRoot
    	Dim objNodeList
    	'Get the absolute path to the xml file
    	strXMLPath = Server.MapPath(xmlFile) 
    	'Create and load the DOM object
    	Set objXMLDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
    	objXMLDOM.async = False
    	blnLoaded = objXMLDOM.load(strXMLPath)
    	'Display any errors
    	If objXMLDOM.parseError.errorcode <> 0 Then
    		Response.write "Error code: " & objXMLDOM.parseError.errorCode & "<br>"
    		Response.write "Error reason: " & objXMLDOM.parseError.reason & "<br>"
    		Response.Write "XML path: " & strXMLPath & "<br>"
    	End If
    	Response.Write "XML file loaded: " & blnLoaded & "<br>"
    	If blnLoaded Then
    		'Get the root and node list
    		Set objRoot = objXMLDOM.documentElement
    		Set objNodeList = objRoot.selectNodes("*")
    		'Call the IterateNodes function to start displaying node info
    		IterateNodes objNodeList
    		'Clean up
    		Set objNodeList = Nothing
    		Set objRoot = Nothing
    	End If
    	Set objXMLDOM = Nothing
    End Function
    Public Function IterateNodes(objNodeList)
    	Dim intNodeCnt
    	Dim objNode
    	Dim strNodeName
    	Dim strNodeText
    	Dim objAttrNodeList
    	Dim objAttrNode
    	Dim intAttrLength
    	Dim strAttrName
    	Dim strAttrText
    	Dim objChildNodes
    	'Get the node count
    	intNodeCnt = objNodeList.length
    	If intNodeCnt > 0 Then
    		'Loop around the node list
    		For Each objNode In objNodeList
    			'Get the node details into local variable
    			strNodeName = objNode.nodeName
    			Response.Write "<b>Node name:</b> " & strNodeName
    			If objNode.nodeType = NODE_TEXT Then
    				strNodeText = objNode.Text
    				'Display the node name and text
    				 Response.Write " <b>node text:</b> " & strNodeText
    			End If
    			Response.Write "<br>" & vbCrLf
    			'Get a node list of the attributes
    			Set objAttrNodeList = objNode.selectNodes("@*")
    			'Get the number of attributes
    			intAttrLength = objAttrNodeList.length
    			If intAttrLength > 0 Then
    				For Each objAttrNode In objAttrNodeList
    					'Get attrbute values into local variables
    					strAttrName = objAttrNode.nodeName
    					strAttrText = objAttrNode.Text
    					'Display attributes
    					Response.Write "<b>Attrbute:</b> " & strAttrName & " <b>text:</b> " & strAttrText & "<br>" & vbcrlf
    			End If
    			'Line break between nodes
    			Response.Write "<br>" & vbcrlf
    			'Get child nodes
    			Set objChildNodes = objNode.selectNodes("*")
    			If objChildNodes.length > 0 Then
    				'Call IterateNodes function to display child nodes
    				IterateNodes objChildNodes
    			End If
    			Set objChildNodes = Nothing
    	End If
    End Function
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <% IterateXML("test.xml") %>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts