最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to getElement xml by attribute? - Stack Overflow

programmeradmin1浏览0评论

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

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?

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

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?

Share Improve this question edited Oct 5, 2015 at 22:33 temporary_user_name 37.1k48 gold badges160 silver badges243 bronze badges asked Aug 16, 2011 at 18:44 WennWenn 3492 gold badges4 silver badges14 bronze badges 5
  • 7 Don't do that. It's rude. Try to speak to the element before grabbing it by its attribute. Seriously, can you elaborate on your question? What did you try and what were the results? – Frédéric Hamidi Commented Aug 16, 2011 at 18:45
  • I'm trying to get an element using javascript instead of jquery.find("node[id='1']") – Wenn Commented Aug 16, 2011 at 18:51
  • Please don't prefix your titles with "Javascript:". That's what tags are for. – John Saunders Commented Aug 16, 2011 at 19:02
  • If you don't want to use jQuery, you could always use DomParser or loadXML in the case of IE: w3schools.com/Xml/xml_parser.asp But seriously, use jQuery. – Alex Churchill Commented Aug 16, 2011 at 19:04
  • It might be worth it to check this link: stackoverflow.com/questions/2694640/… Not sure if it's what you're asking for though. – Pro Q Commented Feb 11, 2017 at 1:43
Add a comment  | 

4 Answers 4

Reset to default 5

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

<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.

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

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

I feel this justifies a new answer as it is 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

// 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;
}

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

发布评论

评论列表(0)

  1. 暂无评论