【问题标题】:Dom parser is not working- output shown is all nullDom 解析器不工作 - 显示的输出全部为空
【发布时间】:2015-05-14 08:31:51
【问题描述】:

我要解析这个名为“Weather.xml”的xml文件如下所示:

<?xml version ="1.0" encoding="UTF-8" ?> 
   <weather> 
       <forecast_information>
                                    <city data="Pittsford, NY" />
                                    <postal_code data="14534" /> 
                                    <forecast_date data="2015-03-12" /> 
                                    <unit_system data="US" />
                                  <condition data = "Mostly Cloudy" /> 
                                   <temp_f data ="42" /> 
                                   <wind_condition data="Wind: NW at 7 mph" /> 
                                    <day_of_week data="Sat" /> 
                                    <low data="32"/> 
                                   <high data = "45" />
                                   <condition data="Rain and Snow" />
          </forecast_information>
         <forecast_information> 
                                    <city data= "Rochester, NY" /> 
                                    <postal_code data="14623" /> 
                                   <forecast_date data= "2015-03-12" /> 
                                   <unit_system data="US" /> 
                                    <condition data="Partly Cloudy" /> 
                                    <temp_f data="40" />
                                    <wind_condition data="Wind: St at 3.5 mph"       />
                                     <day_of_week data="Mon" /> 
                                     <low data="30" /> 
                                     <high data="40" />
                                     <condition data="Bright and Sunny" />
            </forecast_information> 
   </weather> 

我已经通过以下方式为它编写了 DOM 解析器:

public class DomParserDemo {
 public static void main(String[] args){

  try { 
     File inputFile = new File("Weather.xml");
     DocumentBuilderFactory dbFactory 
        = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document doc = dBuilder.parse(inputFile);
     doc.getDocumentElement().normalize();
     System.out.println("Root element :" 
        + doc.getDocumentElement().getNodeName());
     NodeList nList = doc.getElementsByTagName("forecast_information");
     System.out.println("----------------------------");
     for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" 
           + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
           Element eElement = (Element) nNode;
           System.out.println("City : " + eElement.getAttribute("city"));

           System.out.println("Postal_Code : " + eElement.getElementsByTagName("postal_code").item(0).getTextContent());
           System.out.println("Forecast date : " + eElement.getElementsByTagName("forecast_date").item(0).getTextContent());
           System.out.println("Unit System : " + eElement .getElementsByTagName("unit_system") .item(0).getTextContent());
           System.out.println("Condition : " + eElement .getElementsByTagName("condition") .item(0).getTextContent());
           System.out.println("Wind Condition : " + eElement .getElementsByTagName("wind_condition") .item(0).getTextContent());
           System.out.println("Day of week : " + eElement .getElementsByTagName("day_of_week") .item(0).getTextContent());
           System.out.println("Low : " + eElement .getElementsByTagName("low") .item(0).getTextContent());
           System.out.println("High: " + eElement .getElementsByTagName("high") .item(0).getTextContent());




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

但是显示的输出如下,信息不是从我提供的.xml文件中提取出来的:

   Root element :weather
   ----------------------------

   Current Element :forecast_information
   City : 
   Postal_Code : 
   Forecast date : 
   Unit System : 
   Condition : 
   Wind Condition : 
   Day of week : 
   Low : 
   High: 

   Current Element :forecast_information
   City : 
   Postal_Code : 
   Forecast date : 
   Unit System : 
   Condition : 
   Wind Condition : 
   Day of week : 
   Low : 
   High: 

【问题讨论】:

  • 您还可以执行以下操作... eElement.getElementsByTagName("postal_code").item(0).getAttributes().getNamedItem("data")

标签: java xml parsing dom


【解决方案1】:

第一个错误:

System.out.println("City : " + eElement.getAttribute("city"));

您正在请求名为city 的元素的属性,但forecast_information 元素没有任何属性。 cityforecast_information子元素,因此您应该通过遍历子元素或使用getElementsByTagName() 来提取它,就像处理其余项目一样。

第二个错误:

对于所有其他元素,您正在执行getElementsByTagName,它可以很好地检索元素本身。但是,您要检索的 data 项目不是它们的内容 - 它是一个属性。

如果项目如下所示,则使用 getTextContent() 是正确的:

<forecast_date>2015-03-12</forecast_date> 
<unit_system>US</unit_system>

但这不是您的 XML 的格式。在您的 XML 中,您需要检索属性 data。所以你应该替换如下调用:

System.out.println("Postal_Code : " + eElement.getElementsByTagName("postal_code").item(0).getTextContent());

收件人:

System.out.println("Postal_Code : " + eElement.getElementsByTagName("postal_code").item(0).getAttribute("data"));

【讨论】:

    【解决方案2】:

    在您的所有标签中,都没有内容。 data 是一个属性,因此您需要使用eElement.getAttribute("data"); 获取该属性的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2015-07-25
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多