10

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?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
Wenn
  • 339
  • 2
  • 4
  • 14
  • 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 Aug 16 '11 at 18:45
  • I'm trying to get an element using javascript instead of jquery.find("node[id='1']") – Wenn Aug 16 '11 at 18:51
  • Please don't prefix your titles with "Javascript:". That's what tags are for. – John Saunders Aug 16 '11 at 19:02
  • If you don't want to use jQuery, you could always use `DomParser` or `loadXML` in the case of IE: http://www.w3schools.com/Xml/xml_parser.asp But seriously, use jQuery. – Alex Churchill Aug 16 '11 at 19:04
  • It might be worth it to check this link: http://stackoverflow.com/questions/2694640/find-an-element-in-dom-based-on-an-attribute-value Not sure if it's what you're asking for though. – Pro Q Feb 11 '17 at 01:43

4 Answers4

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.

Morio
  • 8,463
  • 5
  • 25
  • 29
2

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;
}
rlemon
  • 17,518
  • 14
  • 92
  • 123
  • are you sure this is a jquery free answer? `$(parentElement)` ... ? – Brian Aug 16 '11 at 19:04
  • whoops! i was looking at two on the same forums.... http://www.codingforums.com/showthread.php?t=26744 I tested both.. seems to work for me. – rlemon Aug 16 '11 at 19:05
  • 1
    So not exactly jQuery free, but jQuery lite for sure - no extensions/plugins/modules... – Brian Aug 16 '11 at 19:07
  • Thanks anyways, I just looped through elements and compared the attributes to get the result. I just wanted to know if there was something more simplier like jquery. – Wenn Aug 16 '11 at 19:11
  • there is a jquery free one... the editor butchers the code.. sorry. – rlemon Aug 16 '11 at 19:20
2

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

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

Silkster
  • 2,190
  • 15
  • 28
1

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

rlemon
  • 17,518
  • 14
  • 92
  • 123