Python Looping and results printing

compbuilder00

Senior member
Jul 27, 2006
628
3
81
Hey Guys,

Could use a second pair of eyes on a small python script to convert JSON to XML. I've been looking at this all day and cant seem to get it to run past the first line of the json file I'm trying to convert. It converts the first line beautifully but doesn't seem to go past it. I think it has to do with the for statement but I hit the wall. Any hints would help!

Sean

Code:
from xml.dom.minidom import Document
from json import JSONDecoder
import dicttoxml
import json
import bson
#from bson import BSONdecoder
#BSONdecoder().decode('{}')
from functools import partial
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

nav = 'navigation_items.bson.json'
df = 'butt1.txt'

class DictToXMLtest(object):
    default_list_item_name = "item"

    def __init__(self, structure, list_mappings={}):
        self.doc = Document()

        if len(structure) == 1:
            rootName = str(list(structure.keys())[0])
            self.root = self.doc.createElement(rootName)

            self.list_mappings = list_mappings

            self.doc.appendChild(self.root)
            self.build(self.root, structure[rootName])

    def build(self, father, structure):
        if type(structure) == dict:
            for k in structure:
                tag = self.doc.createElement(k)
                father.appendChild(tag)
                self.build(tag, structure[k])
        elif type(structure) == list:
            tag_name = self.default_list_item_name

            if father.tagName in self.list_mappings:
                tag_name = self.list_mappings[father.tagName]

            for l in structure:
                tag = self.doc.createElement(tag_name)
                self.build(tag, l)
                father.appendChild(tag)
        else:
            data = str(structure)
            tag = self.doc.createTextNode(data)
            father.appendChild(tag)

    def display(self):
        print(self.doc.toprettyxml(indent="  "))

    def get_string(self):
        return self.doc.toprettyxml(indent="  ")
       
def json_parse(fileobj, decoder=JSONDecoder(), buffersize=16384):
    buffer = ''
    for chunk in iter(partial(fileobj.read, buffersize), ''):
         buffer += chunk
         while buffer:
             try:
                 result, index = decoder.raw_decode(buffer)
                 yield result
                 buffer = buffer[index:]
             except ValueError:
                 # Not enough data to decode, read more
                 break


if __name__ == '__main__':

    ifile = open(nav,'r')
    nfile = open(df,'w')
   
    data = {}
    with ifile as data_file:
        data = (data_file)
       
        for line in json_parse(data):
            nfile.write(dicttoxml.dicttoxml(line, attr_type=False))
       
nfile.close()
 

Childs

Lifer
Jul 9, 2000
11,450
7
81
Two things:

1. Did you try a different json file, or can you attach the one you are working with?
2. Why not use json.load():

Code:
if __name__ == '__main__':

    ifile = open(nav,'r')
    nfile = open(df,'w')

    with ifile as data_file:
        data = json.load(data_file)
        nfile.write(dicttoxml.dicttoxml(data, attr_type=False))
      
nfile.close()

This would look make the xml more human readable:

Code:
from xml.dom.minidom import parseString

if __name__ == '__main__':

    ifile = open(nav,'r')
    nfile = open(df,'w')
   
    with ifile as data_file:
        data = json.load(data_file)
        xml = dicttoxml.dicttoxml(data, attr_type=False)
        dom = parseString(xml)
        nfile.write(dom.toprettyxml())
       
nfile.close()

Anyways, I dont seem to have any problems using your code or json.load().
 

compbuilder00

Senior member
Jul 27, 2006
628
3
81
Sorry to follow up so late. It was the file being exported to me. Need to have commas and brackets added to make it consume the file line by line. Anyways, thanks again for the help!
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |