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> 
Shaon Hasan
  • 730
  • 7
  • 17
  • 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 ``? We'll need a little more info. – Rudi Kershaw Mar 21 '14 at 08:28
  • ya should append to existing xml. And i want the xml like this: value1 value2 value3 – Shaon Hasan Mar 21 '14 at 08:32
  • 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. :) – Rudi Kershaw Mar 21 '14 at 08:35
  • that wud be great , coz i have to submit my project within 2 hrs :-) – Shaon Hasan Mar 21 '14 at 08:38

1 Answers1

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.

Community
  • 1
  • 1
Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
  • 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? – Shaon Hasan Mar 21 '14 at 09:34
  • @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. – Rudi Kershaw Mar 21 '14 at 09:39
  • I just added : Person hash = new Person("1","Shaon"); in the activity start. But hash.entrySet() is showing error on entryset. :-( – Shaon Hasan Mar 21 '14 at 09:48
  • @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. – Rudi Kershaw Mar 21 '14 at 09:54
  • k.. added HashMap hash = new HashMap(); hash.put("2", "Shaon"); inside activity. and ur code inside a try catch block also inside activity. did i do wrong again? – Shaon Hasan Mar 21 '14 at 10:02
  • @ShaonHasan - That should do it, but you will still need to save the created document. – Rudi Kershaw Mar 21 '14 at 10:03
  • 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. – Shaon Hasan Mar 21 '14 at 10:20
  • @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.](http://developer.android.com/guide/topics/data/data-storage.html) – Rudi Kershaw Mar 21 '14 at 10:35