Is there a way to getElement xml by attribute?

I am trying to grab an xml node by its attribute.

change

I am trying to get an element by its attribute value using javascript xml instead of jquery. Is there an easy way to do this?

+6
source share
4 answers

You must first find a list of elements and then filter their attributes.

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

+3
source
+1
source

I feel this justifies the new answer since 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; }); } 

a source


I was told that I sent the wrong script .. hehe read it 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; } 
+1
source

Very simple to use jQuery. Suppose we have a line 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 return the string "John".

+1
source

Source: https://habr.com/ru/post/895181/


All Articles