解析显示的文件输入的DOM不是有序的

时间:2015-05-14 10:04:55

标签: java xml parsing dom

我现在有以下xml文件,名为" Weather 2.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" />
        </forecast_information>
        <current_conditions> 
                                    <condition data = "Mostly Cloudy" /> 
                                   <temp_f data ="42" /> 
                                   <wind_condition data="Wind: NW at 7 mph" /> 
        </current_conditions> 
        <forecast_conditions>
                                    <day_of_week data="Sat" /> 
                                    <low data="32"/> 
                                   <high data = "45" />
                                   <condition data="Rain and Snow" />
         </forecast_conditions>
         <forecast_information> 
                                    <city data= "Rochester, NY" /> 
                                    <postal_code data="14623" /> 
                                   <forecast_date data= "2015-03-12" /> 
                                   <unit_system data="US" /> 
         </forecast_information>
          <current_conditions> 
                                    <condition data="Partly Cloudy" /> 
                                    <temp_f data="40" />
                                    <wind_condition data="Wind: St at 3.5 mph" />
           </current_conditions> 
           <forecast_conditions>
                                     <day_of_week data="Mon" /> 
                                     <low data="30" /> 
                                     <high data="40" />
                                     <condition data="Bright and Sunny" />
            </forecast_conditions> 
   </weather> 

我已按照以下方式更改了我的程序:

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

  try { 
     File inputFile = new File("Weather 2.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.getElementsByTagName("city").item(0).getAttributes().getNamedItem("data"));
           System.out.println("Postal_Code : " + eElement.getElementsByTagName("postal_code").item(0).getAttributes().getNamedItem("data"));
           System.out.println("Forecast date : " + eElement.getElementsByTagName("forecast_date").item(0).getAttributes().getNamedItem("data"));
           System.out.println("Unit System : " + eElement .getElementsByTagName("unit_system") .item(0).getAttributes().getNamedItem("data"));
           }

        NodeList nList1 = doc.getElementsByTagName("current_conditions");
        System.out.println("----------------------------");
        for (temp = 0; temp < nList1.getLength(); temp++) {
           Node nNode1 = nList1.item(temp);
           System.out.println("\nCurrent Element :" 
              + nNode1.getNodeName());
           if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
               Element eElement1 = (Element) nNode1;
           System.out.println("Condition : " + eElement1 .getElementsByTagName("condition") .item(0).getAttributes().getNamedItem("data"));
           System.out.println("Temperature : " + eElement1 .getElementsByTagName("temp_f").item(0) .getAttributes().getNamedItem("data"));

           System.out.println("Wind Condition : " + eElement1 .getElementsByTagName("wind_condition").item(0) .getAttributes().getNamedItem("data"));

           }


           NodeList nList2 = doc.getElementsByTagName("forecast_conditions");
           System.out.println("----------------------------");
           for (temp = 0; temp < nList2.getLength(); temp++) {
              Node nNode2 = nList2.item(temp);
              System.out.println("\nCurrent Element :" 
                 + nNode2.getNodeName());
              if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
                 Element eElement2 = (Element) nNode2;
           System.out.println("Day of week : " + eElement2 .getElementsByTagName("day_of_week") .item(0).getAttributes().getNamedItem("data"));
           System.out.println("Low : " + eElement2 .getElementsByTagName("low") .item(0).getAttributes().getNamedItem("data"));
           System.out.println("High: " + eElement2 .getElementsByTagName("high") .item(0).getAttributes().getNamedItem("data"));
           System.out.println("Condition: " + eElement2 .getElementsByTagName("condition") .item(0).getAttributes().getNamedItem("data"));

           }

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

输出显示不在订单中&amp;一些领域缺失:

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

  Current Element :forecast_information
  City : data="Pittsford, NY"
  Postal_Code : data="14534"
  Forecast date : data="2015-03-12"
  Unit System : data="US"
  ----------------------------

  Current Element :current_conditions
  Condition : data="Mostly Cloudy"
  Temperature : data="42"
  Wind Condition : data="Wind: NW at 7 mph"
  ----------------------------

  Current Element :forecast_conditions
  Day of week : data="Sat"
  Low : data="32"
  High: data="45"
  Condition: data="Rain and Snow"

  Current Element :forecast_conditions
  Day of week : data="Mon"
  Low : data="30"
  High: data="40"
  Condition: data="Bright and Sunny"

我希望代码能够按顺序显示:预测信息,当前条件和预测条件;两个XML文件中有两个记录

1 个答案:

答案 0 :(得分:0)

未打印所有数据的原因:您在所有for循环中使用了int变量temp相同

详细信息:for循环int变量temp在每个循环forecast_information,current_conditions和forecast_conditions中初始化。当forecast_conditions的for循环执行两次时,int变量temp值为2,因此不会执行forecast_information和current_conditions的for循环。

解决方案: 为每个for循环分配变量。 嵌套for循环将导致多次打印current_conditions和forecast_conditions数据,因此您必须重新构建代码以避免嵌套for循环。