我想解析这个名为“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:
答案 0 :(得分:1)
在所有代码中,没有内容。 data
是一个属性,因此您需要使用eElement.getAttribute("data");
答案 1 :(得分:1)
第一个错误:
System.out.println("City : " + eElement.getAttribute("city"));
您要求的元素属性名为city
,但forecast_information
元素没有任何属性。 city
是forecast_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"));