【发布时间】: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")