Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding Attributes to XML node using VBA

Adding Attributes to XML node using VBA

Scheduled Pinned Locked Moved Visual Basic
xmlhelp
25 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DaveAuld

    I had a think about what i had written, and no it won't throw an excpetion, All that will happen is MatchAll will always be False so that code is effectively doing nothing (because 1 or higher is always greater than 0). All you are doing is generating a bunch of strings and bolting them altogether. After you generate the xml and the function exits, have you had a look at the XML file to see what it looks like?

    Dave Don't forget to rate messages!
    Find Me On: Web|Facebook|Twitter|LinkedIn
    Waving? dave.m.auld[at]googlewave.com

    P Offline
    P Offline
    priyaahh
    wrote on last edited by
    #13

    Hi Dave, Thanks. Yes XML looks good. Regards, Priya.

    D 1 Reply Last reply
    0
    • P priyaahh

      Hi Dave, Thanks. Yes XML looks good. Regards, Priya.

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #14

      In that case all you are doing now then is adding the attributes to the node. Create a helper function like; getNodeAttributes(node as integer) as string In it, test node to see which attributes you need to build, then return them in the function, if the node does not need any attributes, then return an empty string. Then modify; strXML = strXML & "<" & Nodes(t) & ">" to strXML = strXML & "<" & Nodes(t) & getNodeAttributes(t) & ">" simples........

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn
      Waving? dave.m.auld[at]googlewave.com

      P 1 Reply Last reply
      0
      • D DaveAuld

        In that case all you are doing now then is adding the attributes to the node. Create a helper function like; getNodeAttributes(node as integer) as string In it, test node to see which attributes you need to build, then return them in the function, if the node does not need any attributes, then return an empty string. Then modify; strXML = strXML & "<" & Nodes(t) & ">" to strXML = strXML & "<" & Nodes(t) & getNodeAttributes(t) & ">" simples........

        Dave Don't forget to rate messages!
        Find Me On: Web|Facebook|Twitter|LinkedIn
        Waving? dave.m.auld[at]googlewave.com

        P Offline
        P Offline
        priyaahh
        wrote on last edited by
        #15

        Hi Dave, Thanks lot. I tried adding the funtion like :

        Function getNodeAttributes(node As String) As String
        Dim str_att As String
        If node = "Policy_Years" Then
        str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2="""
        End If
        End Function

        and also changed

        strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">"

        Just tried assigning attribute to only one node. But I am not getting any in the output for the tag Policy_Years. Please Help.. Regards, Priya.

        D 1 Reply Last reply
        0
        • P priyaahh

          Hi Dave, Thanks lot. I tried adding the funtion like :

          Function getNodeAttributes(node As String) As String
          Dim str_att As String
          If node = "Policy_Years" Then
          str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2="""
          End If
          End Function

          and also changed

          strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">"

          Just tried assigning attribute to only one node. But I am not getting any in the output for the tag Policy_Years. Please Help.. Regards, Priya.

          D Offline
          D Offline
          DaveAuld
          wrote on last edited by
          #16

          Of course you wont, your passing across the node index not the node name;

          priyaahh wrote:

          strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">"

          change to; strXML = strXML & "<" & Nodes(t) & getNodeAttributes("Policy_Years") & ">"

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn
          Waving? dave.m.auld[at]googlewave.com

          P 1 Reply Last reply
          0
          • D DaveAuld

            Of course you wont, your passing across the node index not the node name;

            priyaahh wrote:

            strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">"

            change to; strXML = strXML & "<" & Nodes(t) & getNodeAttributes("Policy_Years") & ">"

            Dave Don't forget to rate messages!
            Find Me On: Web|Facebook|Twitter|LinkedIn
            Waving? dave.m.auld[at]googlewave.com

            P Offline
            P Offline
            priyaahh
            wrote on last edited by
            #17

            Hi Dave, Still no its not showing... :-( Is there any other way to implement this? Regards, Priya.

            D 1 Reply Last reply
            0
            • P priyaahh

              Hi Dave, Still no its not showing... :-( Is there any other way to implement this? Regards, Priya.

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #18

              Well i fail to see what you are doing wrong. Don't give in! At the end of the day, all you are doing is simple string manipulation, it doesn't matter if it is in XML format or any other text based format. If your output XML file shows the attributes after your text generation then it is working. If your output does not show the attributes, then your code is wrong. You maybe need to start putting in extra breakpoints, debug.print(), or message boxes statements through your code until you can find out what is going on. everytime you generate a node or an attribute use these statements to see what the string is before and after. That way you will be able to see what you are getting versus what you are expecting at each stage. This is basic troubleshooting. As i have stated before your function is way to big, and you really need to restructure and break out lots of smaller helper functions which can then be called to piece the final text XML string. As i say, this is basic string manipulation, so something obvious is wrong somewhere!

              Dave Don't forget to rate messages!
              Find Me On: Web|Facebook|Twitter|LinkedIn
              Waving? dave.m.auld[at]googlewave.com

              P 1 Reply Last reply
              0
              • D DaveAuld

                Well i fail to see what you are doing wrong. Don't give in! At the end of the day, all you are doing is simple string manipulation, it doesn't matter if it is in XML format or any other text based format. If your output XML file shows the attributes after your text generation then it is working. If your output does not show the attributes, then your code is wrong. You maybe need to start putting in extra breakpoints, debug.print(), or message boxes statements through your code until you can find out what is going on. everytime you generate a node or an attribute use these statements to see what the string is before and after. That way you will be able to see what you are getting versus what you are expecting at each stage. This is basic troubleshooting. As i have stated before your function is way to big, and you really need to restructure and break out lots of smaller helper functions which can then be called to piece the final text XML string. As i say, this is basic string manipulation, so something obvious is wrong somewhere!

                Dave Don't forget to rate messages!
                Find Me On: Web|Facebook|Twitter|LinkedIn
                Waving? dave.m.auld[at]googlewave.com

                P Offline
                P Offline
                priyaahh
                wrote on last edited by
                #19

                Hi Dave, Thanks really!... I am planning another way. TO open the xml file generated and then adding attributes to elements. If you dont mind can you please give me example for this. Yes i remember, you have sent me the reference to DOM Methods for implementing this. But at this point of time with so much stress i cannt think of anything and this have to completed asap i do not have even hours of time...pls pls help.. Thanks in advance..reply if ur online Regards, Priya

                D 1 Reply Last reply
                0
                • P priyaahh

                  Hi Dave, Thanks really!... I am planning another way. TO open the xml file generated and then adding attributes to elements. If you dont mind can you please give me example for this. Yes i remember, you have sent me the reference to DOM Methods for implementing this. But at this point of time with so much stress i cannt think of anything and this have to completed asap i do not have even hours of time...pls pls help.. Thanks in advance..reply if ur online Regards, Priya

                  D Offline
                  D Offline
                  DaveAuld
                  wrote on last edited by
                  #20

                  No that is not the way to go, you are making things harder for your self, you will then have to read, parse, inject , save strings. You have already got a function that generates a valid XML file, so you are 99% of the way there. Have you done what i have suggested with regards to breakpoints and or debug statements? strategically place them in your code, and you will see where the attributes you have added are then disappearing (if they are disappearing at all that is). Also, in the previous message did you replace both instances of the code where you inject the attribute function? you have 2 lines that do it depending on the state of some value. Did you please a debug statement in your attribute function to prove it is being called? Did you add a debug.print statement to see what it was adding to the string?

                  Dave Don't forget to rate messages!
                  Find Me On: Web|Facebook|Twitter|LinkedIn
                  Waving? dave.m.auld[at]googlewave.com

                  P 1 Reply Last reply
                  0
                  • D DaveAuld

                    No that is not the way to go, you are making things harder for your self, you will then have to read, parse, inject , save strings. You have already got a function that generates a valid XML file, so you are 99% of the way there. Have you done what i have suggested with regards to breakpoints and or debug statements? strategically place them in your code, and you will see where the attributes you have added are then disappearing (if they are disappearing at all that is). Also, in the previous message did you replace both instances of the code where you inject the attribute function? you have 2 lines that do it depending on the state of some value. Did you please a debug statement in your attribute function to prove it is being called? Did you add a debug.print statement to see what it was adding to the string?

                    Dave Don't forget to rate messages!
                    Find Me On: Web|Facebook|Twitter|LinkedIn
                    Waving? dave.m.auld[at]googlewave.com

                    P Offline
                    P Offline
                    priyaahh
                    wrote on last edited by
                    #21

                    Dave, <pre attribute="vb"> Yes I did debut and breakpoints. Actually I have to keep getNodeAttribute function in the first set not in the second set. I examined that. During call of the above function i print the var in immediate window. it is printing like : Attribute=1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" Attribute2=" but when it go back to the main line from where are calling the function it is not returning anything.. Hope u might got my explanation..pls help This is my function: Function getNodeAttributes(node As String) As String dim str_att as string If node = "Policy_Years" Then str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2=""" ElseIf node = "Loss_Descriptions" Then str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2=""" Debug.Print str_att End If End Function Calling the above function from the below line: strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">" I wanted output like: <Adjusted_Loss_PD Attribute="1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" Attribute2=""> <Adjusted_Loss_PD-1 /> <Adjusted_Loss_PD-2 /> <Adjusted_Loss_PD-3 /> <Adjusted_Loss_PD-4 /> <Adjusted_Loss_PD-5 /> <Adjusted_Loss_PD-6 /> <Adjusted_Loss_PD-7 /> <Adjusted_Loss_PD-8 /> <Adjusted_Loss_PD-9 /> <Adjusted_Loss_PD-10 /> </Adjusted_Loss_PD> </pre>

                    D 1 Reply Last reply
                    0
                    • P priyaahh

                      Dave, <pre attribute="vb"> Yes I did debut and breakpoints. Actually I have to keep getNodeAttribute function in the first set not in the second set. I examined that. During call of the above function i print the var in immediate window. it is printing like : Attribute=1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" Attribute2=" but when it go back to the main line from where are calling the function it is not returning anything.. Hope u might got my explanation..pls help This is my function: Function getNodeAttributes(node As String) As String dim str_att as string If node = "Policy_Years" Then str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2=""" ElseIf node = "Loss_Descriptions" Then str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2=""" Debug.Print str_att End If End Function Calling the above function from the below line: strXML = strXML & "<" & Nodes(t) & getNodeAttributes(Nodes(t)) & ">" I wanted output like: <Adjusted_Loss_PD Attribute="1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" Attribute2=""> <Adjusted_Loss_PD-1 /> <Adjusted_Loss_PD-2 /> <Adjusted_Loss_PD-3 /> <Adjusted_Loss_PD-4 /> <Adjusted_Loss_PD-5 /> <Adjusted_Loss_PD-6 /> <Adjusted_Loss_PD-7 /> <Adjusted_Loss_PD-8 /> <Adjusted_Loss_PD-9 /> <Adjusted_Loss_PD-10 /> </Adjusted_Loss_PD> </pre>

                      D Offline
                      D Offline
                      DaveAuld
                      wrote on last edited by
                      #22

                      It looks like you are missing quotes from the output string. As you already know, you have to increase the number of quotes if you want to add quotes to the output, you are only doing this in some of your code.

                      priyaahh wrote:

                      str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2="""

                      Look at the code after "Attribute=" the opening value only has a single double quote, which will result in Attribute=1,1,....etc. and not Attribute="1,1,...etc. yet at the end you correctly add the extra quotes. you can always consider user Char(34) in you string generator so you can see where you want to add the quote to the output e.g. (34 is the ASCII value for a double quote, look at asciitable[^]) str_Att = "Attribute1=" & chr(34) & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" & chr(34) & " Attribute2=" & chr(34) & "some value" & chr(34)

                      Dave Don't forget to rate messages!
                      Find Me On: Web|Facebook|Twitter|LinkedIn
                      Waving? dave.m.auld[at]googlewave.com

                      P 2 Replies Last reply
                      0
                      • D DaveAuld

                        It looks like you are missing quotes from the output string. As you already know, you have to increase the number of quotes if you want to add quotes to the output, you are only doing this in some of your code.

                        priyaahh wrote:

                        str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2="""

                        Look at the code after "Attribute=" the opening value only has a single double quote, which will result in Attribute=1,1,....etc. and not Attribute="1,1,...etc. yet at the end you correctly add the extra quotes. you can always consider user Char(34) in you string generator so you can see where you want to add the quote to the output e.g. (34 is the ASCII value for a double quote, look at asciitable[^]) str_Att = "Attribute1=" & chr(34) & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" & chr(34) & " Attribute2=" & chr(34) & "some value" & chr(34)

                        Dave Don't forget to rate messages!
                        Find Me On: Web|Facebook|Twitter|LinkedIn
                        Waving? dave.m.auld[at]googlewave.com

                        P Offline
                        P Offline
                        priyaahh
                        wrote on last edited by
                        #23

                        Hi Dave, Yes I have noticed that and changed...but why the function is not returning anything. Regards, Priya.

                        D 1 Reply Last reply
                        0
                        • P priyaahh

                          Hi Dave, Yes I have noticed that and changed...but why the function is not returning anything. Regards, Priya.

                          D Offline
                          D Offline
                          DaveAuld
                          wrote on last edited by
                          #24

                          Because you haven't returned a value yet! remember you need to set the function name to the value you want to return;

                          Function getNodeAttributes(node As String) As String

                          'Do whatever you need

                          'Return the output to the caller
                          getNodeAttributes = str_att

                          End Function

                          Dave Don't forget to rate messages!
                          Find Me On: Web|Facebook|Twitter|LinkedIn
                          Waving? dave.m.auld[at]googlewave.com

                          1 Reply Last reply
                          0
                          • D DaveAuld

                            It looks like you are missing quotes from the output string. As you already know, you have to increase the number of quotes if you want to add quotes to the output, you are only doing this in some of your code.

                            priyaahh wrote:

                            str_att = "Attribute=" & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1"" Attribute2="""

                            Look at the code after "Attribute=" the opening value only has a single double quote, which will result in Attribute=1,1,....etc. and not Attribute="1,1,...etc. yet at the end you correctly add the extra quotes. you can always consider user Char(34) in you string generator so you can see where you want to add the quote to the output e.g. (34 is the ASCII value for a double quote, look at asciitable[^]) str_Att = "Attribute1=" & chr(34) & "1,1,0,1,16711680,2,0,0,1,16711680,0,0,0,0,0,0.5,10,Arial,1" & chr(34) & " Attribute2=" & chr(34) & "some value" & chr(34)

                            Dave Don't forget to rate messages!
                            Find Me On: Web|Facebook|Twitter|LinkedIn
                            Waving? dave.m.auld[at]googlewave.com

                            P Offline
                            P Offline
                            priyaahh
                            wrote on last edited by
                            #25

                            Hi Dave, Thank you so much for ur kind and immediate reply...it worked. :-) Regards, Priya.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups