Hi, I am trying to parse multiple xml files with sax using python but i for some reason i am having an issue: if someone can just help me get through this part, I would greatly appreciate it. It is parsing to get the attributes amount,unit and the ingredient name. here is my xml file, its small.
<?xml version="1.0" encoding="ASCII" standalone="yes"?>
<recipe name="bread" prep_time="5 mins" cook_time="3 hours">
<title>Basic bread</title>
<ingredient amount="3" unit="cups">Flour</ingredient>
<ingredient amount="0.25" unit="ounce">Yeast</ingredient>
<ingredient amount="1.5" unit="cups" state="warm">Water</ingredient>
<ingredient amount="1" unit="teaspoon">Salt</ingredient>
<instructions>
<step step_id="1">Mix all ingredients together, and knead thoroughly.</step>
<step step_id="2">Cover with a cloth, and leave for one hour in warm room.</step>
<step step_id="3">Knead again, place in a tin, and then bake in the oven.</step>
</instructions>
</recipe>
and here is my python code... COMMAND USAGE: python file.py some1.xml some2.xml (many xml files)
import sys, string
from xml.sax import handler, make_parser
from xml.sax.handler import feature_namespaces
from xml.sax import saxutils
class RecipeHandler(saxutils.DefaultHandler):
def __init__(self):
self.args = sys.argv[1:]
#self.outfile = outfile
# self.level = 0
self.inIngredient = 0
#self.interestData = []
self.ingredientList = []
def get_ingredientList(self):
for archivo in self.args:
return self.ingredientList
def set_ingredientList(self, ingredientList):
for archivo in self.args:
self.ingredientList = ingredientList
def startElement(self, name, attrs):
# self.level += 1
for archivo in self.args:
if name == 'ingredient':
key=attrs.get("name")
amount=attrs.get("amount")
unit=attrs.get("unit")
self.ingredientList=[]
listing=self.ingredientList.append(key,unit,amount)
print listing
class test(inFile):
outFile=sys.stdout
parser=make_parser()
parser.setFeature(feature_namespace,0)
defaultHandler=RecipeHandler(outFile)
parser.setContentHandler(defaultHandler)
inFile=open(inFileName, 'r')
parser.parse(inFile)
inFile.close()
ingredientsList=defaultHandler.get_ingredientList()
print 'Ingredients:'
for archivos in outFile:
if ingredients in ingredientLust:
print (ingredient, )
if __name__ == '__main__':
print 'usage: python file.py file.xml file2.xml'
args=sys.argv[1:]
test(args)
THANKS FOR YOUR HELP ANYONE! IF ANYONE CAN JUST POINT ME TO THE RIGHT DIRECTION WITH PSUEDOCODE THAT WILL BE GREAT!
:mrgreen: