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" ?>
<shopdata>
    <info>
        <shopname>おいしい肉屋</shopname>
        <yomi>おいしいにくや</yomi>
        <address>市内</address>
        <business_hours>10:30〜21:30</business_hours>
        <close>水曜休(季節により変更あり)</close>
        <tel>00-00-000</tel>
        <map>google map</map>
        <position>街中</position>
        <coupon>何かクーポン</coupon>
        <coupon_offer>何かテキスト</coupon_offer>
        <coupon_use>何かテキスト</coupon_use>
        <coupon_date>なし</coupon_date>
        <url></url>
        <budget></budget>
        <seat></seat>
        <card></card>
        <parking></parking>
        <smoking></smoking>
        <main_text>何かテキスト</main_text>
        <sub_text_a></sub_text_a>
        <sub_text_b></sub_text_b>
        <sub_text_c></sub_text_c>
        <file_name>nikuya</file_name>
        <image>0910_01</image>
        <image2>0910_02</image2>
        <image3>0910_03</image3>
        <new_open></new_open>
        <special_1></special_1>
        <special_2></special_2>
        <special_3></special_3>
        <area>9</area>
        <category>1</category>
    </info>
</shopdata>

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)):
              shop_data.append(self.datalist[i])
          return shop_data

      def loop_data(self,list_data,start,end):
          try:
             for i in range(start,end):
                 g = self.tree.getElementsByTagName(list_data).item(i).childNodes[0].data
             self.datalist.append(g)
             return self.datalist
          except IndexError:
             list_data = ["shopname","tel","position"]
             for i in range(start,end):
                 g = self.tree.getElementsByTagName(list_data).item(i)
                 self.datalist.append(g)
             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])
  else:
    print "Error! given two arguments. Two or less arguments only, Enter start and the end"
    sys.exit()

  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])
      fp.write(write_data+"\n")
  f.close()

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.

},{
     name:"おいしい肉屋",
     tel:"00-00-000",
     area:"街中",
     food:"何かテキスト"

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