gpt4 book ai didi

java - Dom 解析器不工作 - 显示的输出全部为空

转载 作者:行者123 更新时间:2023-11-30 03:25:56 26 4
gpt4 key购买 nike

我想解析这个名为“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 元素没有任何属性。 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"));

关于java - Dom 解析器不工作 - 显示的输出全部为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com