我正在编写一个Java应用程序,其中包含一种将数据保存到XML的方法。
这是我的代码:
private void SaveToXML(String strCity, String strDate, String strforecast, String strminDegrees, String FileName)
{
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Weather");
doc.appendChild(rootElement);
Element weatherElement = doc.createElement(strCity);
rootElement.appendChild(weatherElement);
Element dateElement = doc.createElement("Date");
weatherElement.appendChild(dateElement);
Attr attr = doc.createAttribute("id");
attr.setValue(strDate);
dateElement.setAttributeNode(attr);
Element forecast = doc.createElement("forecast");
forecast.appendChild(doc.createTextNode(strforecast));
dateElement.appendChild(forecast);
Element mindegrees = doc.createElement("mindegrees");
mindegrees.appendChild(doc.createTextNode(strminDegrees));
dateElement.appendChild(mindegrees);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(FileName));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
方法调用:
SaveToXML("Auckland", "24-05-2013", "Fine", "10", "Test.xml");
以下是输出XML数据:
<?xml version="1.0" encoding="UTF-8"?>
<Weather>
<Auckland>
<Date id="24-05-2013">
<forecast>Fine</forecast>
<mindegrees>10</mindegrees>
</Date>
</Auckland>
</Weather>
我可以帮助修改代码,以便在调用方法时将数据附加到正确元素中的文档中。
例如,如果该方法是第二次与奥克兰市召开,天气详情将放在奥克兰元素中。如果将City作为参数传递给文档中尚未存在,则将创建该城市的新元素。
UPDATE2
这是我当前执行错误的代码:
private void SaveToXML(String strCity, String strDate, String strforecast, String strminDegrees, String FileName)
{
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//Document doc = docBuilder.newDocument();
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(FileName));
Element rootElement = doc.createElement("Weather");
doc.appendChild(rootElement);
NodeList weatherNodes = rootElement.getElementsByTagName(strCity);// do we already have node?
Element weatherElement;
if(weatherNodes.getLength() > 0){ // if so reuse
weatherElement = (Element) weatherNodes.item(0);
System.out.println("Found");
}else { // else create
weatherElement = doc.createElement(strCity);
rootElement.appendChild(weatherElement);
}
Element dateElement = doc.createElement("Date");
weatherElement.appendChild(dateElement);
Attr attr = doc.createAttribute("id");
attr.setValue(strDate);
dateElement.setAttributeNode(attr);
Element forecast = doc.createElement("forecast");
forecast.appendChild(doc.createTextNode(strforecast));
dateElement.appendChild(forecast);
Element mindegrees = doc.createElement("mindegrees");
mindegrees.appendChild(doc.createTextNode(strminDegrees));
dateElement.appendChild(mindegrees);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(FileName));
transformer.transform(source, result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
以上代码在运行时生成此错误:
[致命错误] Test.xml:1:177:根元素后面的文档中的标记必须格式正确。 org.xml.sax.SAXParseException:根元素后面的文档中的标记必须格式正确。
UPDATE3
以下是正在运行的代码:
private void SaveToXML(String strCity, String strDate, String strforecast, String strminDegrees, String FileName)
{
try
{
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc;
File file = new File(FileName);
if (!file.exists()) {
doc = docBuilder.newDocument();
doc.appendChild(doc.createElement("Weather"));
} else {
doc = docBuilder.parse(new File(FileName));
}
Element rootElement = doc.getDocumentElement();
Element weatherElement;
NodeList weatherNodes = doc.getDocumentElement().getElementsByTagName(strCity);
if (weatherNodes.getLength() > 0) {
weatherElement = (Element) weatherNodes.item(0);
} else {
weatherElement = doc.createElement(strCity);
rootElement.appendChild(weatherElement);
}
Element dateElement = doc.createElement("Date");
weatherElement.appendChild(dateElement);
Attr attr = doc.createAttribute("id");
attr.setValue(strDate);
dateElement.setAttributeNode(attr);
Element forecast = doc.createElement("forecast");
forecast.appendChild(doc.createTextNode(strforecast));
dateElement.appendChild(forecast);
Element mindegrees = doc.createElement("mindegrees");
mindegrees.appendChild(doc.createTextNode(strminDegrees));
dateElement.appendChild(mindegrees);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(FileName));
transformer.transform(source, result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
答案 0 :(得分:0)
修改更新DOM文档结构的方式,
修改
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Weather");
doc.appendChild(rootElement);
Element weatherElement = doc.createElement(strCity);
rootElement.appendChild(weatherElement);
到
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Weather");
doc.appendChild(rootElement);
NodeList weatherNodes = rootElement.getElementsByTagName(strCity);// do we already have node?
Element weatherElement;
if(weatherNodes.getLength() > 0){ // if so reuse
weatherElement = (Element) weatherNodes.item(0);
}else { // else create
weatherElement = doc.createElement(strCity);
rootElement.appendChild(weatherElement);
}
请注意,如果DOM变得非常大,那么DOM实际上适用于小型文档 你需要看看像STaX这样的东西。
答案 1 :(得分:0)
如果XML文件已存在,则应解析它并更新城市(如果已存在)或以其他方式添加。然后重写XML。
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc;
File file = new File(FileName);
if (!file.exists()) {
doc = docBuilder.newDocument();
doc.appendChild(doc.createElement("Weather"));
} else {
doc = docBuilder.parse(file);
}
Element rootElement = doc.getDocumentElement();
Element weatherElement = createWeatherElement(strCity, strDate, strforecast, strminDegrees, doc);
NodeList weatherNodes = doc.getDocumentElement().getElementsByTagName(strCity);
if (weatherNodes.getLength() > 0) {
rootElement.replaceChild(weatherElement, weatherNodes.item(0));
} else {
rootElement.appendChild(weatherElement);
}
Transformer transformer = transformerFactory.newTransformer();
...
private Element createWeatherElement(String strCity, String strDate, String strforecast, String strminDegrees, Document doc) {
Element rootElement = doc.getDocumentElement();
Element weatherElement = doc.createElement(strCity);
...
return weatherElement;
}