0

我有一个看起来像这样的 xml 文档:

<login>
   <status>Login successful</status>
   <ARole10082>Master</ARole10082>
   <IsAdmin10082>True</IsAdmin10082>
   <UserID10082>email@address.com</UserID10082>
   <privilege name="Billing">1</privilege>
   <privilege name="Nag">0</privilege>
   <privilege name="BoardForum">1</privilege>
   <privilege name="Association">1</privilege>
   <privilege name="Security">1</privilege>
   <privilege name="Appearance">1</privilege>
   <privilege name="Configure">1</privilege>
   <privilege name="MerchantAccounts">1</privilege>
</login>

我正在尝试访问名为“Security”的 Privilege 节点值。我正在使用 jquery $.get() 调用来调用这个 xml 文档。使用javascript,如何通过搜索属性名称等于“安全”的节点来访问“安全”的节点值“1”?

4

3 回答 3

0
var role = x.getElementsByTagName("login");
                    for (i=0; i<role.length; i++){
                        if (role[i].childNodes.length > 3){
                            privileges = role[0].getElementsByTagName("privilege");
                            for (j=0; j<privileges.length; j++){
                                name = privileges[j].attributes.name.value;
                                if (privileges[j].hasChildNodes()){
                                    value = privileges[j].childNodes[0].nodeValue;
                                }else{
                                    value = "0";
                                }

                                if (name === "Security" && value === "1"){
                                    localStorage["Security"] = 1;
                                }else{
                                    localStorage["Security"] = 0;
                                }
                            }
                        }
                    }
于 2013-07-09T20:36:24.323 回答
0

您可以将接收到的 XML 字符串设置为innerHTML临时元素,然后通过document.querySelector().

示例(此处为在线演示):

var xml = '<login>\
   <status>Login successful</status>\
   <ARole10082>Master</ARole10082>\
   <IsAdmin10082>True</IsAdmin10082>\
   <UserID10082>email@address.com</UserID10082>\
   <privilege name="Billing">1</privilege>\
   <privilege name="Nag">0</privilege>\
   <privilege name="BoardForum">1</privilege>\
   <privilege name="Association">1</privilege>\
   <privilege name="Security">1</privilege>\
   <privilege name="Appearance">1</privilege>\
   <privilege name="Configure">1</privilege>\
   <privilege name="MerchantAccounts">1</privilege>\
</login>';
var container = document.createElement('div');
container.innerHTML = xml;

var securityPrivilege =
                 container.querySelector('privilege[name="Security"]').innerHTML;
console.log('securityPrivilege: ', securityPrivilege);

输出:

securityPrivilege: 1
于 2013-07-09T19:39:16.147 回答
0

以编程方式选择正确的节点:

    public static string GetSecurityValue()
    {
        var document = new System.Xml.XmlDocument();
        document.Load("data.xml");

        foreach (System.Xml.XmlNode child in document.FirstChild.ChildNodes)
        {
            // Name is a bad attribut name, child.Name already exists, it will give you status, ARole10082, etc
            if (child.Attributes.Count > 0 && string.Equals("Security", child.Attributes["name"].Value))
            {
                return child.InnerText;
            }
        }

        return "Could not find security node";
    }
于 2013-07-09T19:46:57.823 回答