Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:58:51+00:00 2026-06-11T10:58:51+00:00

Following is my xml from where I have to get attribute value: <R a=1

  • 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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:58:52+00:00Added an answer on June 11, 2026 at 10:58 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get one of the attributes from the following xml,
I have the following XML LINQ query from my XDocument. var totals = (from
I have to read the xml node name from the following XML, but I
I have following object structure, deseralized from XML (WS): <ns2:Category> <ns2:CategoryId>800003</ns2:CategoryId> <ns2:CategoryName>Name1</ns2:CategoryName> <ns2:Categories> <ns2:Category>
I have following code for loading image from url in xml parsing endElement method
From windows event viewer I can get the following xml structure: <Event xmlns=http://schemas.microsoft.com/win/2004/08/events/event> <System>
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I found the following which shows how to delete an attribute from XML in
I have the following XML: <products> <product> <name>Flat Panel Monitor</name> <code>LS123</code> <attributes> <attribute> <group>Color</group>
I have some output from 3rd party software: Sample XML from the software: <testsuite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.