为什么返回null在这里?

时间:2016-10-08 01:36:42

标签: java xml jsp

我正在尝试创建一个基于xml代码可视化一些棒球统计数据的网站。这是xml代码:

<?xml version = "1.0" encoding = "utf-8"?>
  <phillies>
    <player>
      <name>Ryan Howard</name>
      <position>First Baseman</position>
      <bats>Left</bats>
      <throws>Left</throws>
      <year2004>
        <games>18</games>
        <pa>42</pa>
        <ab>39</ab>
        <hits>11</hits>
        <runs>9</runs>
        <doubles>5</doubles>
        <triples>0</triples>
        <hr>2</hr>
        <rbi>5</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>2</bb>
        <so>13</so>
        <ba>.282</ba>
      </year2004>
      <year2005>
        <games>88</games>
        <pa>348</pa>
        <ab>312</ab>
        <hits>90</hits>
        <runs>52</runs>
        <doubles>17</doubles>
        <triples>2</triples>
        <hr>22</hr>
        <rbi>63</rbi>
        <sb>0</sb>
        <cs>1</cs>
        <bb>33</bb>
        <so>100</so>
        <ba>.288</ba>
      </year2005>
      <year2006>
        <games>159</games>
        <pa>704</pa>
        <ab>581</ab>
        <hits>182</hits>
        <runs>104</runs>
        <doubles>25</doubles>
        <triples>1</triples>
        <hr>58</hr>
        <rbi>149</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>108</bb>
        <so>181</so>
        <ba>.313</ba>
      </year2006>
      <year2007>
        <games>144</games>
        <pa>648</pa>
        <ab>529</ab>
        <hits>142</hits>
        <runs>94</runs>
        <doubles>26</doubles>
        <triples>0</triples>
        <hr>47</hr>
        <rbi>136</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>107</bb>
        <so>199</so>
        <ba>.268</ba>
      </year2007>
      <year2008>
        <games>162</games>
        <pa>700</pa>
        <ab>610</ab>
        <hits>153</hits>
        <runs>105</runs>
        <doubles>26</doubles>
        <triples>4</triples>
        <hr>48</hr>
        <rbi>146</rbi>
        <sb>1</sb>
        <cs>1</cs>
        <bb>81</bb>
        <so>199</so>
        <ba>.251</ba>
      </year2008>
      <year2009>
        <games>160</games>
        <pa>703</pa>
        <ab>616</ab>
        <hits>172</hits>
        <runs>105</runs>
        <doubles>37</doubles>
        <triples>4</triples>
        <hr>45</hr>
        <rbi>141</rbi>
        <sb>8</sb>
        <cs>1</cs>
        <bb>75</bb>
        <so>186</so>
        <ba>.279</ba>
      </year2009>
      <year2010>
        <games>143</games>
        <pa>620</pa>
        <ab>550</ab>
        <hits>152</hits>
        <runs>87</runs>
        <doubles>23</doubles>
        <triples>5</triples>
        <hr>31</hr>
        <rbi>108</rbi>
        <sb>1</sb>
        <cs>1</cs>
        <bb>59</bb>
        <so>157</so>
        <ba>.276</ba>
      </year2010>
      <year2011>
        <games>152</games>
        <pa>644</pa>
        <ab>557</ab>
        <hits>141</hits>
        <runs>81</runs>
        <doubles>30</doubles>
        <triples>1</triples>
        <hr>33</hr>
        <rbi>116</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>75</bb>
        <so>172</so>
        <ba>.253</ba>
      </year2011>
      <year2012>
        <games>71</games>
        <pa>292</pa>
        <ab>260</ab>
        <hits>57</hits>
        <runs>28</runs>
        <doubles>11</doubles>
        <triples>0</triples>
        <hr>14</hr>
        <rbi>56</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>25</bb>
        <so>99</so>
        <ba>.219</ba>
      </year2012>
      <year2013>
        <games>80</games>
        <pa>317</pa>
        <ab>286</ab>
        <hits>76</hits>
        <runs>34</runs>
        <doubles>20</doubles>
        <triples>2</triples>
        <hr>11</hr>
        <rbi>43</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>23</bb>
        <so>95</so>
        <ba>.266</ba>
      </year2013>
      <year2014>
        <games>153</games>
        <pa>648</pa>
        <ab>569</ab>
        <hits>127</hits>
        <runs>65</runs>
        <doubles>18</doubles>
        <triples>1</triples>
        <hr>23</hr>
        <rbi>95</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>67</bb>
        <so>190</so>
        <ba>.223</ba>
      </year2014>
      <year2015>
        <games>129</games>
        <pa>503</pa>
        <ab>467</ab>
        <hits>107</hits>
        <runs>53</runs>
        <doubles>29</doubles>
        <triples>1</triples>
        <hr>23</hr>
        <rbi>77</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>27</bb>
        <so>138</so>
        <ba>.229</ba>
      </year2015>
      <year2016>
        <games>112</games>
        <pa>362</pa>
        <ab>331</ab>
        <hits>65</hits>
        <runs>35</runs>
        <doubles>10</doubles>
        <triples>0</triples>
        <hr>25</hr>
        <rbi>59</rbi>
        <sb>0</sb>
        <cs>1</cs>
        <bb>27</bb>
        <so>138</so>
        <ba>.196</ba>
      </year2016>
    </player>

    <player>
      <name>Maikel Franco</name>
      <position>Third Baseman</position>
      <bats>Right</bats>
      <throws>Right</throws>
      <year2014>
        <games>16</games>
        <pa>58</pa>
        <ab>56</ab>
        <runs>5</runs>
        <hits>10</hits>
        <doubles>2</doubles>
        <triples>0</triples>
        <hr>0</hr>
        <rbi>5</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>1</bb>
        <so>13</so>
        <ba>.179</ba>
      </year2014>
      <year2015>
        <games>80</games>
        <pa>335</pa>
        <ab>304</ab>
        <runs>45</runs>
        <hits>85</hits>
        <doubles>22</doubles>
        <triples>1</triples>
        <hr>14</hr>
        <rbi>50</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>26</bb>
        <so>52</so>
        <ba>.280</ba>
      </year2015>
      <year2016>
        <games>152</games>
        <pa>630</pa>
        <ab>581</ab>
        <runs>67</runs>
        <hits>148</hits>
        <doubles>23</doubles>
        <triples>1</triples>
        <hr>25</hr>
        <rbi>88</rbi>
        <sb>1</sb>
        <cs>1</cs>
        <bb>40</bb>
        <so>106</so>
        <ba>.255</ba>
      </year2016>
    </player>

    <player>
      <name>Cameron Rupp</name>
      <position>Catcher</position>
      <bats>Right</bats>
      <throws>Right</throws>
      <year2013>
        <games>4</games>
        <pa>14</pa>
        <ab>13</ab>
        <runs>1</runs>
        <hits>4</hits>
        <doubles>1</doubles>
        <triples>0</triples>
        <hr>0</hr>
        <rbi>2</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>1</bb>
        <so>4</so>
        <ba>.308</ba>
      </year2013>
      <year2014>
        <games>18</games>
        <pa>64</pa>
        <ab>60</ab>
        <runs>4</runs>
        <hits>11</hits>
        <doubles>4</doubles>
        <triples>0</triples>
        <hr>0</hr>
        <rbi>6</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>4</bb>
        <so>20</so>
        <ba>.183</ba>
      </year2014>
      <year2015>
        <games>81</games>
        <pa>299</pa>
        <ab>270</ab>
        <runs>24</runs>
        <hits>63</hits>
        <doubles>9</doubles>
        <triples>1</triples>
        <hr>9</hr>
        <rbi>28</rbi>
        <sb>0</sb>
        <cs>1</cs>
        <bb>24</bb>
        <so>71</so>
        <ba>.233</ba>
      </year2015>
      <year2016>
        <games>105</games>
        <pa>419</pa>
        <ab>389</ab>
        <runs>36</runs>
        <hits>98</hits>
        <doubles>26</doubles>
        <triples>1</triples>
        <hr>16</hr>
        <rbi>54</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>24</bb>
        <so>114</so>
        <ba>.252</ba>
      </year2016>
    </player>

    <player>
      <name>Tommy Joseph</name>
      <position>First Baseman</position>
      <bats>Right</bats>
      <throws>Right</throws>
      <year2016>
        <games>107</games>
        <pa>347</pa>
        <ab>315</ab>
        <runs>47</runs>
        <hits>81</hits>
        <doubles>15</doubles>
        <triples>0</triples>
        <hr>21</hr>
        <rbi>47</rbi>
        <sb>1</sb>
        <cs>1</cs>
        <bb>22</bb>
        <so>75</so>
        <ba>.257</ba>
      </year2016>
    </player>

    <player>
      <name>Cesar Hernandez</name>
      <position>Second Baseman</position>
      <bats>Switch</bats>
      <throws>Right</throws>
      <year2013>
        <games>34</games>
        <pa>131</pa>
        <ab>121</ab>
        <runs>17</runs>
        <hits>35</hits>
        <doubles>5</doubles>
        <triples>0</triples>
        <hr>0</hr>
        <rbi>10</rbi>
        <sb>0</sb>
        <cs>3</cs>
        <bb>9</bb>
        <so>26</so>
        <ba>.289</ba>
      </year2013>
      <year2014>
        <games>66</games>
        <pa>125</pa>
        <ab>114</ab>
        <runs>13</runs>
        <hits>27</hits>
        <doubles>2</doubles>
        <triples>0</triples>
        <hr>1</hr>
        <rbi>4</rbi>
        <sb>1</sb>
        <cs>1</cs>
        <bb>9</bb>
        <so>33</so>
        <ba>.237</ba>
      </year2014>
      <year2015>
        <games>127</games>
        <pa>452</pa>
        <ab>405</ab>
        <runs>57</runs>
        <hits>110</hits>
        <doubles>20</doubles>
        <triples>4</triples>
        <hr>1</hr>
        <rbi>35</rbi>
        <sb>19</sb>
        <cs>5</cs>
        <bb>40</bb>
        <so>86</so>
        <ba>.274</ba>
      </year2015>
      <year2016>
        <games>155</games>
        <pa>622</pa>
        <ab>547</ab>
        <runs>67</runs>
        <hits>161</hits>
        <doubles>14</doubles>
        <triples>11</triples>
        <hr>6</hr>
        <rbi>39</rbi>
        <sb>17</sb>
        <cs>13</cs>
        <bb>66</bb>
        <so>116</so>
        <ba>.294</ba>
      </year2016>
    </player>

    <player>
      <name>Freddy Galvis</name>
      <position>Shortstop</position>
      <bats>Switch</bats>
      <throws>Right</throws>
      <year2012>
        <games>58</games>
        <pa>200</pa>
        <ab>190</ab>
        <runs>14</runs>
        <hits>43</hits>
        <doubles>15</doubles>
        <triples>1</triples>
        <hr>3</hr>
        <rbi>24</rbi>
        <sb>0</sb>
        <cs>0</cs>
        <bb>7</bb>
        <so>29</so>
        <ba>.229</ba>
      </year2012>
      <year2013>
        <games>70</games>
        <pa>222</pa>
        <ab>205</ab>
        <runs>13</runs>
        <hits>48</hits>
        <doubles>5</doubles>
        <triples>4</triples>
        <hr>6</hr>
        <rbi>19</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>13</bb>
        <so>45</so>
        <ba>.234</ba>
      </year2013>
      <year2014>
        <games>43</games>
        <pa>128</pa>
        <ab>119</ab>
        <runs>14</runs>
        <hits>21</hits>
        <doubles>3</doubles>
        <triples>1</triples>
        <hr>4</hr>
        <rbi>12</rbi>
        <sb>1</sb>
        <cs>0</cs>
        <bb>8</bb>
        <so>30</so>
        <ba>.176</ba>
      </year2014>
      <year2015>
        <games>151</games>
        <pa>603</pa>
        <ab>559</ab>
        <runs>63></runs>
        <hits>147</hits>
        <doubles>14</doubles>
        <triples>5</triples>
        <hr>7</hr>
        <rbi>50</rbi>
        <sb>10</sb>
        <cs>1</cs>
        <bb>30</bb>
        <so>103</so>
        <ba>.241</ba>
      </year2015>
      <year2016>
        <games>158</games>
        <pa>624</pa>
        <ab>584</ab>
        <runs>61</runs>
        <hits>141</hits>
        <doubles>26</doubles>
        <triples>3</triples>
        <hr>20</hr>
        <rbi>67</rbi>
        <sb>17</sb>
        <cs>6</cs>
        <bb>25</bb>
        <so>136</so>
        <ba>.241</ba>
      </year2016>
    </player>
  </phillies>

