我的XML
文件如下:
<?xml version="1.0" encoding="utf-8"?>
<Test1>
<task uuid="92F7F685-C370-4E55-9026-020E3CDCEDE0" status="1000">
<task_status>200</task_status>
</task>
<task uuid="92F7F685-C370-4E55-9026-020E3CDCEDE0" status="
<task_status>200</task_status>
</task>
</Test1>
此文件存储在私人应用程序目录中。我只是想编辑这个文件并将其存储在其中&#34;更新&#34;版本在同一目录中。
我有这种方法来编写和读取XML文件:
private void writeToFile(String data, String fileName) {
try {
String UTF8 = "UTF-8";
int BUFFER_SIZE = 8192;
FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, UTF8), BUFFER_SIZE);
bufferedWriter.write(data);
bufferedWriter.close();
} catch (IOException e) {
Log.e("writeToFile: ", "Datei-Erstellung fehlgeschlagen: " + e.toString());
}
}
//Datei lesen von Datei im privatem App-Verzeichnis
private String readFromFile(String fileName) {
String ret = "";
String UTF8 = "UTF-8";
int BUFFER_SIZE = 8192;
try {
InputStream inputStream = openFileInput(fileName);
if (inputStream != null) {
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream, UTF8), BUFFER_SIZE);
String receiveString;
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader1.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("readFromFile: ", "Datei nicht gefunden: " + e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e("readFromFile: ", "Kann Datei nicht lesen: " + e.toString());
e.printStackTrace();
}
return ret;
}
如何添加<task_note>
等新标记?我想把它放在正确的任务中,所以我必须使用uuid
属性来确定我必须放置标签的位置。
我知道XmlPullParser
只是为了阅读,所以这没有用。
那么,我怎么能这样做呢?
编辑:
我收到此错误:
07-28 11:16:22.676 17703-17703/de.exampleapp W/System.err﹕ javax.xml.xpath.XPathExpressionException: java.net.MalformedURLException: Protocol not found: 000273E060E87000C0001323FA21427120150602153306.kx_task
07-28 11:16:22.676 17703-17703/de.exampleapp W/System.err﹕ at org.apache.xpath.jaxp.XPathImpl.evaluate(XPathImpl.java:481)
07-28 11:16:22.676 17703-17703/de.example.app W/System.err﹕ at de.example.app.TasksList.onActivityResult(TasksList.java:141)
我在这里得到这个:
InputSource inputSource = new InputSource(filename);
String uuid = taskItems.get(position).get("uuid");
XPath xPath = XPathFactory.newInstance().newXPath();
try {
Node taskNode = (Node) xPath.evaluate("//task[@uuid='" + uuid + "']", inputSource, XPathConstants.NODE);
Document document = taskNode.getOwnerDocument();
//Füge neue Zeile ein
Node noteNode = document.createElement("task_note");
noteNode.setTextContent(taskItems.get(position).get("task_note"));
taskNode.appendChild(noteNode);
//Speichere Datei
Source input = new DOMSource(document);
Result output = new StreamResult(new File(filename));
TransformerFactory.newInstance().newTransformer().transform(input, output);
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
不幸的是,您将XML数据视为简单的文本文件。这不是一个好主意。 XML是一种恰好基于文本的数据格式 - 就像从头到尾阅读字典一样......是的,它是一本书,从技术上说,所以你可以做到,但不是它通常用于什么?与XML相同,您可以将其作为简单的文本文件读取,但通常您希望实际解析此文本中包含的数据,操作此数据,然后再次序列化XML。
所以你应该通过其中一种处理XML的方式来阅读它,例如通过JAXB(使用你希望拥有的Schema)或简单的DOMParser。但当然还有很多其他方法可以做到这一点......
答案 1 :(得分:1)
您可以在Android中使用XPath
和其他Java XML标准库:
// Read file
InputSource inputStream = new InputSource(new FileInputStream(inputFileName));
// Find the task node
XPath xpath = XPathFactory.newInstance().newXPath();
Node taskNode = (Node)xpath.evaluate("//task[uuid='92F7F685-C370-4E55-9026-020E3CDCEDE0']", inputStream,
XPathConstants.NODE);
Document document = taskNode.getOwnerDocument();
// Insert a new node
Node noteNode = document.createElement("task_note");
noteNode.setTextContent("this is a note");
taskNode.appendChild(noteNode);
// Save file
Source input = new DOMSource(document);
Result output = new StreamResult(new File(outputFileName));
TransformerFactory.newInstance().newTransformer().transform(input, output);