Jump to content

How to get return as XML by Javascript if condition is sucess


nalinishrma1@gmail.com

Recommended Posts

Hi,


I have 2 XML stored in the variable 'ad' and 'addd' respectively:

Variable 'ad':


<?xml version="1.0" encoding="UTF-8"?>

<name>

<data>

<Id>003</Id>

</data>

<data>

<Id>006</Id>

</data>

....

</name>


And Variable 'addd':


<?xml version="1.0" encoding="UTF-8"?>

<name>

<data>

<Id>009</Id>

</data>

<data>

<Id>005</Id>

</data>

...

</name>


I have written a `ttt` Javascript function:


function ttt(ad,addd) {

var match = ad.match(/<Id\/>/);

var matcht = addd.match(/<Id\/>/);

if ((! match || match.length == 0) && (! matcht || match.length == 0)){

return "Below is the details of the Id of ad:\n\n" + ad.split("<Id>")[1].split("</Id>")[0]; + "\n\n And, Below is the details of the Id of addd:\n\n" +addd.split("<Id>")[1].split("</Id>")[0];

}

}


I need `If` `Id` is not blank from 'ad' variable then return alert as `error` with `Id` as below, otherwise not:


<?xml version="1.0" encoding="UTF-8"?>

<name>

<data>

<Id>003</Id>

</data>

<data>

<Id>006</Id>

</data>

</name>


And, I need `If` `Id` is not blank from 'addd' variable then return alert as `error` with `Id` as below, otherwise not:



<?xml version="1.0" encoding="UTF-8"?>

<name>

<data>

<Id>009</Id>

</data>

<data>

<Id>005</Id>

</data>

</name>





Link to comment
Share on other sites

Hi,

 

I need whole XML in return if 'Id' from variable 'ad' is not blank. same I need whole XML in return if 'Id' from variable 'addd' is not blank.

 

For that i have written function but getting

 

function ttt(ad,addd) {
var match = ad.match(/<Id\/>/);
var matcht = addd.match(/<Id\/>/);
if ((! match || match.length == 0) && (! matcht || matcht.length == 0)){
return "Below is the details of the Id of ad:\n\n" + ad.split("<Id>")[1].split("</Id>")[0]; + "\n\n And, Below is the details of the Id of addd:\n\n" +addd.split("<Id>")[1].split("</Id>")[0];
}
}
Link to comment
Share on other sites

I saw the function the first time you posted it, but I'm not sure what you're trying to do. You need the "whole XML", so the complete XML document? If the XML contains an ID, then you want to return all of the XML? That would look something like this:

 

function checkXML(xml) {
  if (xml.match(/<Id>(.)+<\/Id>/) != null) {
    return xml;
  }
  else {
    return false;
  }
}
That will return the whole XML if the XML contains an ID that is not blank. If that's not what you're trying to do, then I don't understand what you're asking for.
Link to comment
Share on other sites

  • 2 weeks later...

Not, Working...I have customize my example:

 

I have 2 variable 'aassn' and 'addd' in xml format. my concern is if 'Idc' of 'aassn' is blank then dont need 'Idc' value in output and same if 'Idd' of 'addd' is blank then dont need in output. If, 'Idc' and 'Idd' contaning data then need both in output. where aassn=
<?xml version="1.0" encoding="UTF-8"?>
<info>
<name>
<Idc>81</Idc>
</name>
</info>
and addd=
<?xml version="1.0" encoding="UTF-8"?>
<info>
<name>
<Idd></Idd>
</name>
</info>
my code is
var ssn1 = aassn.match(/<Idc\/>/);
var acc1 = addd.match(/<Idd\/>/);
var tempaassn = String(aassn);
var tempacc = String(addd);
var star1 = "Hi,\n\nDetails";
if (typeof(ssn1) != 'undefined' && ssn1 != '' && String(ssn1) != '' && tempaassn.indexOf('</Idc>') > -1)
star1+= "\n\nBelow first data:\n\n" + aassn.split("<Idc>")[1].split( "</Idc>" )[0];
if (typeof(acc1) != 'undefined' && acc1 != '' && String(acc1) != '' && tempacc.indexOf('</Idd>') > -1)
star1+= "\n\nBelow secound data\n\n" + addd.split("<Idd>")[1].split( "</Idd>" )[0];
return star1;
But i am gettitng output as below while Idd of addd is blank:
Hi,
Details:
Below first data:
81
Below secound data:
But, i need
Hi,
Details:
Below first data:
81
Edited by nalinishrma1@gmail.com
Link to comment
Share on other sites

var ssn1 = aassn.match(/<Idc\/>/);
    var acc1 = addd.match(/<Idd\/>/);
Those aren't going to match anything, because the text "<Idc/>" does not appear in the XML. You're searching for regular text when you do that, and that text is not there. There is an Idc element, but that regular expression match isn't going to find it because you're searching for text that doesn't exist. The regular expression I suggested above would find the contents of a particular element, or a better way is to use your XML string to create an XML document that you can then search with the regular DOM methods like getElementsByTagName.

 

 if (typeof(ssn1) != 'undefined' && ssn1 != ''
Neither of those conditions will ever be true, there's no reason to test for them. String.match returns an array, or null. It will never return an undefined value and it will never return an empty string.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...