简体   繁体   English

如何将xml文件与多个节点和子节点链接?

[英]How do i link a xml file with several nodes and subnodes?

am looking for a javascript that will load an xml file and will display something like rectangules with links based on the xml child nodes, parents linked to childs and subnodes linked to nodes. 我正在寻找一个将加载xml文件并显示诸如带有基于xml子节点的链接,链接到子节点的父节点和链接到节点的子节点的矩形之类的JavaScript的JavaScript。 how would i be able to do this? 我将如何做到这一点?

You can accomplish this with AJAX. 您可以使用AJAX完成此操作。

First, create a function that will pull information from your XML file: 首先,创建一个将从XML文件中提取信息的函数:

function loadXMLdoc(url) {
  var ajaxRequest;
  try {
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } catch(e) {
    // Internet Explorer Browsers
    try {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        // Something went wrong
    alert("Your browser broke!");
    return false;
      }
    }
  }
  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function() {
    if (ajaxRequest.readyState === 4) {
  if (ajaxRequest.status === 200) {
        // a hidden div to display the result
    var result = document.getElementById('result');
    result.style.display = 'block';
    result.innerHTML = ajaxRequest.responseText;
  } else {
    result.innerHTML = 'An error has occurred making the request';
  }
    }
    ajaxRequest.open("GET", url, true);
    ajaxRequest.send();
  };
}

From here, you can start grabbing data from your XML file. 从这里,您可以开始从XML文件中获取数据。 Inside your if (ajaxRequest.status === 200) { } statement, you can start calling elements: if (ajaxRequest.status === 200) { }语句中,您可以开始调用元素:

var elem = ajaxRequest.responseXML.getElementById('elem');
var parents = elem.parentNodes;
var children = parents.childNodes;

It's up to you exactly how you'd like to format this and what data you're grabbing, but this should be a good starting point for you. 完全由您决定如何格式化以及要获取的数据,但这对您来说应该是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM