Python xml.dom.minidom() Examples

The following are 30 code examples of xml.dom.minidom(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.dom , or try the search function .
Example #1
Source File: test_minidom.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: test_minidom.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #3
Source File: tools.py    From DL.EyeSight with GNU General Public License v3.0 6 votes vote down vote up
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

# 原始数据中多目标的显示 
Example #4
Source File: scour.py    From beampy with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #5
Source File: xmlelement.py    From cross3d with MIT License 6 votes vote down vote up
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 
Example #6
Source File: Utility.py    From p2pool-n with GNU General Public License v3.0 5 votes vote down vote up
def values(self):
        return self.list



# This is a runtime guerilla patch for pulldom (used by minidom) so
# that xml namespace declaration attributes are not lost in parsing.
# We need them to do correct QName linking for XML Schema and WSDL.
# The patch has been submitted to SF for the next Python version. 
Example #7
Source File: soap.py    From ChatLearner with Apache License 2.0 5 votes vote down vote up
def getBody(self):
		""" Return the child elements of Body element 
		    getBody() return a list with xml.dom.minidom.Element objects
		"""
		return self._body.childNodes 
Example #8
Source File: soap.py    From ChatLearner with Apache License 2.0 5 votes vote down vote up
def setHeader(self, header):
		""" Set the child content to Header element
		    setHeader(header), header is a xml.dom.minidom.Document object
		 """
		if isinstance(header,xml.dom.minidom.Document):
			self._header.appendChild(header.documentElement)
		elif isinstance(header,xml.dom.minidom.Element):
			self._header.appendChild(header) 
Example #9
Source File: test_minidom.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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 
Example #10
Source File: test_minidom.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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() 
Example #11
Source File: test_minidom.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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() 
Example #12
Source File: xmlreport.py    From coveragepy with Apache License 2.0 5 votes vote down vote up
def serialize_xml(dom):
    """Serialize a minidom node to XML."""
    out = dom.toprettyxml()
    if env.PY2:
        out = out.encode("utf8")
    return out 
Example #13
Source File: test_minidom.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #14
Source File: test_minidom.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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() 
Example #15
Source File: test_minidom.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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() 
Example #16
Source File: Utility.py    From p2pool-n with GNU General Public License v3.0 5 votes vote down vote up
def createDocument(self, nsuri, qname, doctype=None):
        """Create a new writable DOM document object."""
        impl = xml.dom.minidom.getDOMImplementation()
        return impl.createDocument(nsuri, qname, doctype) 
Example #17
Source File: Utility.py    From p2pool-n with GNU General Public License v3.0 5 votes vote down vote up
def loadDocument(self, data):
        """Load an xml file from a file-like object and return a DOM
           document instance."""
        return xml.dom.minidom.parse(data) 
Example #18
Source File: soap.py    From ChatLearner with Apache License 2.0 5 votes vote down vote up
def getHeader(self):
		""" Return the child elements of Header element 
		    getHeader() return a list with xml.dom.minidom.Element objects
		"""
		return self._header.childNodes 
Example #19
Source File: test_minidom.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #20
Source File: test_minidom.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #21
Source File: test_minidom.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #22
Source File: scour.py    From beampy with GNU General Public License v3.0 5 votes vote down vote up
def scourXmlFile(filename, options=None):
    # sanitize options (take missing attributes from defaults, discard unknown attributes)
    options = sanitizeOptions(options)
    # we need to make sure infilename is set correctly (otherwise relative references in the SVG won't work)
    options.ensure_value("infilename", filename)

    # open the file and scour it
    with open(filename, "rb") as f:
        in_string = f.read()
    out_string = scourString(in_string, options)

    # prepare the output xml.dom.minidom object
    doc = xml.dom.minidom.parseString(out_string.encode('utf-8'))

    # since minidom does not seem to parse DTDs properly
    # manually declare all attributes with name "id" to be of type ID
    # (otherwise things like doc.getElementById() won't work)
    all_nodes = doc.getElementsByTagName("*")
    for node in all_nodes:
        try:
            node.setIdAttribute('id')
        except NotFoundErr:
            pass

    return doc


# GZ: Seems most other commandline tools don't do this, is it really wanted? 
Example #23
Source File: test_minidom.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #24
Source File: test_minidom.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #25
Source File: test_minidom.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #26
Source File: test_minidom.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #27
Source File: test_minidom.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #28
Source File: test_minidom.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
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() 
Example #29
Source File: xmlelement.py    From cross3d with MIT License 5 votes vote down vote up
def __getattr__( self, key ):
		""" pass along all unknown attributes to the <xml.dom.minidom.Element> class instance """
		return getattr( self._object, key ) 
Example #30
Source File: test_minidom.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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()