Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用javascript读取xml值_Javascript_Jquery_Xml_Jqplot - Fatal编程技术网

用javascript读取xml值

用javascript读取xml值,javascript,jquery,xml,jqplot,Javascript,Jquery,Xml,Jqplot,我正试图做一个小项目的基础上 无论如何,我需要从xml文件中读取一个值,并通过javascript将其打印到网页中。问题是,当我将值放入变量时,它似乎不是一个有效的数字。这是我的密码: index.html <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <script language="javascript" type="te

我正试图做一个小项目的基础上

无论如何,我需要从xml文件中读取一个值,并通过javascript将其打印到网页中。问题是,当我将值放入变量时,它似乎不是一个有效的数字。这是我的密码:

index.html

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<script language="javascript" type="text/javascript" src="jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="jquery.jqplot.min.js"></script>
<script type="text/javascript" src="mchp.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.jqplot.css" />
</head>
<body>

<div id="chartdiv" style="height:400px;width:600px;" align="center"></div>
<script type="text/javascript">
var x=getXMLValue('status.xml','key');
var y=parseFloat(x);

$.jqplot('chartdiv',  [[[-24, y],[-20,5.12],[-17,13.1],[-15,33.6],[-7,85.9],[-2,219.9]]],
    {
        axes:{
            xaxis:{min:-24, max:-1},
            yaxis:{min:-24, max:50+2}
        },
        grid: {
            background: 'rgba(00,00,00,0.0)',
            drawBorder: false,
            shadow: false,
            gridLineColor: '#FFFFFF',
            gridLineWidth: 2},
        title: 'Temperature',
        animate : true,
        showTicks: true,   
        series: [{color: '#FF0000'}],
    }
    );

</script>
</body>
</html>

我认为jquery和jqplot文件在这种情况下并不重要,但如果需要,我可以提供链接。我认为问题在于读取或转换xml数据。提前感谢您的帮助

您正在解析的不是xml,而是字符串“status.xml”。

问题在于您以错误的方式调用getXMLValue。getXMLValue需要一个已解析的xml文档,而不是带有url/文件名的字符串。您需要调用返回status.xml的url。例如,由于您使用的是jQuery,因此可以执行以下操作:

jQuery.ajax({
  type : "GET",
  url : "http://localhost/status.xml",
  dataType : "xml",
  success : function(xml) {
    var y = parseFloat(getXMLValue(xml,key));
    jQuery.jqplot('chartdiv',  [[[-24, y],[-20,5.12],[-17,13.1],[-15,33.6],[-7,85.9],[-2,219.9]]],{
        axes:{
        xaxis:{min:-24, max:-1},
            yaxis:{min:-24, max:50+2}
        },
        grid: {
            background: 'rgba(00,00,00,0.0)',
            drawBorder: false,
            shadow: false,
            gridLineColor: '#FFFFFF',
            gridLineWidth: 2},
        title: 'Temperature',
        animate : true,
        showTicks: true,   
        series: [{color: '#FF0000'}],
    });
    },
    error : function(xhr) {
        alert(xhr.responseText);
    }
});
这至少是我发现的一个问题。您的代码可能还有一些其他问题

// Determines when a request is considered "timed out"
var timeOutMS = 5000; //ms

// Stores a queue of AJAX events to process
var ajaxList = new Array();

