I have a XML string which contains some special characters(<,>,&) in it and hence can not be parsed by using jQuery $.parseXML.
This is the sample XML string
<?xml version="1.0" encoding="UTF-8"?>
<BackgroundCheck userId="{Username}" password="{Password}">
<BackgroundSearchPackage action="submit" type="{PackageName}">
<ReferenceId>ab<</ReferenceId>
<UserArea>
<PositionDetail>
<EmploymentState>{StateJob}</EmploymentState>
<ProposedSalary>{AnnualSalary}</ProposedSalary>
</PositionDetail>
</UserArea>
<PersonalData>
<PersonName>
<GivenName>{FirstName}</GivenName>
<MiddleName>{MiddleName}</MiddleName>
<FamilyName>{LastName}</FamilyName>
<Affix>{Generation}</Affix>
</PersonName>
<EmailAddress>{Email}</EmailAddress>
<DemographicDetail>
<GovernmentId countryCode="US" issuingAuthority="SSN">{SSN}</GovernmentId>
<DateOfBirth>{DateOfBirth}</DateOfBirth>
</DemographicDetail>
{Aliases}
{PostalAddress}
</PersonalData>
<Screenings useConfigurationDefaults="no">
{Screenings}
<AdditionalItems type="x:interface">
<Text>{Search&Type}</Text>
</AdditionalItems>
<AdditionalItems type="x:return_xml_results">
<Text>yes</Text>
</AdditionalItems>
<AdditionalItems type="x:embed_credentials">
<Text>true</Text>
</AdditionalItems>
<AdditionalItems type="x:integration_type">
<Text>Sample XML</Text>
</AdditionalItems>
<AdditionalItems type="x:postback_url">
<Text>{CallbackURL}</Text>
</AdditionalItems>
{AdditionalItems}
</Screenings>
{Documentation}
</BackgroundSearchPackage>
</BackgroundCheck>
Note the value of tag ReferenceId on 4th line, it contains special character and hence this string can not be parsed to XML.
What I need is to replace those special characters with escape sequences(<,>,&). The closest I came across is this
how to escape xml entities in javascript?
But this answer assumes that we have XML node values already with us.
My requirements is different, I have the complete xml as a string and I want to replace only the node values without touching the tag names(tags also contain <,>).
This is what i tried using jQuery
$(xml).each(function() {
var t = $(this).wrap('<p/>').parent().html();
t.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
xml = t;
});
This is working fine, the only problem with this code is that it is converting the XML tags to lower case. I thing this is because of jQuery's behavior.
Please suggest be a fix/solution for this.Thanks