javascript 有没有办法通过属性getElement xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7083341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:55:14  来源:igfitidea点击:

Is there a way to getElement xml by attribute?

javascriptxml

提问by Wenn

I'm trying to grab an xml node by its attribute.

我正在尝试通过其属性获取 xml 节点。

edit

编辑

I'm trying to retrieve an element by its attribute value using javascript xml instead of jquery. Is there a simple method for this?

我正在尝试使用 javascript xml 而不是 jquery 通过其属性值检索元素。有没有一个简单的方法呢?

采纳答案by Silkster

You have to find a list of elements first, then filter by their attributes.

您必须先找到元素列表,然后按其属性进行过滤。

Check out my demo here: http://jsfiddle.net/silkster/eDP5V/

在这里查看我的演示:http: //jsfiddle.net/silkster/eDP5V/

回答by Morio

It is really easy using jQuery. Suppose we have a string like this.

使用 jQuery 真的很容易。假设我们有一个这样的字符串。

<document>
   <value type="people">
      <name>
         John
      </name>
   </value>
   <value type="vehicle">
      <name>
         Ford
      </name>
   </value>
</document>

Then

然后

var xmlDocument = $.parseXML(str);
$(xmlDocument).find("value[type='people'] name").text()

We will get string 'John' back.

我们将取回字符串 'John'。

回答by rlemon

I feel this justifies a new answer as it is JQuery Free

我觉得这证明了一个新答案是合理的,因为它是 JQuery Free

document.getElementsByAttribute = function(attribute, value, tagName, parentElement) {
    var children = ($(parentElement) || document.body).getElementsByTagName((tagName || '*'));
    return $A(children).inject([], function(elements, child) {
        var attributeValue = child.getAttribute(attribute);
        if(attributeValue != null) {
            if(!value || attributeValue == value) {
                elements.push(child);
            }
        }
        return elements;
    });
}

source

来源



it has been pointed out to me that I posted the wrong script.. hehe read here

有人向我指出我发布了错误的脚本.. hehe 阅读这里

// document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaHyphenOrSpaceSeparatedList:false])
document.getElementsByAttribute=function(attrN,attrV,multi){
    attrV=attrV.replace(/\|/g,'\|').replace(/\[/g,'\[').replace(/\(/g,'\(').replace(/\+/g,'\+').replace(/\./g,'\.').replace(/\*/g,'\*').replace(/\?/g,'\?').replace(/\//g,'\/');
    var
        multi=typeof multi!='undefined'?
            multi:
            false,
        cIterate=document.getElementsByTagName('*'),
        aResponse=[],
        attr,
        re=new RegExp(multi?'\b'+attrV+'\b':'^'+attrV+'$'),
        i=0,
        elm;
    while((elm=cIterate.item(i++))){
        attr=elm.getAttributeNode(attrN);
        if(attr &&
            attr.specified &&
            re.test(attr.value)
        )
            aResponse.push(elm);
    }
    return aResponse;
}

回答by rlemon

jquery can do this very easily. http://think2loud.com/224-reading-xml-with-jquery/

jquery 可以很容易地做到这一点。http://think2loud.com/224-reading-xml-with-jquery/