本文整理汇总了Python中xml.dom.minidom方法的典型用法代码示例。如果您正苦于以下问题:Python dom.minidom方法的具体用法?Python dom.minidom怎么用?Python dom.minidom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.dom
的用法示例。
在下文中一共展示了dom.minidom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setValue
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def setValue( self, val ):
"""Sets the text value for this instance. If it doesn't already
have a child who is of :class:`xml.dom.minidom.Text` type, then
it will add one and set the data of it to the inputed value. The
inputed value will automatically be converted to a string value to
avoid errors as well.
"""
if ( self._object ):
# find existing text node & update
for child in self._object.childNodes:
if ( isinstance( child, xml.dom.minidom.Text ) ):
child.data = unicode( val )
return True
# create new text node
text = self._document().createTextNode( unicode( val ) )
self._object.appendChild( text )
return True
return False
示例2: extract_target_from_xml
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def extract_target_from_xml(filename):
if not os.path.exists(filename):
raise IOError(filename + " not exists !")
# 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse(filename)
collection = DOMTree.documentElement
# 获取集合中所有的目标
targets = collection.getElementsByTagName("object")
res = []
for target in targets:
target_name = target.getElementsByTagName('name')[0].childNodes[0].data
bndbox = target.getElementsByTagName("bndbox")[0]
xmin = bndbox.getElementsByTagName("xmin")[0].childNodes[0].data
ymin = bndbox.getElementsByTagName("ymin")[0].childNodes[0].data
xmax = bndbox.getElementsByTagName("xmax")[0].childNodes[0].data
ymax = bndbox.getElementsByTagName("ymax")[0].childNodes[0].data
res.append([int(xmin), int(ymin), int(xmax), int(ymax), target_name])
return res
# 原始数据中多目标的显示
示例3: removeComments
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def removeComments(element):
"""
Removes comments from the element and its children.
"""
global _num_bytes_saved_in_comments
num = 0
if isinstance(element, xml.dom.minidom.Comment):
_num_bytes_saved_in_comments += len(element.data)
element.parentNode.removeChild(element)
num += 1
else:
for subelement in element.childNodes[:]:
num += removeComments(subelement)
return num
示例4: testNormalizedAfterLoad
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def testNormalizedAfterLoad(self):
"""
Introduced this test on jython because
1. Cpython guarantees, by the use of xml.dom.expatbuilder,
that all text nodes are normalized after loading.
2. Jython has no expat, and thus uses xml.dom.pulldom.parse
(which uses any java SAX2 compliant parser), and which makes
no guarantees about text node normalization.
Thus we have to check if text nodes are normalized after a parse.
See this bug for further information
minidom chunks the character input on multi-line values
http://bugs.jython.org/issue1614
"""
num_lines = 2
# Up to 16K lines should be enough to guarantee failure without normalization
while num_lines <= 2**14:
doc_content = "\n".join( ("Line %d" % i for i in xrange(num_lines)) )
doc_text = "<document>%s</document>" % doc_content
dom = parseString(doc_text)
node_content = dom.getElementsByTagName("document")[0].childNodes[0].nodeValue
self.confirm(node_content == doc_content, "testNormalizedAfterLoad")
num_lines *= 2
示例5: create_nonempty_doctype
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def create_nonempty_doctype():
doctype = getDOMImplementation().createDocumentType("doc", None, None)
doctype.entities._seq = []
doctype.notations._seq = []
notation = xml.dom.minidom.Notation("my-notation", None,
"http://xml.python.org/notations/my")
doctype.notations._seq.append(notation)
entity = xml.dom.minidom.Entity("my-entity", None,
"http://xml.python.org/entities/my",
"my-notation")
entity.version = "1.0"
entity.encoding = "utf-8"
entity.actualEncoding = "us-ascii"
doctype.entities._seq.append(entity)
return doctype
示例6: testGetElementsByTagNameNS
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def testGetElementsByTagNameNS(self):
d="""<foo xmlns:minidom='http://pyxml.sf.net/minidom'>
<minidom:myelem/>
</foo>"""
dom = parseString(d)
elems = dom.getElementsByTagNameNS("http://pyxml.sf.net/minidom",
"myelem")
self.confirm(len(elems) == 1
and elems[0].namespaceURI == "http://pyxml.sf.net/minidom"
and elems[0].localName == "myelem"
and elems[0].prefix == "minidom"
and elems[0].tagName == "minidom:myelem"
and elems[0].nodeName == "minidom:myelem")
dom.unlink()
示例7: testRenameOther
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def testRenameOther(self):
# We have to create a comment node explicitly since not all DOM
# builders used with minidom add comments to the DOM.
doc = xml.dom.minidom.getDOMImplementation().createDocument(
xml.dom.EMPTY_NAMESPACE, "e", None)
node = doc.createComment("comment")
self.assertRaises(xml.dom.NotSupportedErr, doc.renameNode, node,
xml.dom.EMPTY_NAMESPACE, "foo")
doc.unlink()
示例8: __eq__
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def __eq__( self, other ):
""" checks to see if the wrapper <xml.dom.minidom.Element> instance is the same """
result = False
if ( isinstance( other, XMLElement ) ):
result = ( self._object == other._object )
return result
示例9: __getattr__
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def __getattr__( self, key ):
""" pass along all unknown attributes to the <xml.dom.minidom.Element> class instance """
return getattr( self._object, key )
示例10: __init__
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def __init__( self, object, filename = '' ):
""" initialize the class with an <xml.dom.minidom.Element> instance """
if ( object == None ):
object = xml.dom.minidom.Element(None)
self._object = object
self.__file__ = filename
# Used to allow saving empty attributes.
self.allowEmptyAttrs = False
示例11: _document
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def _document( self ):
""" recursese up the hierarchy to find the parent who is a <xml.dom.minidom.Document> class """
out = self._object
while ( out and not isinstance( out, xml.dom.minidom.Document ) ):
out = out.parentNode
return out
示例12: _children
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def _children( self ):
""" collects the minidom child nodes which are <xml.dom.minidom.Element> types """
if ( self._object ):
return [ child for child in self._object.childNodes if isinstance( child, xml.dom.minidom.Element ) ]
return []
示例13: children
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def children( self ):
"""Collects all the child nodes of this element whose child type is an
:class:`xml.dom.minidom.Element`, wrapping each child as an
:class:`XMLElement`.
"""
if ( self._object ):
return [ XMLElement( child, self.__file__ ) for child in self._object.childNodes if isinstance( child, xml.dom.minidom.Element ) ]
return []
示例14: index
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def index( self, object ):
"""Finds the index of the inputed child object in this instance's
XMLElement children, returning -1 if it cannot be found.
"""
if ( self._object ):
if ( isinstance( object, XMLElement ) ):
if ( object._object in self._object.childNodes ):
return self._object.childNodes.index( object._object )
elif ( isinstance( object, xml.dom.minidom.Element ) ):
if ( object in self._object.childNodes ):
return self._object.childNodes.index( object )
return -1
示例15: parent
# 需要导入模块: from xml import dom [as 别名]
# 或者: from xml.dom import minidom [as 别名]
def parent(self):
if (self.parentNode and isinstance(self.parentNode, xml.dom.minidom.Element)):
return XMLElement(self.parentNode, self.__file__)
return None