473,480 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to create xml

39 New Member
Hi,

I have a json that i have parsed with simplejson, Now i have to make a xml by using this json
Expand|Select|Wrap|Line Numbers
  1. a = simplejson.loads('{"result":"true","name":{"first":"abcd","middle":"efgh"}}')
  2.  a
  3. {u'result': u'true', u'name': {u'middle': u'efgh', u'first': u'abcd'}}
  4.  
  5.  
now i have to create a xml by using these data like name,result,middle,first.

could you tell me how to do this,
some code will be good for me,
if you can tell links that will also be good.
Thanx
Oct 17 '07 #1
1 1914
diwakar09
39 New Member
Hi
i have got the thing, so no need to try it guys
here is the whole code
Expand|Select|Wrap|Line Numbers
  1. from xml.dom.minidom import Document
  2. import simplejson
  3. doc = Document()
  4. class xmlhelper:
  5.     astring = None
  6.     jsonBuffer = None
  7.     def __init__(self,sstr):
  8.         self.astring = sstr
  9.     def createjson(self):
  10.         self.jsonBuffer =  simplejson.loads(self.astring)
  11.     def isValidDict(self, aDict):
  12.         try:
  13.             itemList = aDict.items()
  14.             return True
  15.         except:
  16.             return False
  17.  
  18.     def createXML(self,aTime,key,dict):
  19.         # Create the minidom document
  20.         result = doc.createElement(key)
  21.         aTime.appendChild(result)
  22.         val = dict.get(key)
  23.  
  24.         if self.isValidDict(val):
  25.             dictobj = dict.get(key)
  26.             for i in dictobj.iterkeys():
  27.                 j = dictobj.get(i)
  28.                 if self.isValidDict(j):
  29.                     self.createXML(result,i,j)
  30.                 else:
  31.                     temp = doc.createElement(i)
  32.                     result.appendChild(temp)
  33.                     ptext = doc.createTextNode(j)
  34.                     temp.appendChild(ptext)
  35.         else:
  36.             ptext = doc.createTextNode(val)
  37.             result.appendChild(ptext)
  38.             return
  39.  
  40.  
  41.     def jsontoxml(self):
  42.         aTime = doc.createElement('aTime')
  43.         doc.appendChild(aTime)
  44.         for key in self.jsonBuffer.iterkeys():
  45.             self.createXML(aTime,key,self.jsonBuffer)
  46.         print doc.toprettyxml(indent="  ")
  47.  
  48. def xmlStart():
  49.  
  50.     obj = xmlhelper('{"result":"true","name":{"first":"abcd","middle":"dhsjk"},"command":{"inter":"ps","exter":"ls"}}')
  51.     obj.createjson()
  52.     obj.jsontoxml()
  53.  
Oct 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
8818
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
4380
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
10
8768
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung...
9
8245
by: Peter | last post by:
Hello£¬everyone, My program will collect a testing machine's data ,save the data and deal with the data everyday. I want to use vb.net to create database, add and delete tables or modify the...
37
3241
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
2680
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
5
4851
by: Michael | last post by:
Hello, I've created an ASP web page where users in our organization can create Active Directory computer accounts. The web page is running on a Server 2003 SP1 IIS 6 installation. The...
8
20326
by: barb | last post by:
So that the world at large benefits from our efforts, here is one fully documented way to use Windows Irfanview freeware to create thumbnail web galleries (http://www.irfanview.com). STEP 1:...
2
22549
by: masri999 | last post by:
I have a requirement in SQL 2005 in Development database 1. Schema dbo owns all objects (tables,views,SPs,UDFs etc) . 2. Only DBA's ( who are database owners ) can create, alter tables ....
4
3725
by: JohnnyDeep | last post by:
I am trying to create a store proc that contain a create index with the cluster option and I receive DB21034E The command was processed as an SQL statement because it was not a valid Command...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.