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

javascript - Get attribute value from xml by checking conditions - Stack Overflow

programmeradmin3浏览0评论

Following is my xml from where I have to get attribute value:

 <R a="1" b="2">
<I attribute1="" attribute2="some text"/>
<I attribute1="" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
</R>

Here I've to check if attribute1 is not null then I've to get value of attribute2 from I tag.How to do this??? Please help...

Following is my xml from where I have to get attribute value:

 <R a="1" b="2">
<I attribute1="" attribute2="some text"/>
<I attribute1="" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
</R>

Here I've to check if attribute1 is not null then I've to get value of attribute2 from I tag.How to do this??? Please help...

Share Improve this question edited Sep 17, 2012 at 11:41 user1495475 asked Sep 17, 2012 at 11:25 user1495475user1495475 1,0573 gold badges22 silver badges34 bronze badges 3
  • In what language and what have you tried? – J. K. Commented Sep 17, 2012 at 11:41
  • @Andreas:I have modified my xml in the post.Please check.Please ask me if you still don`t understand. – user1495475 Commented Sep 17, 2012 at 11:41
  • @IanKuca:I have a xml with me,from the xml I have to get attribute values as said based on the condition.I`m doing this in a js file. – user1495475 Commented Sep 17, 2012 at 11:43
Add a ment  | 

1 Answer 1

Reset to default 6

UPDATE:

Here's a full X-browser working script that should do the trick. Again, replace the getAttribute('attribute1') by either arguments or return the DOM and take care of the rest. This code might look a bit plicated (it uses closures to be as lightweight as possible) but it should be quite sturdy and safe to use... as long as you don't declare another function called parseXML and you don't call this parseXML prior to it being declared.

var parseXML = (function(w,undefined)
{
    'use strict';
    var parser,i,ie,parsed;
    ie = false;
    switch (true)
    {
        case w.DOMParser !== undefined:
            parser = new w.DOMParser();
        break;
        case new w.ActiveXObject("Microsoft.XMLDOM") !== undefined:
            parser = new w.ActiveXObject("Microsoft.XMLDOM");
            parser.async = false;
            ie = true;
        break;
        default :
            throw new Error('No parser found');
    }
    return function(xmlString,getTags)
    {
        var tags,keep = [];
        if (ie === true)
        {
            parser.loadXML(xmlString);
            parsed = parser;
        }
        else
        {
            parsed = parser.parseFromString(xmlString,'text/xml');
        }
        tags = parsed.getElementsByTagName(getTags);
        for(i=0;i<tags.length;i++)
        {
            if (tags[i].getAttribute('attribute1') && tags[i].getAttribute('attribute2'))
            {
                keep.push(tags[i].getAttribute('attribute2'));
            }
        }
        //optional:
        keep.push(parsed);//last element of array is the full DOM
        return keep;
    }
})(this);
var parseResult = parseXML('<r><i attribute1="" attribute2="Ignore This"/><i attribute1="foo" attribute2="got this"/></r>','i');
alert(parseResult[0] || 'nothing');//alerts 'got this' in IE and others

You can parse the XML:

var parser = new DOMParser();
var parsed = parser.parseFromString('<r a="1" b="2"><i v="" x="some text"/><i v="0" x="some important text"/></r>','text/xml');
var iTag = parsed.getElementsByTagName('i');
for (var i=0;i<iTag.length;i++)
{
    if (iTag[i].getAttribute('v'))
    {
        console.log(iTag[i].getAttribute('x'));//do whatever
    }
}

This snippet will log some important text, and not some text. That's all there is to it. If you need to store the x values, or return them just declare another variable:

var keep = [];//an array
//change console.log line by:
keep.push(iTag[i].getAttribute('x'));

This is assuming an x property will be set, if that's not always the case, an additional check can easily fix that. The full code will then look like:

function parseXML(xml)
{
    'use strict';
    var parser,keep,parsed,i,iTag;
    parser = new DOMParser();
    keep = [];
    parsed = parser.parseFromString(xml,'text/xml');//xml is the string
    iTag = parsed.getElementsByTagName('i');
    for (i=0;i<iTag.length;i++)
    {
        if (iTag[i].getAttribute('v') && iTag[i].getAttribute('x'))
        {
            keep.push(iTag[i].getAttribute('x'));
        }
    }
    return keep;//return array
}
发布评论

评论列表(0)

  1. 暂无评论