XML → JSON script

I wrote program which XML change to JSON. Your work what change XML format to JSON format will easy! Example, there is this XML about several shop data.

<?xml version="1.0" encoding="UTF-8" ?>
        <map>google map</map>

The following code will rewrite to JSON format. It will extract shopname,tel,position and main_text.

#-*- coding:utf-8 -*-

from xml.dom import minidom
import codecs
import sys

class xml_parse(object):
      def __init__(self,node):
          self.datalist = []
          self.tree = minidom.parse(node)

      def readon(self,lisu):
          shop_data = []
          for i in range(len(lisu)):
          return shop_data

      def loop_data(self,list_data,start,end):
             for i in range(start,end):
                 g = self.tree.getElementsByTagName(list_data).item(i).childNodes[0].data
             return self.datalist
          except IndexError:
             list_data = ["shopname","tel","position"]
             for i in range(start,end):
                 g = self.tree.getElementsByTagName(list_data).item(i)
             return self.datalist

      def item_obj(self,get_element,st,en):
             element_list = self.loop_data(get_element,st,en)
             return element_list

class format(object):
      def fom(self,a,b,c,d):
          display_data = "    },{\n     name:\"%s\",\n     tel:\"%s\",\n     area:\"%s\",\n     food:\"%s\"" %(a,b,c,d)
          return display_data

if __name__ == '__main__':
  read_file = "shopdata.xml"
  comp_parse = xml_parse(read_file)
  element = ["shopname","tel","position","main_text"]

  if len(sys.argv) != 1:
     xml_start = int(sys.argv[1])
     xml_end = int(sys.argv[2])
    print "Error! given two arguments. Two or less arguments only, Enter start and the end"

  for nk in range(xml_start,xml_end):
      for j in range(len(element)):
          parse_element = comp_parse.item_obj(element[j],nk,nk+1)
      gen = comp_parse.readon(parse_element)

  f = open("shopiin.txt","a")
  foma = format()
  for j in range(0,len(gen),4):
      fp = codecs.getwriter('utf-8')(f)
      write_data = foma.fom(gen[j],gen[j+1],gen[j+2],gen[j+3])

This program require two arguments. If this program is named shop_json, please you input this

shop_json.py 1 10

First argument is point of start in data. second argument is end point. As a result, Output file which name is shopiin.txt which was written ten of data. All of ten data of XML is changed to JSON like this.


And you adjust it now. Javascript is made to read.