javascriptxmle4x

Error adding TAG to XML and using variables (E4X)


I am just learning Javascript and XML and have two problems. This is the XML code I am using:

var xmlVar =    <zone>
            <entry name="template">
                <network>
                    <layer3>
                        <member>ethernet1/3</member>
                        <member>ethernet1/1</member>
                        <member>ethernet1/3.10</member>
                    </layer3>
                    <zone-protection-profile>zone-prot-prof-ex</zone-protection-profile>
                    <log-setting>log-forward-ex</log-setting>
                </network>
                <user-acl>
                    <include-list>
                        <member>192.168.0.0/24</member>
                    </include-list>
                    <exclude-list>
                        <member>10.0.0.0/23</member>
                    </exclude-list>
                </user-acl>
                <enable-user-identification>yes</enable-user-identification>
            </entry>
        </zone>;

Now I want to do two things. First I want to delete the members and add new ones. I can delete them:

delete xmlVar..member;

But when I try to add new members I run into the following error:

xmlVar.zone.entry.network.layer3.appendChild(<member>ethernet1</member>);

"The appendChild method works only on lists containing one item"

I have tried a million combinations and tried to find similar case on Internet but Each one gives this error or something similar. What am I missing?

Now second question. What if I want to use the XML variable as a template and replace certain parts. For example:

var xmlVar =    <zone>
            <entry name="{newEntryName}">
                <network>
                    {newLayer3}
                    <zone-protection-profile>zone-prot-prof-ex</zone-protection-profile>
                    <log-setting>log-forward-ex</log-setting>
                </network>
                <user-acl>
                    {newIncludeList}
                    <exclude-list>
                        <member>10.0.0.0/23</member>
                    </exclude-list>
                </user-acl>
                <enable-user-identification>yes</enable-user-identification>
            </entry>
        </zone>;

The first one trying to replace entry name with variable content doesn't work as there are quotes around the thing (can I escape them?) and I don't want to replace the whole tag. The other two work as long as I replace the whole tag. So is there any way to do this? Otherwise my backup plan is simply to use E4X and then simply overwrite the attribute name. But I would like to be able to have it be a variable.

Thankful for any ideas.


Solution

  • You do not need to declare the root node name when accessing child nodes.

    xmlVar.entry.network.layer3.appendChild(<member>ethernet1</member>);
    

    produces

    <zone>
      <entry name="template">
        <network>
          <layer3>
            <member>ethernet1</member>
          </layer3>
          <zone-protection-profile>zone-prot-prof-ex</zone-protection-profile>
          <log-setting>log-forward-ex</log-setting>
        </network>
        <user-acl>
          <include-list/>
          <exclude-list/>
        </user-acl>
        <enable-user-identification>yes</enable-user-identification>
      </entry>
    </zone>