1

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

user1495475
  • 1,037
  • 3
  • 20
  • 34

1 Answers1

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 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
}
Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Van:Thanks for the reply and answer.I`ll just check this and let you know.What do you mean by 'use strict'; here?? – user1495475 Sep 17 '12 at 11:47
  • @user1495475: You're welcome. I've checked it in chrome, but you should check the link if your code needs to run on (older) IE versions. Also: don't forget to replace `getAttribute('v')` and stuff with the correct names, or turn them into parameters... – Elias Van Ootegem Sep 17 '12 at 11:49
  • van:I tried the case,it`s throwing error like 'Domparser is undefined'.I checked it in google,old IEs` have this problem.Do you suggest me other way please??? – user1495475 Sep 17 '12 at 11:59
  • I'll update my answer, giving you a full X-browser compatible sollution – Elias Van Ootegem Sep 17 '12 at 12:11
  • @user1495475: made a couple of little mistakes in my first full version. This one works. [As you can see here](http://jsfiddle.net/d6cgf/) – Elias Van Ootegem Sep 17 '12 at 12:51