解析 xml 时,getAttribute 获取一些项目,而不获取其他项目
下面是 XML 的示例:
<markers><marker name="Faulkner State Community College" lat="30.853801" lng="-87.776692" type="PS" number="279" address="CFOT, CPCT" /></markers>
解析器正在获取名称、纬度、局域网和地址。它不选择类型和数量。类型和数字的输出未定义。我不需要将它们转换为字符串以外的任何内容。
这是我的代码:
var markerNodes = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var address = markerNodes[i].getAttribute("address");
var type = markerNodes[i].getAttribute("type");
var number = markerNodes[i].getAttribute("number");
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng")));
createMarker(latlng, name, address, type, number);
}
这是“createMarker”的代码,以防万一:
function createMarker(latlng, name, address, type, number) {
var html = "<font face=\"Arial\" size=\"2\"><b><a href=\"school.php?school_number=" + number + "\">" + name + "</a></b> <br/>Certifications: " + address + "<br/>School #: " + number + "</font>";
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: customIcon(type)
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
markers.push(marker);
}
Here is a sample of the XML:
<markers><marker name="Faulkner State Community College" lat="30.853801" lng="-87.776692" type="PS" number="279" address="CFOT, CPCT" /></markers>
The parser is picking up name, lat, lan, and address. It's not picking up type and number. The output for type and number is undefined. I don't need them converted to anything other than a string.
Here is my code:
var markerNodes = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var address = markerNodes[i].getAttribute("address");
var type = markerNodes[i].getAttribute("type");
var number = markerNodes[i].getAttribute("number");
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng")));
createMarker(latlng, name, address, type, number);
}
And here is the code for 'createMarker', just in case:
function createMarker(latlng, name, address, type, number) {
var html = "<font face=\"Arial\" size=\"2\"><b><a href=\"school.php?school_number=" + number + "\">" + name + "</a></b> <br/>Certifications: " + address + "<br/>School #: " + number + "</font>";
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: customIcon(type)
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
markers.push(marker);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有两组标记节点解析,并且正在编辑错误的组。
I had two sets of the markerNodes parsing and was editing the wrong set.