// Initiates a new AJAX command
//  url: the url to access
//  container: the document ID to fill, or a function to call with response XML (optional)
//  repeat: true to repeat this call indefinitely (optional)
//  data: an URL encoded string to be submitted as POST data (optional)
function newAJAXCommand(url, container, repeat, data)
{
    // Set up our object
    var newAjax = new Object();
    var theTimer = new Date();
    newAjax.url = url;
    newAjax.container = container;
    newAjax.repeat = repeat;
    newAjax.ajaxReq = null;

    // Create and send the request
    if(window.XMLHttpRequest) {
        newAjax.ajaxReq = new XMLHttpRequest();
        newAjax.ajaxReq.open((data==null)?"GET":"POST", newAjax.url, true);
        newAjax.ajaxReq.send(data);
    // If we're using IE6 style (maybe 5.5 compatible too)
    } else if(window.ActiveXObject) {
        newAjax.ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
        if(newAjax.ajaxReq) {
            newAjax.ajaxReq.open((data==null)?"GET":"POST", newAjax.url, true);
            newAjax.ajaxReq.send(data);
        }
    }

    newAjax.lastCalled = theTimer.getTime();

    // Store in our array
    ajaxList.push(newAjax);
}

// Loops over all pending AJAX events to determine if any action is required
function pollAJAX() {   
    var curAjax = new Object();
    var theTimer = new Date();
    var elapsed;

    // Read off the ajaxList objects one by one
    for(i = ajaxList.length; i > 0; i--)
    {
        curAjax = ajaxList.shift();
        if(!curAjax)
            continue;
        elapsed = theTimer.getTime() - curAjax.lastCalled;

        // If we suceeded
        if(curAjax.ajaxReq.readyState == 4 && curAjax.ajaxReq.status == 200) {
            // If it has a container, write the result
            if(typeof(curAjax.container) == 'function'){
                curAjax.container(curAjax.ajaxReq.responseXML.documentElement);
            } else if(typeof(curAjax.container) == 'string') {
                document.getElementById(curAjax.container).innerHTML = curAjax.ajaxReq.responseText;
            } // (otherwise do nothing for null values)

            curAjax.ajaxReq.abort();
            curAjax.ajaxReq = null;

            // If it's a repeatable request, then do so
            if(curAjax.repeat)
                newAJAXCommand(curAjax.url, curAjax.container, curAjax.repeat);
            continue;
        }

        // If we've waited over 1 second, then we timed out
        if(elapsed > timeOutMS) {
            // Invoke the user function with null input
            if(typeof(curAjax.container) == 'function'){
                curAjax.container(null);
            } else {
                // Alert the user
                alert("Command failed.\nConnection to development board was lost.");
            }

            curAjax.ajaxReq.abort();
            curAjax.ajaxReq = null;

            // If it's a repeatable request, then do so
            if(curAjax.repeat)
                newAJAXCommand(curAjax.url, curAjax.container, curAjax.repeat);
            continue;
        }

        // Otherwise, just keep waiting
        ajaxList.push(curAjax);
    }

    // Call ourselves again in 100ms
    setTimeout("pollAJAX()",100);

}

// Parses the xmlResponse returned by an XMLHTTPRequest object
//  xmlData: the xmlData returned
//  field: the field to search for
function getXMLValue(xmlData, field) {
    try {
        if(xmlData.getElementsByTagName(field)[0].firstChild.nodeValue)
            return xmlData.getElementsByTagName(field)[0].firstChild.nodeValue;
        else
            return null;
    } catch(err) { return null; }
}

//kick off the AJAX Updater
setTimeout("pollAJAX()",500);
jQuery.ajax({
  type : "GET",
  url : "http://localhost/status.xml",
  dataType : "xml",
  success : function(xml) {
    var y = parseFloat(getXMLValue(xml,key));
    jQuery.jqplot('chartdiv',  [[[-24, y],[-20,5.12],[-17,13.1],[-15,33.6],[-7,85.9],[-2,219.9]]],{
        axes:{
        xaxis:{min:-24, max:-1},
            yaxis:{min:-24, max:50+2}
        },
        grid: {
            background: 'rgba(00,00,00,0.0)',
            drawBorder: false,
            shadow: false,
            gridLineColor: '#FFFFFF',
            gridLineWidth: 2},
        title: 'Temperature',
        animate : true,
        showTicks: true,   
        series: [{color: '#FF0000'}],
    });
    },
    error : function(xhr) {
        alert(xhr.responseText);
    }
});