java - Add line to XML using attribute as identifier -
my xml
file looks like:
<?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>
this file stored in private app directory. want edit file , store in it's "newer" version in same directory.
i have method write , read xml files:
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; }
how can add new tag <task_note>
? want put in right task, have use uuid
attribute identify have put tag.
the xmlpullparser
reading know, doesn't help.
so, how can then?
edit:
i error:
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﹕ @ org.apache.xpath.jaxp.xpathimpl.evaluate(xpathimpl.java:481) 07-28 11:16:22.676 17703-17703/de.example.app w/system.err﹕ @ de.example.app.taskslist.onactivityresult(taskslist.java:141)
i here:
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(); } }
you can use xpath
, , other java xml standard libraries within android:
// read file inputsource inputstream = new inputsource(new fileinputstream(inputfilename)); // find 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 new node node notenode = document.createelement("task_note"); notenode.settextcontent("this note"); tasknode.appendchild(notenode); // save file source input = new domsource(document); result output = new streamresult(new file(outputfilename)); transformerfactory.newinstance().newtransformer().transform(input, output);
Comments
Post a Comment