这是我用于解析HTML的java类:

package com.grim.baseballvisualizer;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLParser {

    public static NodeList parseXML(String file) {

        File xmlFile = new File("howard.xml");
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(xmlFile);
            NodeList names = document.getElementsByTagName("name");
            return names;
        } 
        catch (ParserConfigurationException e) {

            e.printStackTrace();
        }
        catch (SAXException e) {

            e.printStackTrace();
        } 
        catch (IOException e) {

            e.printStackTrace();
        }
        return null;



    }

}

最后,我有一个jsp程序来调用parseXML函数,然后我想编写剩下的代码来实际可视化数据。但是,我遇到的问题是,当我返回“名称”时,我得到null,即使xml文件中有多个“name”实例。

2 个答案:

答案 0 :(得分:1)

当我修改代码以使其可运行并在您的示例输入文件上运行时,它可以正常工作。它返回一个非空NodeList

com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@7852e922

这告诉我你的案例中实际发生的是以下一个或多个:

  1. 您实际执行的代码 与您向我们展示的代码明显不同。

  2. 输入文件不同。

  3. 您实际上正在获取堆栈跟踪,但是标准输出正在被丢弃,或者它被定向到您未查看/的某处(例如日志文件)。

  4. 我的钱将在3日。

    如果您遇到问题,请尝试使用解析代码,将其拆分为可以使用java命令直接运行的代码,然后查看所获得的内容。

答案 1 :(得分:0)

您的代码中没有任何问题。有一些小的测试代码可以证明节点列表符合你的需要。

private static void testNodeList(NodeList names){
    for(int i = 0; i < names.getLength(); i++){
        System.out.println(names.item(i).getTextContent());
    }
}