当前位置: 首页>>代码示例>>Python>>正文


Python Car.to_xml_node方法代码示例

本文整理汇总了Python中car.Car.to_xml_node方法的典型用法代码示例。如果您正苦于以下问题:Python Car.to_xml_node方法的具体用法?Python Car.to_xml_node怎么用?Python Car.to_xml_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在car.Car的用法示例。


在下文中一共展示了Car.to_xml_node方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: open

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
"""Loads json data from file"""
json_file = open("7-main.json")
data = json.loads(json_file.read())

"""Initializes the xml document"""
xml_cars = doc.createElement("cars")
doc.appendChild(xml_cars)

"""Creates a list of Car classes"""
for car in data:
    car = Car(car)
    cars.append(car)

for car in cars:
    """Converts car to xml node"""
    c_xml = car.to_xml_node(doc)

    """Add year element to the car"""
    year = doc.createElement("year")
    year_content = doc.createTextNode("2015")
    year.appendChild(year_content)
    c_xml.appendChild(year)

    """Sets the weight attribute of the car"""
    c_xml.setAttribute("weight", "1000")

    """Converts the brand element to a CDATA section with the © symbol added"""
    brand = c_xml.getElementsByTagName("brand")[0]
    new_brand = "©" + brand.childNodes[0].nodeValue
    brand_content = doc.createCDATASection(new_brand)
    brand.removeChild(brand.childNodes[0])
开发者ID:rickharris-dev,项目名称:holbertonschool-higher_level_programming,代码行数:33,代码来源:7-main.py

示例2: Car

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
from xml.dom.minidom import Document
from car import Car

c = Car(name="Rogue", brand="Nissan", nb_doors=5)
print c
c_json_string = c.to_json_string()
print type(c_json_string)
print c_json_string

doc = Document()
c_xml = c.to_xml_node(doc)
doc.appendChild(c_xml)
print doc.toxml(encoding='utf-8')
开发者ID:IanWagener,项目名称:holbertonschool-higher_level_programming,代码行数:15,代码来源:4-main.py

示例3: Document

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
counter = 0
brands = []
fourth_car = ""
doc = Document()
cars = doc.createElement("Cars")
while counter < len(json_data):
    c = Car(json_data[counter])
    ''' adding number of doors castied into ints '''
    doorsNum += int(c.get_nb_doors())
    '''Adding elements to the array brands '''
    brands.append(c.get_brand())
    if counter == 3:
        fourth_car = c
    counter +=1
    '''Appending to root element cars '''
    cars.appendChild(c.to_xml_node(doc))
doc.appendChild(cars)
''' set gets rid of the duplicates'''
print len(set(brands))
print doorsNum
print fourth_car
print doc.toxml(encoding='utf-8')








开发者ID:stvngrcia,项目名称:holbertonschool-higher_level_programming,代码行数:24,代码来源:5-main.py

示例4: open

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
''' Open the json file and load it into a variable to be used later '''
with open('5-main.json') as json_data:
    data = json.load(json_data)

all_brands = []
total_doors = 0

''' Create a DOM document '''
document = Document()
all_cars = document.createElement("all_cars")
document.appendChild(all_cars)

''' Iterate through data to format each dict '''
for i in data:
    car = Car(i)

    brand = car.get_brand()
    nb_doors = car.get_nb_doors()
    xml = car.to_xml_node(document)
    if brand not in all_brands:
        all_brands.append(brand)
    total_doors += nb_doors

    all_cars.appendChild(xml)

print len(all_brands)
print total_doors
last_car = Car(data[3])
print last_car
print document.toxml(encoding='utf-8')
开发者ID:DamianAli,项目名称:holbertonschool-higher_level_programming,代码行数:32,代码来源:5-main.py

示例5: isinstance

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
		return [load(element) for element in input]

	elif isinstance(input, unicode):
		return input.encode('utf-8')

	return input

file = open('5-main.json') 
file_contents = load(json.loads(file.read()))
file.close

cars = [] # create empty arrays.
brands = []
doors = 0

doc = Document()
cars_parent = doc.createElement('cars')
doc.appendChild(cars_parent)

for i in file_contents: #Iterate through each element.
	c = Car(i)
	cars.append(c)
	brands.append(c.get_brand())
	doors += c.get_nb_doors()
	car_node = c.to_xml_node(doc)
	cars_parent.appendChild(car_node)

print len(set(brands)) 
print doors
print cars[3]
print doc.toxml(encoding='utf-8')
开发者ID:jdeepee,项目名称:holbertonschool-higher_level_programming,代码行数:33,代码来源:5-main.py

示例6: open

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
'''read file'''
file = open("5-main.json")
content = file.read()
file_content = json.loads(content)

cars_obj = []
brands = []
nb_doors = 0

'''DOM'''
doc = minidom.Document()
cars = doc.createElement("cars")
doc.appendChild(cars)

for i in file_content:
  car = Car(i)
  cars_obj.append(car)
  brand = car.get_brand()
  if not brand in brands:
    brands.append(brand)
  nb = car.get_nb_doors()
  nb_doors += nb
  xml_car = car.to_xml_node(doc)
  cars.appendChild(xml_car)

