Пример #1
0
def createXML():
	XMLFile = open('Output.xml', 'w')
	tree = elements[idOfRootElement].createXMLNode(elements, attributes)
	soup = BeautifulStoneSoup(tree)
	XMLFile.write('<?xml version="1.0"?>')
	XMLFile.write(soup.prettify())
	XMLFile.close()
Пример #2
0
import re
from extra_lib.BeautifulSoup import BeautifulStoneSoup, Tag

f = open("./example/example.xml", "r")
xml = [l.strip() for l in f.readlines()]
xml = "".join(xml)
soup = BeautifulStoneSoup(xml)
f.close()
# Begin XML Translation Grammar
# result -> (course*)
# result = BeautifulStoneSoup('<result></result>')
# courses -> (cname, student*)
# courses = [ (c.coursename, c) for c in soup.findAll('course') ]

res = "<result>"
for c in soup.findAll("course"):
    res += "<course>"
    s = c.findPrevious("student")
    res += str(c.coursename)
    res += str(s.find("name"))
    res += str(s.grade)
    res += "</course>"
# print result.prettify()
res += "</result>"
result = BeautifulStoneSoup(res)
result.prettify()
# student -> (name, mark)
# students = [ (s.find('name'), s.grade, s) for s in [ c.findPrevious('student') for (name, c) in courses] ]