开发者

find text string in XML

开发者 https://www.devze.com 2023-03-24 03:44 出处:网络
How would i find the xml node that has a certain text value? example i want to do something like this which is not working:

How would i find the xml node that has a certain text value?

example i want to do something like this which is not working:

var xml = "<data><itemB>more data</itemB><itemC>yes, more data</itemC><itemA>some data</itemA></data>";

var s开发者_Go百科earchString = "more data";

var foundin = $(xml + ":contains(" + searchString + ")");


You can invoke the :contains selector with the root XML element as its context:

var foundin = $(":contains(" + searchString + ")", xml);

You can see the results in this fiddle.


Try $(xml).children('*:contains("more data")')


These guys beat me to it, but here is the example I made.

    <script>
        $(function(){
            var xml = "<data><itemB>more data</itemB><itemC>yes, more data</itemC><itemA>some data</itemA></data>";
            var query = "more data";
            $(":contains(" + query + ")", xml).each(function(){
                alert(this.localName); //show node name
            });
        });
    </script>

Edit I'm assuming you're trying to get to the xml node with the query text. You probably want to use nodeName instead of localName for better IE support.

0

精彩评论

暂无评论...
验证码 换一张
取 消