我想解析这个名为“ 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:
第一个错误:
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"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句