Link to home
Start Free TrialLog in
Avatar of caoimhincryan
caoimhincryan

asked on

Ampersand & issues Javascript

Hi all,

This question is a follow on as such from a previous question I asked: https://www.experts-exchange.com/questions/23292957/Read-from-xml-file.html

I am returning a string but sometime there is an ampersand(&) in the string returned from the xml file and it throws an error for me.

result = objXML.selectSingleNode("//configuration/component[@name='AnotherComponentLinks']/item[@name='"+v+"']").getAttribute("value");

Any one got any ideas?

The workaround I have is to replace '&' with 'AMPERSAND' in the xml file and then do a .replace("AMPERSAND", "&") once i have got:
result = objXML.selectSingleNode("//configuration/component[@name='AnotherComponentLinks']/item[@name='"+v+"']").getAttribute("value");
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Try:
result = objXML.selectSingleNode("//configuration/component[@name='AnotherComponentLinks']/item[@name='"+ v.replace(/&/g, "&" ) +"']").getAttribute("value");
Avatar of caoimhincryan
caoimhincryan

ASKER

No that didnt work. You see, v that is passed is only the item name in the component in the XML file. Its the value i need to look at and stop it from throwing an error: See XML below. the value of v is "HRLINK".

Am i making sense?
<?xml version="1.0" encoding="utf-8" ?> 
<configuration>	
 
	<component name="PVHRLinks">
		<item name="HRLink" value="http://www.google.com/search?hl=en&q="/>
	</component>
 
</configuration>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Am i making sense?
Yes, but the error is because the XML file you created is invalid. You cannot have an ampersand by itself in XML. Meaning, it has to be encoded. You can encoded either as &#38; or &amp;

When you query/retrieve the value, you will get the expected & (instead of &#38;). This is similar/analogous to encoding a newline with \n. You encode tha newline with the two characters \n in a string, but the result on your screen ends up being an actual (invisible/non-printalbe) new line character.
That was the issue surely! Thanks alot.