解析 xml 时,getAttribute 获取一些项目,而不获取其他项目

发布于 2024-12-19 06:56:39 字数 1554 浏览 2 评论 0原文

下面是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-12-26 06:56:39

我有两组标记节点解析,并且正在编辑错误的组。

I had two sets of the markerNodes parsing and was editing the wrong set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文