获取具有特定属性值的元素的nodeValue

时间:2013-07-09 19:16:09

标签: javascript xml get

我有一个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,如何通过搜索属性名称等于“Security”的节点来访问“Security”的nodeValue为“1”?

3 个答案:

答案 0 :(得分:0)

您可以将收到的XML字符串设置为临时元素的innerHTML,然后通过document.querySelector()查询该元素(使用CSS样式选择器)。

示例(online demo here):

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

答案 1 :(得分: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";
    }

答案 2 :(得分: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;
                                }
                            }
                        }
                    }