Sunday, June 13, 2010

How to create or convert XML from Excel

I was doing a Project on Some Data extraction from XML using parsing.

I faced the problem of creating XML file from Excel 2007.After getting solution i am writing this post for your help.

I am illustrating it with help of an example.

Let you want to create a XML file for giving some reminder.
XML file have following fields

1.mail --root
2.reminder
3.to
4.from
5.heading
6.message


steps for creating xml file

1. First of all you have to enable Developer tab in Excel,for this perform following steps
1.1 Open Excel
1.2 Open menu by Pressing round button containing the microsoft logo in the top-left corner
1.3 click on the Excel option, this will open the option window


1.4 Now in option window under Popular category check the option
"Show developer Tab in the Ribbon"


2. In second step, create a small XML file into notepad having more than one entry of each element of XML.This is because Excel will create on fly XML map from this sample.

for e.g.

create reminder.xml in notedpad having following xml data:

<?xml version="1.0" encoding="ISO-8859-1"?>
<mail>
<reminder>
<to>Ashu </to>
<from>Rahul</from>
<heading>Money Reminder</heading>
<message>Don't forget to give me Rs.100 back</message>
</reminder>
<reminder>
<to>Vaibhav</to>
< from>luv</from>
<heading>book reminder</heading>
<message>bring my book back</message>
</reminder>
</mail>

2. Now open reminder.xml into Excel using file>open>reminder.xml

3. when you open this file, Excel will show a dialog box "open XML" . Select "Use the XML source Task Pan"





4.Next - we get the information box that the XML does not have a schema. This is a fantastic message - because Microsoft is now going to create a schema for you on the fly.
Press Ok here.


5. You now get a blank worksheet, but the XML Source pane on the right lists all of the available fields.




6.Choose the Options dropdown and select Preview data to see a sample of each element in the task pane.


7. Now drag the reminder element and drop it to any cell say B7.
you would get a sheet like this.



8. now go to Developers tab and press Refresh Data.



9. Now you would get the fresh data like this.


10.Now enter the data in this table.



11. In last step save this Excel file using save as option
name the file as reminder updated.xml and change save as type to XML Data .

You got your XML file ready



Please give comments and feedback.