473,289 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

how to create xml

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 1912
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
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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.