print len(brands)
print nb_doors
print cars_obj[3]
print doc.toxml(encoding='utf-8')
开发者ID:madejean,项目名称:holbertonschool-higher_level_programming,代码行数:31,代码来源:5-main.py

示例7: Car

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
# create int for cumulative no. of doors for all cars
cnb_doors = 0

# create root element for DOM document which will contain all cars
doc = minidom.Document()
cars = doc.createElement("cars")
doc.appendChild(cars)

# for every car hash in <variable>, call car constructor with hash as dictionary
for i in file_contents:
    car = Car(i)

    # get brand of car, number of doors
    brand = car.get_brand()
    nb_doors = car.get_nb_doors()
    car_xml = car.to_xml_node(cars)

    # check if brand has already been accounted for in brands
    # (if not, add)
    if brand not in brands:
        brands.append(brand)

    # add number of doors from car to cnb_doors
    cnb_doors += nb_doors

    # add car element to cars xml
    cars.appendChild(car_xml)

# print number of different brands, print cumulative number of doors
print len(brands)
print cnb_doors
开发者ID:IanWagener,项目名称:holbertonschool-higher_level_programming,代码行数:33,代码来源:5-main.py

示例8: Document

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
doc = Document()
car_elements = doc.createElement('cars')
doc.appendChild(car_elements)
brands = []
num_doors = 0
index = 0

for line in json_data:
	''' Create car objects from json_data '''
	c = Car(name=(str(line['name'])),
			brand=(str(line['brand'])),
			nb_doors=line['nb_doors'])

	''' Create DOM document of cars '''
	car_elements.appendChild(c.to_xml_node(doc))

	''' Count unique car brands '''
	if line['brand'] not in brands:
		brands.append(line['brand'])

	''' Sum number of car doors '''
	num_doors += line['nb_doors']

	''' Find fourth car in json_data '''
	index += 1
	if index == 4:
		fourth_car = c

print len(brands)
print num_doors
开发者ID:frakentoaster,项目名称:holbertonschool-higher_level_programming,代码行数:32,代码来源:5-main.py

示例9: Document

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
from xml.dom.minidom import Document
from car import Car
from json import loads

"""Setting the dom obj"""
doc = Document()
x_cars = doc.createElement('cars')
doc.appendChild(x_cars)

"""Opening the json file"""
with open('./7-main.json') as f:
    data = loads(f.read())

"""Iterating through each obj and adding them to the dom"""
for car in data:
    item = Car(car)
    c_xml = item.to_xml_node(doc)
    year = doc.createElement('year')
    year_value = doc.createTextNode('2015')
    year.appendChild(year_value)
    c_xml.appendChild(year)
    c_xml.setAttribute('weight', str(1000))
    brand = doc.createElement('brand')
    brand_value = doc.createCDATASection(u"\u00a9" + item.get_brand())
    brand.appendChild(brand_value)
    c_xml.replaceChild(brand, c_xml.getElementsByTagName('brand')[0])
    x_cars.appendChild(c_xml)

"""Printing the dom to xml"""
print doc.toxml(encoding='utf-8')
开发者ID:havk64,项目名称:holbertonschool-higher_level_programming,代码行数:32,代码来源:7-main.py

示例10: Document

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
file_open.close()

'''initialize lists and variables'''
cars = []
brands = []
totalDoors = 0
doc = Document()

'''special XML stuff to create an element'''
element_cars = doc.createElement('cars')

'''special XML stuff to create a child element'''
doc.appendChild(element_cars)

'''Iterating through the json file to place
   items in the list and update the number of
   total doors'''
for item in diffCars:
    obj = Car(item)
    cars.append(obj)
    if not obj.get_brand() in brands:
        brands.append(obj.get_brand())
    totalDoors = totalDoors + obj.get_nb_doors()
    objXml = obj.to_xml_node(doc)
    element_cars.appendChild(objXml)

print len(brands)
print totalDoors
print cars[3]
print doc.toxml(encoding = 'utf-8')
开发者ID:nappybrainiac,项目名称:holbertonschool-higher_level_programming,代码行数:32,代码来源:5-main.py

示例11: open

# 需要导入模块: from car import Car [as 别名]
# 或者: from car.Car import to_xml_node [as 别名]
'''copy originial JSON file content to new JSON file'''
json_file = open("5-main.json")
file_copy = json.load(json_file)
json_file.close()

'''arrays for cars and brands'''
cars = []
brands = []

'''initialize number of car doors'''
car_doors = 0

'''prepare DOM document and create root element'''
doc = minidom.Document()
all_cars = doc.createElement('cars')
doc.appendChild(all_cars)

for each in file_copy:
    objects = Car(each)
    cars.append(objects)
    if objects.get_brand() not in brands:
        brands.append(objects.get_brand())
    car_doors += objects.get_nb_doors()
    all_cars.appendChild(objects.to_xml_node(doc))

print len(brands)
print car_doors
print cars[3]
print doc.toxml(encoding = 'utf-8')
开发者ID:WaBlueKey,项目名称:holbertonschool-higher_level_programming,代码行数:31,代码来源:5-main.py


注:本文中的car.Car.to_xml_node方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。