Reputation: 27
I'm trying to parse a dynamic xml file with javascript . But the url inside the <link>
element contains the ambersant symbol ( & ).
So i have to create a function to replace dynamically the symbol ' &
' with ' &
'.
Do you have any ideas how i'm gonna do that?
Here's is the whole html file
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>metar.gr</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="util.js"></script>
<script src="js/libs/require.js" data-main="js/mobile"></script>
<link rel="stylesheet" href="https://d10ajoocuyu32n.cloudfront.net/mobile/1.3.1/jquery.mobile-1.3.1.min.css"/>
<link rel="stylesheet" href="fonts-and-colors.css" type="text/css" media="screen">
<script type="text/javascript">
var infowindow;
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(38.822590,24.653320);
var myOptions = {
zoom: 6,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
downloadUrl("moredata.xml", function(doc) {
var items = doc.documentElement.getElementsByTagName("item");
for (var i = 0; i < items.length; i++) {
var description = items[i].getElementsByTagName("description")[0].textContent;
var temp = items[i].getElementsByTagName("temp")[0].textContent;
var title = items[i].getElementsByTagName("title")[0].textContent;
var link = items[i].getElementsByTagName("link")[0].textContent;
var windSpeed = items[i].getElementsByTagName("windSpeed")[0].textContent;
var dailyRain = items[i].getElementsByTagName("dailyRain")[0].textContent;
var latlng = new google.maps.LatLng(parseFloat(items[i].getElementsByTagName("glat")[0].textContent),
parseFloat(items[i].getElementsByTagName("glon")[0].textContent));
if ( temp <= "1 °C" ) {
//alert(temp);
var marker = createMarker( temp,latlng,cold );
}
if ( temp >= "38 °C" ) {
//alert(temp);
var marker2 = createMarker2( temp,latlng,hot );
}
}
});
}
var cold = 'weather_icons/pagetos2.png';
var hot = 'weather_icons/hot.png';
function createMarker( temp,latlng,cold ) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
icon: cold
});
google.maps.event.addListener(marker, "click", function() {
if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({
content: temp
});
infowindow.open(map, marker);
});
return marker;
}
function createMarker2( temp,latlng,hot ) {
var marker2 = new google.maps.Marker({
position: latlng,
map: map,
icon: hot
});
google.maps.event.addListener(marker2, "click", function() {
if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({
content: temp
});
infowindow.open(map, marker2);
});
return marker2;
}
</script>
</head>
<body onload="initialize()">
<div data-role="header" data-theme="b">
<div align="center">
<img onclick='doAction("showStoreLocatorMap()")' src="http://www.metar.gr/templates/metar/images/metar.gif" alt="main logo" vspace="2"/>
</div>
<a href="main.html" data-role="button" data-inline="true">Home</a>
</div>
<div data-role="content">
<div id="map_canvas" ></div>
</div>
<div data-role="info_icons">
<ul data-role="listview" id="icon_info" data-theme="c">
<img src="weather_icons/pagetos2.png"/>
<h3 class="ui-li-heading">Cold</h3>
<p class="ui-li-desc">temprature < +4° C</p>
</li>
<li>
<img src="weather_icons/hot.png"/>
<h3 class="ui-li-heading">Hot</h3>
<p class="ui-li-desc">temprature > +38° C</p>
</li>
</ul>
</div>
<div data-role="footer" data-theme="b">
<h4>metar.gr mobile version</h4>
</div>
</body>
</html>
A sample of the xml file
<item>
<description>Thesaloniki</description>
<glat>40.422726139672626</glat>
<glon>22.93392777442932</glon>
<title>makedonia</title>
<temp>60 °C</temp>
<dailyRain>0 mm</dailyRain>
<windSpeed>3.1 km/hr</windSpeed>
<link>
http://www.metar.gr/index.php?option=com_jumi&fileid=12&Itemid=73&station=1475
</link>
</item>
<item>
<description>Giannena</description>
<glat>39.62209843837158</glat>
<glon>20.89027225971222</glon>
<title>ipiros</title>
<temp>-16.9°C</temp>
<dailyRain>0.0 mm</dailyRain>
<windSpeed>10 km/hr</windSpeed>
<link>
http://www.metar.gr/index.php?option=com_jumi&fileid=12&Itemid=73&station=1227
</link>
</item>
<item>
<description>Athina</description>
<glat>38.08469095792561</glat>
<glon>23.680233657360077</glon>
<title>sterea</title>
<temp>45°C</temp>
<dailyRain>0.0 mm</dailyRain>
<windSpeed>97 km/hr</windSpeed>
<link>
http://www.metar.gr/index.php?option=com_jumi&fileid=12&Itemid=73&station=1009
</link>
</item>
A part of the util.js file
function downloadUrl(url, callback) {
var status = -1;
var request = createXmlHttpRequest();
if (!request) {
return false;
}
request.onreadystatechange = function() {
if (request.readyState == 4) {
try {
status = request.status;
} catch (e) {
// Usually indicates request timed out in FF.
}
if (status == 200) {
callback(request.responseXML, request.status);
request.onreadystatechange = function() {};
}
}
}
request.open('GET', url, true);
try {
request.send(null);
} catch (e) {
changeStatus(e);
}
};
Upvotes: 0
Views: 3102
Reputation: 27
I found this answer into a similar question in stackoverflow and i quote it.I have to read the xml file into a String.replace
Have a look!
The file you are trying to read is not valid XML. No self-respecting XML parser will accept it.
I'm retrieving my XML dynamically from the web. What's the best way to replace all my escape
characters after fetching the Document object?
You are taking the wrong approach. The correct approach is to inform the people responsible for creating that file that it is invalid, and request that they fix it. Simply writing hacks to (try to) fix broken XML is not in your (or other peoples') long term interest.
If you decide to ignore this advice, then one approach is to read the file into a String, use String.replaceAll(regex, replacement) with a suitable regex to turn these bogus "&" characters into proper character entities ("&"), then give the fixed XML string to the XML parser. You need to carefully design the regex so that it doesn't break valid character entities as an unwanted side-effect. A second approach is to do the parsing and replacement by hand, using appropriate heuristics to distinguish the bogus "&" characters from well-formed character entities.
But this all costs you development and test time, and slows down your software. Worse, there is a significant risk that your code will be fragile as a result of your efforts to compensate for the bad input files. (And guess who will get the blame ...)
Upvotes: 1
Reputation: 163458
The "xml" you have shown us is not well-formed XML (that is, it's not XML at all). XML parsers are required to reject ill-formed XML. The correct approach is to find out what rogue application is generating this corrupt data, and fix the problem at source. Once people start sending (and accepting!) bad XML, the benefits of using a standard interchange format very rapidly evaporate.
If you can't do that, you'll need to write a repair tool. Because your data isn't XML, you can't use XML tools for this job; use something like Perl. The details will depend on what kind of errors you expect to be present in your XML, e.g. whether you just need to tackle unescaped ampersands, or also undeclared entity references such as
.
Upvotes: 3
Reputation: 336388
If you want to know how the function works which you already have in the comment above:
return unsafe.replace(/&/g, "&");
returns unsafe
, after replace
ing all the (g
lobal modifier) matches of the regular expression /&/
(which matches a literal ampersand) with "&"
.
Upvotes: 0