简体   繁体   English

textContent在迭代JavaScript XML中返回不需要的空文本值

[英]textContent returning unwanted empty text values in iteration JavaScript XML

I'm trying to pull the text value of the <point> nodes in my XML file widgets.xml . 我试图在XML文件widgets.xml提取<point>节点的文本值。

I am successful in getting the text, however I am also getting unwanted empty text values in between each iteration in my for loop. 我成功获取了文本,但是在for循环的每次迭代之间也获取了不需要的空文本值。 After careful debugging, it looks like the problem begins right around when lines[i].childNodes[j].textContent is called at the bottom of my JavaScript file. 经过仔细的调试后,似乎在我的JavaScript文件底部调用lines[i].childNodes[j].textContent时问题就开始了。

In my debugger in Chrome, it appears that the first value it holds at points[0][0] is a small arrow that goes down and to the left, similar to the one you find on your Return key on your keyboard. 在我的Chrome调试器中,似乎它在points[0][0]处保留的第一个值是一个向下和向左的小箭头,类似于您在键盘上的Return键上找到的那个箭头。

I have been toying around with it for a while, but I can't seem to figure out what's wrong. 我一直在玩弄它一段时间,但是我似乎无法弄清楚出了什么问题。

What exactly is textContent doing during the iteration? textContent在迭代过程中到底在做什么?

Here's the code: 这是代码:


XML XML格式

<?xml version="1.0" encoding="UTF-8"?>


<user>
    <widgets>
        <widget id="00" status="on">
            <name>Bookings</name>
            <phpfile>booking.php</phpfile>
        </widget>
        <widget id="01" status="on">
            <name>Room Nights</name>
            <phpfile>room_nights.php</phpfile>
        </widget>
        <widget id="02" status="on" currency_type="$">
            <name>Revenue</name>
            <phpfile>revenue.php</phpfile>
        </widget>
        <widget id="03" status="on">
            <name>Visits</name>
            <phpfile>visits.php</phpfile>
        </widget>
        <widget id="04" status="on">
            <name>Website Traffic</name>
            <phpfile>chart.php</phpfile>
            <dataset>
                <data>
                    <startdate>2014-10-01</startdate>
                    <enddate>2014-10-12</enddate>
                    <lines>
                        <line num="1" name="Booking Engine">
                            <point>2014-10-01, 132</point>
                            <point>2014-10-02, 123</point>
                            <point>2014-10-03, 171</point>
                            <point>2014-10-04, 99</point>
                            <point>2014-10-05, 72</point>
                            <point>2014-10-06, 85</point>
                            <point>2014-10-07, 102</point>
                            <point>2014-10-08, 107</point>
                            <point>2014-10-09, 124</point>
                            <point>2014-10-10, 132</point>
                            <point>2014-10-11, 136</point>
                            <point>2014-10-12, 131</point>
                        </line>
                        <line num="2" name="Revenue">
                            <point>2014-10-01, 123</point>
                            <point>2014-10-02, 99</point>
                            <point>2014-10-03, 65</point>
                            <point>2014-10-04, 201</point>
                            <point>2014-10-05, 123</point>
                            <point>2014-10-06, 156</point>
                            <point>2014-10-07, 123</point>
                            <point>2014-10-08, 76</point>
                            <point>2014-10-09, 45</point>
                            <point>2014-10-10, 123</point>
                            <point>2014-10-11, 190</point>
                            <point>2014-10-12, 143</point>
                        </line>
                    </lines>
                </data>
            </dataset>
        </widget>
        <widget id="05" status="on">
            <name>Tips</name>
            <phpfile>tips.php</phpfile>
        </widget>
        <widget id="06" status="on">
            <name>Recent Bookings</name>
            <phpfile>recent_bookings.php</phpfile>
        </widget>
        <widget id="07" status="on">
            <name>Timeline</name>
            <phpfile>timelines.php</phpfile>
        </widget>
        <widget id="08" status="on">
            <name>Employees</name>
            <phpfile>employees.php</phpfile>
        </widget>
        <widget id="09" status="on">
            <name>Daily Progress</name>
            <phpfile>daily_progress.php</phpfile>
        </widget>
    </widgets>
</user>

and the script: 和脚本:

JS JS

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            getXmlGraphData();

            //Gets the XML data from the widgets.xml file
            function getXmlGraphData() {
              var request = new XMLHttpRequest();
              var user;
              var lines = [];
              var points = [[]];

              request.open("GET", "widgets.xml", false);
              request.send();

              var xml = request.responseXML;
              var lines = xml.getElementsByTagName("line");

              for (var i = 0; i < lines.length; ++i) {
                if (!lines) {
                  lines = [];
                }

                for (var j = 0; j < lines[i].childNodes.length; ++j) {
                    if (!points[i]) {
                        points[i] = [];
                    }
                    points[i][j] = lines[i].childNodes[j].textContent; //the problem begins here
                }
              }

              document.write(points);
            }

        });
    </script>

Output 输出量

Notice the gaps between the values: 注意这些值之间的差距:

    ,2014-10-01, 132, ,2014-10-02, 123, ,2014-10-03, 171, ,2014-10-04, 99, ,2014-10-05, 72, ,2014-10-06, 85, ,2014-10-07, 102, ,2014-10-08, 107, ,2014-10-09, 124, ,2014-10-10, 132, ,2014-10-11, 136, ,2014-10-12, 131, , ,2014-10-01, 123, ,2014-10-02, 99, ,2014-10-03, 65, ,2014-10-04, 201, ,2014-10-05, 123, ,2014-10-06, 156, ,2014-10-07, 123, ,2014-10-08, 76, ,2014-10-09, 45, ,2014-10-10, 123, ,2014-10-11, 190, ,2014-10-12, 143,

If you solve it, thanks in advance. 如果您解决了该问题,请先谢谢。

use trim function to get rid of white spaces at begining and ending of a string. 使用trim函数可以消除字符串开头和结尾的空格。 Also check for null strings. 还要检查空字符串。

Eg: 例如:

var str=" text "
var trimmedText=str.trim()//returns "text"

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

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