user1495475
user1495475

Reputation: 1057

Get attribute value from xml by checking conditions

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

Upvotes: 1

Views: 6558

Answers (1)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

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 complicated (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
}

Upvotes: 6

Related Questions