android - How am I going to create a xml file using pullparser for hashmap -


i trying populate xml file user's input values. user give 2 entries key , value. , have model class below:

public class person {    private hashmap<string, string> hash = new hashmap<string, string>();  public person() {   }  public person(string key, string val) { hash.put(key, val);   }   public string getfirstname(string k) { return hash.get(k);   }  } 

how make xml object of class? , how retrieve value xml against key?

and want xml this:

<allentries>    <entry key="key1">value1</entry>     <entry key="key2">value2</entry>     <entry key="key3">value3</entry> </allentries>  

you need use xml parser java dom or sax. example below java dom , shows how loop through hashmap , add entries your_xml.xml.

file xmlfile = new file("your_xml.xml"); documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbuilder = dbfactory.newdocumentbuilder(); document doc = dbuilder.parse(xmlfile);  (map.entry<string, string> m : hash.entryset()) {     // create entry element , set it's value     element entry = doc.createelement("entry");     entry.settextcontent(m.getvalue());     // create attribute, set it's value , add attribute entry            attr attr = doc.createattribute("key");     attr.setvalue(m.getkey());     entry.setattributenode(attr);     // append entry root element     doc.getdocumentelement().appendchild(entry); } 

then need save document on original file. once document has been edited you'll want convert string parse outputstream of choice saving.


Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -