Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am facing one issue in XML Parsing in jquery.

Data saved in DB:
<eligibility>
<eligible id="GeneralManager&FinanceManager_CRM1140" membertype="Employee">
<name>GeneralManager&FinanceManager_CRM1140

<eligible id="Managers(exceptGeneralManager&FinanceManager)_CRM1140" membertype="Employee">
<name>Managers(exceptGeneralManager&FinanceManager)_CRM1140



At client side varialbe we are getting,
<eligibility>
<eligible id="GeneralManager&FinanceManager_CRM1140" membertype="Employee">
<name>GeneralManager&FinanceManager_CRM1140

<eligible id="Managers(exceptGeneralManager&FinanceManager)_CRM1140" membertype="Employee">
<name>Managers(exceptGeneralManager&FinanceManager)_CRM1140




But when we parse in jquery $.parseXml() at client side, it automatically convert backed to below format.
& is invalid xml as per xml rules.
<eligibility>
<eligible id="GeneralManager&FinanceManager_CRM1140" membertype="Employee">
<name>GeneralManager&FinanceManager_CRM1140

<eligible id="Managers(exceptGeneralManager&FinanceManager)_CRM1140" membertype="Employee">
<name>Managers(exceptGeneralManager&FinanceManager)_CRM1140



How can we resolve it?

What I have tried:

$(xmldata).find('Eligible').each(function (id, name) {
id = $(this).attr('ID');
name = $(this).children('Name').text();
alert(id + name);
//$(this).attr('ID', id.replace("&", "&"))
//$(benefitXml).find('Eligible').attr('ID', id.replace("&", "&")));
//$(this).find('Name').val(name.replace("&", "&"));
// do something with the id, name, and description
});

I want to find & for specific node and attribute, then replace & with & and parse using $.parseXML() without any error.
Posted
Updated 4-Jun-19 20:37pm

1 solution

Do a string replace to replace & with something that is supported (a valid char, a string) that doesn't appear anywhere else in your values, then parse the XML, and perform the "reverse replace" (i.e. replace the char/string you chose with & again). The "doesn't appear anywhere else in your values" condition is pretty important because otherwise the reverse replace might just change too many parts into &.

Though ideally, you start from valid XML instead of needing to use such tricks. If you have any control over the XML, I'd advise to just keep it valid from the start.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900