How am I going to create a xml file using pullparser for hashmap
Asked Answered
B

1

0

I am trying to populate a xml file from user's input values. User will give 2 entries a Key and a value. And i have a model class which is a as 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 to make a xml from this object of the class? and how to retrieve a value from xml against a key?

And i want the xml like this:

<AllEntries>
   <entry key="key1">value1</entry> 
   <entry key="key2">value2</entry> 
   <entry key="key3">value3</entry>
</AllEntries> 
Brookins answered 21/3, 2014 at 8:14 Comment(4)
By populate, I assume you mean adding entries to an existing xml? Where will these entries go? In the root node? How do you want the entries to appear? like this <person key="" value="" />? We'll need a little more info.Mcnamee
ya should append to existing xml. And i want the xml like this: <AllEntries> <entry key="key1">value1</entry> <entry key="key2">value2</entry> <entry key="key3">value3</entry> </AllEntries>Brookins
Maybe add that into the question and format it. It will help people answer and make the answer you get more specific to your problem. I'll take a look at it in my lunch break if someone hasn't solved it for you already. :)Mcnamee
that wud be great , coz i have to submit my project within 2 hrs :-)Brookins
M
0

You need to use a XML parser like Java DOM or SAX. The example below is Java DOM and shows you how to loop through the HashMap and add your entries to your_xml.xml.

File xmlFile = new File("your_xml.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);

for (Map.Entry<String, String> m : hash.entrySet()) {
    // Create your entry element and set it's value
    Element entry = doc.createElement("entry");
    entry.setTextContent(m.getValue());
    // Create an attribute, set it's value and add the attribute to your entry       
    Attr attr = doc.createAttribute("key");
    attr.setValue(m.getKey());
    entry.setAttributeNode(attr);
    // Append your entry to the root element
    doc.getDocumentElement().appendChild(entry);
}

Then you just need to save your document over the original file. Once your Document has been edited you'll probably want to convert it to a String to parse to your OutputStream of choice for saving.

Mcnamee answered 21/3, 2014 at 9:25 Comment(8)
whats hash & how to append my data in xml, i mean the new data with the old ? erm, plus how do add hashmap in xml?Brookins
@ShaonHasan - hash is your HashMap instance, as per your example code in the question. The above code adds the data into xml in the format you specified into an xml file called "your_file.xml". You need to specify the file location there.Mcnamee
I just added : Person hash = new Person("1","Shaon"); in the activity start. But hash.entrySet() is showing error on entryset. :-(Brookins
@ShaonHasan - hash needs to be an instance of a HashMap, not a Person. I think you need to read a good Java book or some tutorial to understand scope and the object IS-A relationship. I'm not sure I can talk you through all that to get the result you need. It's just too much for an answer on StackOverflow.Mcnamee
k.. added HashMap<String, String> hash = new HashMap<String, String>(); hash.put("2", "Shaon"); inside activity. and ur code inside a try catch block also inside activity. did i do wrong again?Brookins
@ShaonHasan - That should do it, but you will still need to save the created document.Mcnamee
i have added these undernith ur code: TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult streamResult = new StreamResult(new File("your_xml1.xml")); transformer.transform(source, streamResult); but cant find any xml file in data folder inside the project.Brookins
@ShaonHasan - Any xmls in your project folders get compiled when the project is, so you can't change/replace them at runtime. You will have to save a copy into android storage.Mcnamee

© 2022 - 2024 — McMap. All rights reserved.