DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Parsing attributes in VBScript

Hybrid View

  1. #1
    Igor Guest

    Parsing attributes in VBScript


    Hi everybody,
    I am brand new to XML and have a problem traversing attributes of the Node,
    I’ve got it working in VB, but on ASP page it does not work, why?
    That is what I do in VB:

    Private Function parseAttributes(ByRef oNode As MSXML2.IXMLDOMNode)
    Dim oAttr As MSXML2.IXMLDOMAttribute
    If oNode.Attributes.length > 0 Then
    For Each oAttr In oNode.Attributes
    Debug.Print oAttr.Name & “=” & oAttr.Value
    Next oAttr
    End If
    End Function

    But on ASP page using VBScript it gives me an error on the line
    If oNode.Attributes.length > 0 Then saying that oNode.Attributes is not
    a valid object or something like that.
    Please give me an idea how to loop through all attributes for given node
    on asp page using VBScript.
    Thanks in advance.
    Igor


  2. #2
    Russell Jones Guest

    Re: Parsing attributes in VBScript

    Here's an example. You'll need to enter your own file name and XSLT node
    selection query

    Russell Jones
    Sr. Web Development Editor
    DevX.com

    <%@ Language=VBScript %>
    <%
    dim xml
    set xml = server.CreateObject("msxml2.domdocument")
    if not xml.load ("your_filename_here.xml") then
    Response.Write xml.parseError.reason
    Response.End
    end if
    dim nodes
    dim node
    Function parseAttributes(node)
    dim att
    for each att in node.attributes
    Response.Write att.name & "=" & att.text & "<br>"
    next
    end function
    %>
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>

    <%
    set nodes = xml.selectNodes("your_query_here")
    for each node in nodes
    call parseAttributes(node)
    next

    %>

    </BODY>
    </HTML>

    "Igor" <rodic66@yahoo.com> wrote in message news:3a9a9189$1@news.devx.com...
    >
    > Hi everybody,
    > I am brand new to XML and have a problem traversing attributes of the

    Node,
    > I've got it working in VB, but on ASP page it does not work, why?
    > That is what I do in VB:
    >
    > Private Function parseAttributes(ByRef oNode As MSXML2.IXMLDOMNode)
    > Dim oAttr As MSXML2.IXMLDOMAttribute
    > If oNode.Attributes.length > 0 Then
    > For Each oAttr In oNode.Attributes
    > Debug.Print oAttr.Name & "=" & oAttr.Value
    > Next oAttr
    > End If
    > End Function
    >
    > But on ASP page using VBScript it gives me an error on the line
    > If oNode.Attributes.length > 0 Then saying that oNode.Attributes is not
    > a valid object or something like that.
    > Please give me an idea how to loop through all attributes for given node
    > on asp page using VBScript.
    > Thanks in advance.
    > Igor
    >




  3. #3
    Igor Guest

    Re: Parsing attributes in VBScript


    "Russell Jones" <arj1@northstate.net> wrote:
    >Here's an example. You'll need to enter your own file name and XSLT node
    >selection query
    >
    >Russell Jones
    >Sr. Web Development Editor
    >DevX.com
    >
    ><%@ Language=VBScript %>
    ><%
    >dim xml
    >set xml = server.CreateObject("msxml2.domdocument")
    >if not xml.load ("your_filename_here.xml") then
    > Response.Write xml.parseError.reason
    > Response.End
    >end if
    >dim nodes
    >dim node
    >Function parseAttributes(node)
    > dim att
    > for each att in node.attributes
    > Response.Write att.name & "=" & att.text & "<br>"
    > next
    >end function
    >%>
    ><HTML>
    ><HEAD>
    ></HEAD>
    ><BODY>
    >
    ><%
    >set nodes = xml.selectNodes("your_query_here")
    >for each node in nodes
    > call parseAttributes(node)
    >next
    >
    >%>
    >
    ></BODY>
    ></HTML>
    >

    Hi Russel,
    Thank you for answering my question so quick, it works like a charm, but
    unfortunately I cannot use this approach, because it works only when you
    explicitly pass selection query to xml.selectNodes(pattern). I better post
    all code for my little asp parser, so you have better understanding what
    I am trying to achieve here.
    Here it goes:
    <%@ Language=VBScript %>

    <%
    Response.Expires = "0"
    set xmlreceive = Server.CreateObject( "MSXML2.DOMDocument" )
    xmlreceive.async = false
    xmlreceive.validateonparse = true

    returncode = xmlreceive.load("d:\www\request.xml")

    if returncode then
    set node = xmlreceive.childNodes
    call xmlParser(node, 0)
    end if
    ' here is my little parser, step aside folks, Igor goes recursive
    '--------------- Node Parser--------------------------
    Private Function xmlParser( Nodes , intend )
    Dim oNode
    Dim i_int
    i_int = intend + 1
    For Each oNode In Nodes
    If LCase(oNode.nodeName <> "xml") Then
    If oNode.nodeType = NODE_CDATA_SECTION Or oNode.nodeType = NODE_TEXT
    Then
    Call PrintOut(oNode.nodeName, oNode.nodeValue, i_int)
    Else
    set oNamedNodeMap = oNode.attributes
    if isnull(oNamedNodeMap) then
    If IsNull(oNode.nodeValue) Then
    Call PrintOut(oNode.nodeName, "", i_int)
    Else
    Call PrintOut(oNode.nodeName, oNode.nodeValue, i_int)
    End If
    else
    Call PrintOut(oNode.nodeName, "", i_int)
    '--->>>Here I need to make a call to attribute parser<<<--
    end if
    If oNode.hasChildNodes Then
    Call xmlParser(oNode.childNodes, i_int)
    End If
    End If
    End If
    Next
    End Function
    '----------------Attribute parser--------------------
    Private Function parseAttributes( node , intend )
    dim att
    for each att in node.attributes
    Call PrintOut( att.name ,att.text,intend)
    next
    End Function
    '--------------Printing out "names = values" ---------------
    Private Sub PrintOut(strName , strValue , intend )
    Dim i
    Dim vSpace
    For i = 0 To intend
    vSpace = vSpace & "&nbsp;&nbsp;&nbsp;"
    Next
    If strValue = "" Then
    Response.Write vSpace & strName & "<br>"
    Else
    Response.Write vSpace & strName & "=" & strValue & "<br>"
    End If
    End Sub
    %>
    Thanks.
    I hope I am not being a pain in the butt.
    Igor.


  4. #4
    Rohit Wason Guest

    Re: Parsing attributes in VBScript


    Igor,

    you are doing two things wrong basically:
    1) ByRef ... in VBScript.
    2) As MSXML.... in VBScript!

    these goodies are not supported in VBScript yaar.
    just use
    Function parseAttributes(oNode)
    Dim oAttr
    If oNode.Attributes.length > 0 Then
    For Each oAttr In oNode.Attributes
    Response.Write oAttr.Name & “=” & oAttr.Value
    Next oAttr
    End If
    End Function
    This code will surely work!

    Rohit

    "Igor" <rodic66@yahoo.com> wrote:
    >
    >Hi everybody,
    >I am brand new to XML and have a problem traversing attributes of the Node,
    >I’ve got it working in VB, but on ASP page it does not work, why?
    >That is what I do in VB:
    >
    >Private Function parseAttributes(ByRef oNode As MSXML2.IXMLDOMNode)
    > Dim oAttr As MSXML2.IXMLDOMAttribute
    > If oNode.Attributes.length > 0 Then
    > For Each oAttr In oNode.Attributes
    > Debug.Print oAttr.Name & “=” & oAttr.Value
    > Next oAttr
    > End If
    >End Function
    >
    >But on ASP page using VBScript it gives me an error on the line
    >If oNode.Attributes.length > 0 Then saying that oNode.Attributes is not
    >a valid object or something like that.
    >Please give me an idea how to loop through all attributes for given node
    >on asp page using VBScript.
    >Thanks in advance.
    >Igor
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center