"""Convert a valid HTML document to XML
USAGE: python try_dom1.py < infile.html > outfile.xml
"""
import
sys
from
xml.dom
import
core
from
xml.dom.html_builder
import
HtmlBuilder
# Construct an HtmlBuilder object and feed the data to it
b = HtmlBuilder()
b.feed(sys.stdin.read())
# Get the newly-constructed document object
doc = b.document
# Output it as XML
print
doc.toxml()
"""Build a DOM instance from scratch, write it to XML
USAGE: python try_dom2.py > outfile.xml
"""
import
types
from
xml.dom
import
core
from
xml.dom.builder
import
Builder
# Recursive function to build DOM instance from Python instance
defobject_convert
(builder, inst):
# Put entire object inside an elem w/ same name as the class.
builder.startElement(inst.__class__.__name__)
for
attr
in
inst.__dict__.keys():
if
attr[0] ==
'_':
# Skip internal attributes
continue
value = getattr(inst, attr)
if
type(value) == types.InstanceType:
# Recursively process subobjects
object_convert(builder, value)
else
:
# Convert anything else to string, put it in an element
builder.startElement(attr)
builder.text(str(value))
builder.endElement(attr)
builder.endElement(inst.__class__.__name__)
if
__name__ ==
'__main__':
# Create container classes
classquotations
:
pass
classquotation
:
pass
# Create an instance, fill it with hierarchy of attributes
inst = quotations()
inst.title =
"Quotations file (not quotations.dtd conformant)"
inst.quot1 = quot1 = quotation()
quot1.text =
"""'"is not a quine" is not a quine' is a quine"""
quot1.source =
"Joshua Shagam, kuro5hin.org"
inst.quot2 = quot2 = quotation()
quot2.text =
"Python is not a democracy. Voting doesn't help. "+\
"Crying may..."
quot2.source =
"Guido van Rossum, comp.lang.python"
# Create the DOM Builder
builder = Builder()
object_convert(builder, inst)
print
builder.document.toxml()
"""Read in a DOM instance, convert it to a Python object
"""
from
xml.dom.utils
import
FileReader
classPyObject
:
pass
defpyobj_printer
(py_obj, level=0):
"""Return a "deep" string description of a Python object"""
from
string
import
join, split
import
types
descript =
''
for
membname
in
dir(py_obj):
member = getattr(py_obj,membname)
if
type(member) == types.InstanceType:
descript = descript + (
' '*level) +
'{'+membname+
'}\n'
descript = descript + pyobj_printer(member, level+3)
elif
type(member) == types.ListType:
descript = descript + (
' '*level) +
'['+membname+
']\n'
for
i
in
range(len(member)):
descript = descript+(
' '*level)+str(i+1)+
': '+ \
pyobj_printer(member[i],level+3)
else
:
descript = descript + membname+
'='
descript = descript + join(split(str(member)[:50]))+
'...\n'
return
descript
defpyobj_from_dom
(dom_node):
"""Converts a DOM tree to a "native" Python object"""
py_obj = PyObject()
py_obj.PCDATA =
''
for
node
in
dom_node.get_childNodes():
if
node.name ==
'#text':
py_obj.PCDATA = py_obj.PCDATA + node.value
elif
hasattr(py_obj, node.name):
getattr(py_obj, node.name).append(pyobj_from_dom(node))
else
:
setattr(py_obj, node.name, [pyobj_from_dom(node)])
return
py_obj
# Main test
dom_obj = FileReader(
"quotes.xml").document
py_obj = pyobj_from_dom(dom_obj)
if
__name__ ==
"__main__":
print
pyobj_printer(py_obj)
>>>
from
try_dom3
import
*
>>> py_obj.quotations[0].quotation[3].source[0].PCDATA
'Guido van Rossum, '
"""Manipulate the arrangement of nodes in a DOM object
"""
from
try_dom3
import
*
#-- Var 'doc' will hold the single <quotations> "trunk"
doc = dom_obj.get_childNodes()[0]
#-- Pull off all the nodes into a Python list
# (each node is a <quotation> block, or a whitespace text node)
nodes = []
while
1:
try
: node = doc.removeChild(doc.get_childNodes()[0])
except
:
break
nodes.append(node)
#-- Reverse the order of the quotations using a list method
# (we could also perform more complicated operations on the list:
# delete elements, add new ones, sort on complex criteria, etc.)
nodes.reverse()
#-- Fill 'doc' back up with our rearranged nodes
for
node
in
nodes:
# if second arg is None, insert is to end of list
doc.insertBefore(node, None)
#-- Output the manipulated DOM
print
dom_obj.toxml()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有