我在解析 XML 时遇到问题。
我有以下方法:
public void componentsInfoToSimulation(String houseDivision, String equipment) {
Element root = doc.getRootElement();
pt.ipp.isep.gecad.masgrip.Simulation.getHouseNamesOfComponents().add(equipment);
for (Iterator i = root.elementIterator("house"); i.hasNext();) {
Element element = (Element) i.next();
if (element.attributeValue("id").equals(houseDivision)) {
for (Iterator j = element.elementIterator("" + houseDivision); j.hasNext();) {
Element innerElement = (Element) j.next();
if (innerElement.attributeValue("id").equals(equipment)) {
for (Iterator k = innerElement.elementIterator("" + equipment); k.hasNext();) {
Element innerSubElement = (Element) k.next();
String op = innerSubElement.getQualifiedName();
switch (op) {
case "power":
pt.ipp.isep.gecad.masgrip.Simulation.getHousePowerDosComponents().add(Integer.parseInt(innerSubElement.getTextTrim()));
break;
case "min_power":
pt.ipp.isep.gecad.masgrip.Simulation.getHouseMinPowerComponents().add(Integer.parseInt(innerSubElement.getTextTrim()));
break;
case "max_power":
pt.ipp.isep.gecad.masgrip.Simulation.getHouseMaxPowerComponents().add(Integer.parseInt(innerSubElement.getTextTrim()));
break;
case "average_usage_hour":
pt.ipp.isep.gecad.masgrip.Simulation.getHouseAverageUsageHoursPerDay().add(Float.parseFloat(innerSubElement.getTextTrim()));
break;
case "average_usage_minute":
pt.ipp.isep.gecad.masgrip.Simulation.getHouseAverageUsageMinutesPerDay().add(Float.parseFloat(innerSubElement.getTextTrim()));
break;
default:
break;
}
}
}
}
}
}
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHouseNamesOfComponents());
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHousePowerDosComponents());
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHouseMinPowerComponents());
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHouseMaxPowerComponents());
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHouseAverageUsageHoursPerDay());
System.out.println("" + pt.ipp.isep.gecad.masgrip.Simulation.getHouseAverageUsageMinutesPerDay());
}
这是我的 XML 文件的示例:
<components>
<house id="Kitchen">
<espresso_machine id="espresso machine">
<power>360</power>
<min_power>360</min_power>
<max_power>360</max_power>
<average_usage_hour>0.25</average_usage_hour>
<average_usage_minute>15</average_usage_minute>
</espresso_machine>
<coffee_machine id="coffee machine">
<power>1500</power>
<min_power>800</min_power>
<max_power>1500</max_power>
<average_usage_hour>0.25</average_usage_hour>
<average_usage_minute>15</average_usage_minute>
</coffee_machine>
<coffee_pot id="coffee pot">
<power>0</power>
<min_power>200</min_power>
<max_power>200</max_power>
<average_usage_hour>0.25</average_usage_hour>
<average_usage_minute>15</average_usage_minute>
</coffee_pot>
</house>
</components>
如果我必须给我的程序一个“浓缩咖啡机”,我应该有以下输出:
[浓缩咖啡机] [360] [360] [360] [0.25] [15]
然而,结果是这样的:
[浓缩咖啡机] [] [] [] [] []
我认为问题出在开关/外壳上,innerElement.getTextTrim()
但我不确定。你能帮我解决这个问题吗?