无法摆脱“变量”中的XML

时间:2015-01-05 14:44:42

标签: javascript xml

这涉及Intuit的Quickbase。我正在尝试使用Highcharts.JS并将数据通过XML加载到图表中。我目前遇到的问题是,在变量中,我有两个变量,不需要在图表上完全与其他东西相关。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <title>CoT</title>



                <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
                <script type="text/javascript" src="https://code.highcharts.com/highcharts.js"></script>


                <!-- JavaScript to initialize the chart on document ready -->
                <script type="text/javascript">
                $(document).ready(function() {


                        var options = {
                                chart: {
                                        renderTo: 'container',
                                        type: 'column'
                                },
                                title: {
                                        text: 'Components Over Time'
                                },
                                xAxis: {
                                        categories: []
                                },
                                yAxis: {
                                        title: {
                                                text: 'Units'
                                        }
                                },
                                series: []
                        };

                        // Load the data from the XML file
                        $.get('https://dbname.quickbase.com/db/?apptoken=&act=API_DoQuery&query=%7B14.EX.%27_FID_9%7D&clist=7.24.25.26.27.28.29.30.31.32.33.34.35.36.37', function(xml) {

                                // Split the lines
                                var $xml = $(xml);

                                // push categories
                                $xml.find('variables').each(function(i, variables) {
                                        options.xAxis.categories.push($(variables).text());

                                });

                                // push series
                                $xml.find('record').each(function(i, record) {
                                        var seriesOptions = {
                                                name: $(record).find('methane').text(),
                                                data: [],
                                        };

                                        // push data points
                                        $(record).find('methane').each(function(i, point) {
                                                seriesOptions.data.push(
                                                        parseInt($(point).text())
                                                );
                                        });

                                        // add it to the options
                                        options.series.push(seriesOptions);
                                });
                                var chart = new Highcharts.Chart(options);
                        });


                });
                </script>

        </head>
        <body>

                <!-- 3. Add the container -->
                <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>


        </body>
</html>

以下内容也在推送类别中尝试过,但没有成功

$xml.find('variables:not(iol):not(__iol)').each(function(i, variables) {
  options.xAxis.categories.push($(variables).text());
 });

两者都会使图形看起来像(http://i.imgur.com/fb8A2uO.jpg

编辑:这是XML输出

<?xml version="1.0" ?>
<qdbapi>
        <action>API_DoQuery</action>
        <errcode>0</errcode>
        <errtext>No error</errtext>
<dbinfo>
<name>RESULT</name>
<desc></desc>
</dbinfo>
<variables>
<__iol>&amp;rand=&#039;+new Date().getTime())};&quot;&gt;</__iol>
<iol>&lt;img qbu=&#039;module&#039; src=&#039;/i/clear2x2.gif&#039; onload=&quot;javascript:if(typeof QBU==&#039;undefined&#039;){QBU={};$.getScript(gReqAppDBID+&#039;?a=dbpage&amp;pagename=</iol>
</variables>
<chdbids>
</chdbids>
  <record>
    <sample_date>1386892800000</sample_date>
    <hydrogen>0.002</hydrogen>
    <helium>0.114</helium>
    <oxygen/>
    <hydrogen_sulfide/>
    <nitrogen>1.926</nitrogen>
    <co2>0.454</co2>
    <methane>82.163</methane>
    <ethane>6.353</ethane>
    <propane>4.760</propane>
    <iso_butane>0.618</iso_butane>
    <n_butane>1.819</n_butane>
    <iso_pentane>0.491</iso_pentane>
    <n_pentane>0.544</n_pentane>
    <hexanes_>0.756</hexanes_>
    <update_id>1408654196361</update_id>
  </record>
  <record>
    <sample_date>1383782400000</sample_date>
    <hydrogen>0.006</hydrogen>
    <helium>0.038</helium>
    <oxygen/>
    <hydrogen_sulfide/>
    <nitrogen>0.512</nitrogen>
    <co2>0.844</co2>
    <methane>83.178</methane>
    <ethane>8.678</ethane>
    <propane>3.631</propane>
    <iso_butane>0.493</iso_butane>
    <n_butane>1.097</n_butane>
    <iso_pentane>0.342</iso_pentane>
    <n_pentane>0.371</n_pentane>
    <hexanes_>0.810</hexanes_>
    <update_id>1408981434690</update_id>
  </record>
  <record>
    <sample_date>1369699200000</sample_date>
    <hydrogen>0.004</hydrogen>
    <helium>0.060</helium>
    <oxygen/>
    <hydrogen_sulfide/>
    <nitrogen>1.684</nitrogen>
    <co2>0.443</co2>
    <methane>77.742</methane>
    <ethane>10.430</ethane>
    <propane>6.842</propane>
    <iso_butane>0.587</iso_butane>
    <n_butane>1.482</n_butane>
    <iso_pentane>0.232</iso_pentane>
    <n_pentane>0.249</n_pentane>
    <hexanes_>0.245</hexanes_>
    <update_id>1408981112624</update_id>
  </record>
</qdbapi>

1 个答案:

答案 0 :(得分:0)

尝试

   // note the space character between tags ,
   // to prevent rendering `<variables />` within xml
   $xml.find("variables").replaceWith("<variables> </variables>"); 

http://jsfiddle.net/xwzox6rg/