Navigating child nodes - InfoPath Dev
in

InfoPath Dev

Navigating child nodes

Last post 08-20-2008 08:05 AM by ben.oates. 14 replies.
Page 1 of 1 (15 items)
Sort Posts: Previous Next
  • 08-13-2008 08:40 AM

    Navigating child nodes

    Hi all...

    I'm trying to follow the instructions at this web site: http://www.devguru.com/Technologies/xmldom/quickref/node_childNodes.html

    I am writing a supposedly simple piece of code but I'm really struggling to get it right. It's probably some simple mistake I am making so I thought I would post it here.

    The code:

    Set oNode = XDocument.Dom.selectNodes ("/my:myFields/my:Order")
    For Each cNode In oNode
        XDocument.UI.Alert cNode.nodeName
    Next

    This just brings up one alert saying "my:Order" and that's it!

    If I try to put .childNodes anywhere it tells me that the "Object doesn't support this property or method" and I should be iterating through the children (repeating elements SWID, HWID and UID and some non-repeating elements).

    Can anyone see where I am making my mistake, please?

    Thanks,

    Ben

  • 08-14-2008 12:25 PM In reply to

    Hi, Ben:

    Okay, your "my:Order" group -- is that a repeating group? When you say selectNodes, you can use that to bring back all instances of that group. So you are looping through all the instances of that group and popping up a message with the group's name (thus the single dialog box with "my:Order"). Before you change your code, just for fun, preview your form and add another row or section to the repeating section or table bound to "my:Order" (if, indeed, that is your repeating group) and click your button (or whatever you have attached to your code) and you'll get to see the alert twice!

    Now, to loop through the children of "my:Order", you could do something like this:

    Set oNode = XDocument.Dom.selectNodes ("/my:myFields/my:topGroup/my:repeatingGroup")
    For Each cNode In oNode
         For Each child In cNode.childNodes
              XDocument.UI.Alert child.nodeName
         Next
    Next

    First we are looping through all the "my:Order" nodes. We get all the children for the "my:Order" node we are currently looking at in our inner loop, then we go get another "my:Order" node.....

    Hilary Stoupa

  • 08-15-2008 02:36 AM In reply to

    Thanks Hilary, that worked a treat. I thought I had tried that but I must have missed childNodes off the end! Programming with XML is proving trickier than I thought!

  • 08-15-2008 04:22 AM In reply to

    If you have InfoPath 2007, you can use VSTA & C# or Visual Basic. You'll probably find it much easier with the Intellisense and debugging options available there--I know I have!
    Hilary Stoupa

  • 08-15-2008 05:03 AM In reply to

    I'm still patiently waiting for my boss to sign it off! Until then it's the standard 2003 script editor. I don't know how I ever programmed without Intellisense before!

    I don't know if this is a different context, but I'm still having a couple of problems with this.

    I'm trying to check the length of the nodes once I have found them and then add to their length until they match the length of another done. I am getting the following error:

    The namespace prefix my in the following element does not map to a URI: my:UID

    It's obviously called my:UID otherwise it wouldn't even call that sub in the first place! Any ideas?

    Sub btnJumpOrder_OnClick(eventObj)

    Set oNode = XDocument.Dom.selectNodes ("/my:myFields/my:Order")

    If orderIsFresh(oNode) Then
    Dim i, iNode
    Set uNode = XDocument.Dom.selectNodes ("/my:myFields/my:Users")

    'There is only one order
    Set iNode = oNode.item(0)

    For i=0 to uNode.length - 1
    Do While iNode.selectNodes("my:UID").length - 1 < i
    addDomElement iNode, "my:UID", ""
    Loop
    iNode.selectNodes("my:UID").item(i).Text = uNode.item(i).selectSingleNode("my:UNetID").Text
    Next

    End If
    End
    Sub

    Sub addDomElement(objParentNode, strName, strValue)
    Set objNode = XDocument.Dom.createElement(strName)
    If strValue <> "" Then objNode.Text = strValue
    objParentNode.appendChild objNode 'Error occurs here
    End Sub

  • 08-17-2008 06:09 PM In reply to

    Sadly, I'm barely fluent in VBscript. I'd guess by the error that you need to set the namespace property for the new node? I'm not sure. I found this post when I went searching for examples of the createElement method, but his main suggestion seemed to be to use the createNode method instead.

    Sorry, wish I could be of more help....

    Hilary Stoupa

  • 08-17-2008 11:50 PM In reply to

    Thanks Hilary. Unfortunately, our AUP restrictions stop me getting on to that link but I'll go hunting for the createNode method. Thanks again.

  • 08-18-2008 12:58 AM In reply to

    I'm still getting problems with createNode. When I try it without a namespaceURI it tells me the prefix "my" is not defined, but I have read the MS article and checked the template and xmlns:my is defined in there.

    Element '{http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56}UID' is unexpected according to content model of parent element '{http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56}Order'.

    The bit in the curly brackets is the NamespaceURI of "my" as defined in the template which is what the parser spewed out when I guessed it, so I tried using that string and still it isn't working!

    Sub addDomElement(objParentNode, strName, strValue)

        Set objNode = XDocument.Dom.createNode("element", strName, "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56")

        If strValue <> "" Then objNode.Text = strValue

        objParentNode.appendChild objNode

    End Sub

    I really don't know where I'm going wrong here. The MSDN help articles leave the NamespaceURI blank which is of no use to me because that just gives me this error:

    Reference to undeclared namespace prefix: 'my'.

  • 08-18-2008 09:06 AM In reply to

    Thanks for your e-mail Hilary. Unfortunately, I can not send XSN files out by e-mail. The filter thinks it's ActiveX. I have removed all data connections (potential privacy problems at work), but if you add two users on the user view and click "go forward to order" you will see where the error crops up. Hopefully this will help explain my issue. I just can't seem to crack it!

    Thanks,

    Ben

  • 08-18-2008 10:50 AM In reply to

    If you right click your xsn and select send to --> compressed folder (Windows XP), you can create a zip that your email filter will hopefully ignore.

    Hilary Stoupa

  • 08-19-2008 01:24 AM In reply to

    Unfortunately not... the filter stops zip files as well. Did the XSN attached to the previous post work?

  • 08-19-2008 04:33 AM In reply to

    I think I see the confusion now -- I'd intended for you to reply to my email with the file. You can also place it in the community uploads section of the Downloads on this site.

    Hilary Stoupa

  • 08-19-2008 08:22 AM In reply to

    They keep us quite effectively locked down around here!

    It was attached to a previous post: http://www.infopathdev.com/forums/storage/45/31513/ARSR%20Public.xsn

  • 08-19-2008 10:01 AM In reply to

    Try this: in your data source, move my:UID below my:Info, so it is the last field in the group. When I tried that, the error stopped popping. This poster had the same issue....

    Hilary Stoupa

  • 08-20-2008 08:05 AM In reply to

    Hi Hilary,

    That worked a treat for the single node, but unfortunately, there are three repeating nodes in there that all get added to by this function. Only the one at the bottom allows appendChild to work. From what you said I went hunting for something that inserts XML in a certain place assuming that the DOM is restricting me to keeping all UID, HWID and SWID items together. I found a number of references to an InsertAfter method to replace the appendChild method (code to follow) but apparently this isn't supported in VBScript.

    Does anyone know of an alternative that I can use to get this working?

    Thanks again,

    Ben

    Sub addDomElement(objParentNode, strName, strValue) Set objNode = XDocument.Dom.createNode("element", strName, "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-11-08T16:31:56")

    If strValue <> "" Then objNode.Text = strValue

    Set existingObjNode = objParentNode.selectNodes(strName)

    If existingObjNode.length > 0 Then

    Set existingObjNode = existingObjNode.item(existingObjNode.length -1)

    existingObjNode.ParentNode.InsertAfter objNode, existingObjNode 'ERROR HERE: InsertAfter is not a recognised method

    Else

    objParentNode.appendChild objNode

    End If

    End Sub

Page 1 of 1 (15 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.