我最近正在开发一个天气应用程序,我想将每个客户搜索保存在一个 .xml 格式的文件中。. . 请参阅此处的 GUI 设计。.
我在 Java 上使用以下代码在 .xml 文件中写入所需的元素,但问题是我无法追加下一个搜索并保存新搜索,如下所示:
<search>...NEW ELEMENTS (NEW SEARCH)...</seach>
当我单击“预测”按钮时。
WeatherParser.java:
public void writeOnXML(String date, String[] term, String found, String geoNameID){
try {
XMLOutputFactory xMLOutputFactory = XMLOutputFactory.newFactory();
XMLStreamWriter xMLStreamWriter;
xMLStreamWriter = xMLOutputFactory
.createXMLStreamWriter(new FileOutputStream
("src\\main\\java\\stax\\weatherSearches.xml"));
//Create <weatherSearches></weatherSearches> (in weatherSearches.xml)
xMLStreamWriter.writeStartElement("weathersearches");
//Create <search></search> (weatherSearches.xml)
xMLStreamWriter.writeStartElement("search");
xMLStreamWriter.writeAttribute("date", date);
//Create <search><term>...</term></search> (weatherSearches.xml)
xMLStreamWriter.writeStartElement("term");
xMLStreamWriter.writeStartElement("temperature");
xMLStreamWriter.writeCharacters(term[0]);
xMLStreamWriter.writeEndElement(); //Close <Temperature>
xMLStreamWriter.writeStartElement("windDirection");
xMLStreamWriter.writeCharacters(term[1]);
xMLStreamWriter.writeEndElement(); //Close <WindDirection>
xMLStreamWriter.writeStartElement("windSpeed");
xMLStreamWriter.writeCharacters(term[2]);
xMLStreamWriter.writeEndElement(); //Close <WindSpeed>
xMLStreamWriter.writeStartElement("humidity");
xMLStreamWriter.writeCharacters(term[3]);
xMLStreamWriter.writeEndElement(); //Close <Humidity>
xMLStreamWriter.writeStartElement("pressure");
xMLStreamWriter.writeCharacters(term[4]);
xMLStreamWriter.writeEndElement(); //Close <Pressure>
xMLStreamWriter.writeStartElement("visibility");
xMLStreamWriter.writeCharacters(term[5]);
xMLStreamWriter.writeEndElement(); //Close <Visibility>
xMLStreamWriter.writeEndElement(); //close <term>
//Create <search><found></found></search> (weatherSearches.xml)
xMLStreamWriter.writeStartElement("found");
xMLStreamWriter.writeCharacters(found);
xMLStreamWriter.writeEndElement(); //Close <found>
//Create <search><geoNameID></geoNameID></search> (weatherSearches.xml)
xMLStreamWriter.writeStartElement("geoNameID");
xMLStreamWriter.writeCharacters(geoNameID);
xMLStreamWriter.writeEndElement(); //Close <geoNameID>
//Create <search></search> (weatherSearches.xml)
xMLStreamWriter.writeEndElement(); //Close <search>
xMLStreamWriter.writeEndElement(); //Close <weathersearches>
//xMLStreamWriter.flush();;
xMLStreamWriter.close();
}
catch (FileNotFoundException ex) {
loadErrorException(ex);
}
catch (XMLStreamException ex) {
loadErrorException(ex);
}
}
天气搜索.xml:
<weathersearches>
<search date="Saturday - 14:00">
<term>
<temperature>11�C (52�F)</temperature>
<windDirection>Southerly</windDirection>
<windSpeed>18mph</windSpeed>
<humidity>97%</humidity>
<pressure>988mb</pressure>
<visibility>Poor</visibility>
</term>
<found>true</found>
<geoNameID>2656397</geoNameID>
</search>
</weathersearches>
元素中的未知符号<weathersearches><search><term><temperature>
也会导致我出现任何错误,还是应该从那里删除它?