def testInsertBefore(): dom = parseString("<doc><foo/></doc>") root = dom.documentElement elem = root.childNodes[0] nelem = dom.createElement("element") root.insertBefore(nelem, elem) confirm( len(root.childNodes) == 2 and root.childNodes.length == 2 and root.childNodes[0] is nelem and root.childNodes.item(0) is nelem and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.firstChild is nelem and root.lastChild is elem and root.toxml() == "<doc><element/><foo/></doc>", "testInsertBefore -- node properly placed in tree") nelem = dom.createElement("element") root.insertBefore(nelem, None) confirm( len(root.childNodes) == 3 and root.childNodes.length == 3 and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.childNodes[2] is nelem and root.childNodes.item(2) is nelem and root.lastChild is nelem and nelem.previousSibling is elem and root.toxml() == "<doc><element/><foo/><element/></doc>", "testInsertBefore -- node properly placed in tree") nelem2 = dom.createElement("bar") root.insertBefore(nelem2, nelem) confirm( len(root.childNodes) == 4 and root.childNodes.length == 4 and root.childNodes[2] is nelem2 and root.childNodes.item(2) is nelem2 and root.childNodes[3] is nelem and root.childNodes.item(3) is nelem and nelem2.nextSibling is nelem and nelem.previousSibling is nelem2 and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>", "testInsertBefore -- node properly placed in tree") dom.unlink()
def generate_xml_from_dict(input_dict, xml_file): impl = xml.dom.getDOMImplementation() dom = impl.createDocument(None, 'root', None) root = dom.documentElement instance = dom.createElement('instance') root.appendChild(instance) for (key, value) in sorted(input_dict.items()): nameE = dom.createElement(key) nameT = dom.createTextNode(str(value)) nameE.appendChild(nameT) instance.appendChild(nameE) with open(xml_file, 'w', encoding='utf-8') as tp: dom.writexml(tp, addindent=' ', newl='\n', encoding='utf-8')
def _testElementReprAndStrUnicode(): dom = Document() el = dom.appendChild(dom.createElement(u"abc")) string1 = repr(el) string2 = str(el) confirm(string1 == string2) dom.unlink()
def testLegalChildren(): dom = Document() elem = dom.createElement('element') text = dom.createTextNode('text') try: dom.appendChild(text) except xml.dom.HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" dom.appendChild(elem) try: dom.insertBefore(text, elem) except xml.dom.HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" try: dom.replaceChild(text, elem) except xml.dom.HierarchyRequestErr: pass else: print "dom.appendChild didn't raise HierarchyRequestErr" nodemap = elem.attributes try: nodemap.setNamedItem(text) except xml.dom.HierarchyRequestErr: pass else: print "NamedNodeMap.setNamedItem didn't raise HierarchyRequestErr" try: nodemap.setNamedItemNS(text) except xml.dom.HierarchyRequestErr: pass else: print "NamedNodeMap.setNamedItemNS didn't raise HierarchyRequestErr" elem.appendChild(text) dom.unlink()
def add_wl_links(paragraph, text): words = text.split() punctuation = '.,!"\'' current = [] for i, word in enumerate(words): if not word.startswith('wl_'): current += [word] continue ending = '' if not word[-1].isalpha() and word[-1] in punctuation: ending = word[-1] word = word[:-1] t = protocol.lookup(word) if t is None: current += [word + ending] else: add_text_node(p, current, len(current) > 0) current = [] a = dom.createElement('a') a.setAttribute('href', t.get_url()) add_text_node(a, [word]) p.appendChild(a) current += [ending] add_text_node(p, current)
def testDeleteAttr(): dom = Document() child = dom.appendChild(dom.createElement("abc")) confirm(len(child.attributes) == 0) child.setAttribute("def", "ghi") confirm(len(child.attributes) == 1) del child.attributes["def"] confirm(len(child.attributes) == 0) dom.unlink()
def testRemoveAttr(): dom = Document() child = dom.appendChild(dom.createElement("abc")) child.setAttribute("def", "ghi") confirm(len(child.attributes) == 1) child.removeAttribute("def") confirm(len(child.attributes) == 0) dom.unlink()
def testRemoveAttributeNode(): dom = Document() child = dom.appendChild(dom.createElement("foo")) child.setAttribute("spam", "jam") confirm(len(child.attributes) == 1) node = child.getAttributeNode("spam") child.removeAttributeNode(node) confirm(len(child.attributes) == 0 and child.getAttributeNode("spam") is None) dom.unlink()
def testRemoveAttributeNode(): dom = Document() child = dom.appendChild(dom.createElement("foo")) child.setAttribute("spam", "jam") confirm(len(child.attributes) == 1) node = child.getAttributeNode("spam") child.removeAttributeNode(node) confirm( len(child.attributes) == 0 and child.getAttributeNode("spam") is None) dom.unlink()
def testInsertBefore(): dom = parseString("<doc><foo/></doc>") root = dom.documentElement elem = root.childNodes[0] nelem = dom.createElement("element") root.insertBefore(nelem, elem) confirm(len(root.childNodes) == 2 and root.childNodes.length == 2 and root.childNodes[0] is nelem and root.childNodes.item(0) is nelem and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.firstChild is nelem and root.lastChild is elem and root.toxml() == "<doc><element/><foo/></doc>" , "testInsertBefore -- node properly placed in tree") nelem = dom.createElement("element") root.insertBefore(nelem, None) confirm(len(root.childNodes) == 3 and root.childNodes.length == 3 and root.childNodes[1] is elem and root.childNodes.item(1) is elem and root.childNodes[2] is nelem and root.childNodes.item(2) is nelem and root.lastChild is nelem and nelem.previousSibling is elem and root.toxml() == "<doc><element/><foo/><element/></doc>" , "testInsertBefore -- node properly placed in tree") nelem2 = dom.createElement("bar") root.insertBefore(nelem2, nelem) confirm(len(root.childNodes) == 4 and root.childNodes.length == 4 and root.childNodes[2] is nelem2 and root.childNodes.item(2) is nelem2 and root.childNodes[3] is nelem and root.childNodes.item(3) is nelem and nelem2.nextSibling is nelem and nelem.previousSibling is nelem2 and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>" , "testInsertBefore -- node properly placed in tree") dom.unlink()
def makeEasyTag(dom, tagname, value, type='text'): tag = dom.createElement(tagname) if value.find(']]>') > -1: type = 'text' if type == 'text': value = value.replace('&', '&') value = value.replace('<', '<') text = dom.createTextNode(value) elif type == 'cdata': text = dom.createCDATASection(value) tag.appendChild(text) return tag
def testNamedNodeMapSetItem(): dom = Document() elem = dom.createElement('element') attrs = elem.attributes attrs["foo"] = "bar" a = attrs.item(0) confirm(a.ownerDocument is dom, "NamedNodeMap.__setitem__() sets ownerDocument") confirm(a.ownerElement is elem, "NamedNodeMap.__setitem__() sets ownerElement") confirm(a.value == "bar", "NamedNodeMap.__setitem__() sets value") confirm(a.nodeValue == "bar", "NamedNodeMap.__setitem__() sets nodeValue") elem.unlink() dom.unlink()
def construct_node(dom, styles): styles = styles[:] styles.reverse() root = None last = None for s in styles: n = dom.createElement(s['tag']) n.attributes = s['attributes'] if not root: root = n if last: last.appendChild(n) last = n return root, n
def admin_export_all_compos(request): if not request.user.has_perm("compo.admin"): return HttpResponseNotFound() party = get_party(request) dom = xml.dom.minidom.getDOMImplementation().createDocument(None, "tag", None) compos = dom.createElement("compos") for compo in Competition.objects.filter(party=party): compos.appendChild(compo.export_xml(compo.results_public or request.user.has_perm("compo.count_votes"))) xmldata = generate_xml(compos) resp = HttpResponse(content_type="text/xml") resp["Content-Disposition"] = 'attachment; filename="%s_compos_all_export.xml"' % party.slug.lower() resp.write(xmldata) return resp
def testAddAttr(): dom = Document() child = dom.appendChild(dom.createElement("abc")) child.setAttribute("def", "ghi") confirm(child.getAttribute("def") == "ghi") confirm(child.attributes["def"].value == "ghi") child.setAttribute("jkl", "mno") confirm(child.getAttribute("jkl") == "mno") confirm(child.attributes["jkl"].value == "mno") confirm(len(child.attributes) == 2) child.setAttribute("def", "newval") confirm(child.getAttribute("def") == "newval") confirm(child.attributes["def"].value == "newval") confirm(len(child.attributes) == 2) dom.unlink()
def find_own_dom_element(self): """find_own_dom_element() -> (DOM, Node) Opens the startup DOM, looks for the element that belongs to the package, and returns DOM and node. Creates a new one if element is not there. """ dom = get_vistrails_application().vistrailsStartup.startup_dom() doc = dom.documentElement packages = enter_named_element(doc, 'packages') for package_node in named_elements(packages, 'package'): if str(package_node.attributes['name'].value) == self.codepath: return (dom, package_node) # didn't find anything, create a new node package_node = dom.createElement("package") package_node.setAttribute('name', self.codepath) packages.appendChild(package_node) get_vistrails_application().vistrailsStartup.write_startup_dom(dom) return (dom, package_node)
def testUserData(): dom = Document() n = dom.createElement('e') confirm(n.getUserData("foo") is None) n.setUserData("foo", None, None) confirm(n.getUserData("foo") is None) n.setUserData("foo", 12, 12) n.setUserData("bar", 13, 13) confirm(n.getUserData("foo") == 12) confirm(n.getUserData("bar") == 13) n.setUserData("foo", None, None) confirm(n.getUserData("foo") is None) confirm(n.getUserData("bar") == 13) handler = UserDataHandler() n.setUserData("bar", 12, handler) c = n.cloneNode(1) confirm(handler.called and n.getUserData("bar") is None and c.getUserData("bar") == 13) n.unlink() c.unlink() dom.unlink()
def _get_package_node(self, dom, create): doc = dom.documentElement packages = enter_named_element(doc, 'packages') for package_node in named_elements(packages, 'package'): if package_node.attributes['name'].value == self.codepath: return package_node, 'enabled' oldpackages = enter_named_element(doc, 'disabledpackages') for package_node in named_elements(oldpackages, 'package'): if package_node.attributes['name'].value == self.codepath: return package_node, 'disabled' if create is None: return None, None else: package_node = dom.createElement('package') package_node.setAttribute('name', self.codepath) if create == 'enabled': packages.appendChild(package_node) elif create == 'disabled': oldpackages.appendChild(package_node) else: raise ValueError get_vistrails_application().vistrailsStartup.write_startup_dom(dom) return package_node, create
def admin_export_xml(request, compo): if not request.user.has_perm("compo.admin"): return HttpResponseNotFound() party = get_party(request) try: try: compo = Competition.objects.get(slug=compo, party=party) except Competition.DoesNotExist: raise InvalidCompetition() except InvalidCompetition: messages.add_message(request, messages.ERROR, "Invalid competition") return HttpResponseRedirect(reverse("compo.views.index", args=[request.party])) dom = xml.dom.minidom.getDOMImplementation().createDocument(None, "tag", None) compos = dom.createElement("compos") compos.appendChild(compo.export_xml(compo.results_public or request.user.has_perm("compo.count_votes"))) xmldata = generate_xml(compos) resp = HttpResponse(content_type="text/xml") resp["Content-Disposition"] = 'attachment; filename="%s_%s_export.xml"' % (party.slug, compo.name.lower()) resp.write(xmldata) return resp
continue id = g.getAttribute("id") if id: if id.find("xpin") >= 0: g.setAttribute("id", id.replace("xpin", "pin")) elif id.find("pinx") >= 0: g.setAttribute("id", id.replace("pinx", "pin")) print g.getAttribute("id") p1.parentNode.removeChild(p1) p2.parentNode.removeChild(p2) c1.parentNode.removeChild(c1) np1 = dom.createElement("path") g.appendChild(np1) np1.setAttribute("fill", p1.getAttribute("fill")) r = float(c1.getAttribute("r")) cx = float(c1.getAttribute("cx")) cy = float(c1.getAttribute("cy")) np1.setAttribute("d", ''.join(["M", `cx - r - r`, ",", `cy`, " a",`r * 2`, ",", `r * 2`, " ", `0`, " ", `0`, " ", `1`, " ", `r * 4`, ",", `0`])) np2 = dom.createElement("path") g.appendChild(np2) np2.setAttribute("fill", p2.getAttribute("fill")) np2.setAttribute("d", ''.join(["M", `cx + r + r`, ",", `cy`, " a",`r * 2`, ",", `r * 2`, " ", `0`, " ", `1`, " ", `1`, " ", `-r * 4`, ",", `0`]))
domTagList = domTAG.getElementsByTagName('tag') domEpisodeList = dom.getElementsByTagName('number') property = 'west germany' tagName = 'country' propertyTags = [ tag for tag in domTagList if tag.firstChild.nodeValue == property ] parentOfTags = [tag.parentNode for tag in propertyTags] qualMovies = [movie.firstChild.firstChild.nodeValue for movie in parentOfTags] for movie in qualMovies: colorEpisode = [ episode.parentNode for episode in domEpisodeList if episode.firstChild.nodeValue == movie ] episodeNode = colorEpisode[0] newElement = dom.createElement(tagName) newText = dom.createTextNode(property) newElement.appendChild(newText) genreElements = episodeNode.getElementsByTagName('genre') episodeNode.insertBefore(newElement, genreElements[0]) with open('episodelist-out.xml', 'w', encoding='utf-8') as f: f.write(dom.toxml('utf-8').decode('utf-8'))
def admin_export_xsl(request, compo): if not request.user.has_perm("compo.admin"): return HttpResponseNotFound() party = get_party(request) try: try: compo = Competition.objects.get(slug=compo, party=party) except Competition.DoesNotExist: raise InvalidCompetition() except InvalidCompetition: messages.add_message(request, messages.ERROR, "Invalid competition") return HttpResponseRedirect(reverse("compo.views.index", args=[request.party])) if request.method == "POST": dom = xml.dom.minidom.getDOMImplementation().createDocument(None, "tag", None) compos = dom.createElement("compos") compos.appendChild(compo.export_xml(compo.results_public or request.user.has_perm("compo.count_votes"))) xmldata = generate_xml(compos) xsl = "" uploadedFile = request.FILES["xslpacket"] for c in uploadedFile.chunks(): xsl += c result = transform_xsl(xsl, xmldata) output_type = request.POST.get("output_type", "txt") if output_type == "txt": return HttpResponse(result.encode("utf-8"), mimetype="text/plain") elif output_type == "diploma": tmpxsl = tempfile.NamedTemporaryFile(delete=False) tmpxsl.write(xsl) tmpxsl.close() tmpdata = tempfile.NamedTemporaryFile(delete=False) tmpdata.write(result.encode("utf-8")) tmpdata.close() tmpout, tmpoutname = tempfile.mkstemp() tmpout.close() retcode = subprocess.call(["fop", "-xml", tmpdata.name, "-xsl", tmpxsl.name, "-pdf", tmpoutname]) os.unlink(tmpxsl.name) os.unlink(tmpdata.name) if retcode == 0: outfile = open(tmpoutname) resp = HttpResponse(content_type="application/pdf") resp["Content-Disposition"] = 'attachment; filename="%s_%s_diploma_export.pdf"' % ( party.slug, compo.name.lower(), ) resp.write(outfile.read()) outfile.close() os.unlink(tmpoutname) return resp elif output_type == "impress": odpfile = tempfile.NamedTemporaryFile(delete=False) for c in request.FILES["odppacket"].chunks(): odpfile.write(c) # odpfile.close() archive = zipfile.ZipFile(odpfile, "a") archive.writestr("content.xml", result.encode("utf-8")) archive.close() result = open(odpfile.name, "r") resp = HttpResponse(content_type="application/octet-stream") resp["Content-Disposition"] = 'attachment; filename="%s_%s_export.odp"' % (party.slug, compo.name.lower()) resp.write(result.read()) return resp return HttpResponseNotFound()
def testAttributeRepr(): dom = Document() el = dom.appendChild(dom.createElement(u"abc")) node = el.setAttribute("abc", "def") confirm(str(node) == repr(node)) dom.unlink()
thesis = {} # for each column, make the column name the key and the column # content the value of a new thesis dictionary entry for i in range(len(tags)): thesis[tags[i]] = row[i].decode('utf-8') theses.append(thesis) rowNum +=1 for thesis in theses: #if unititle year equals thesis year: for sub in subseries: if sub.getElementsByTagName("unittitle")[0].firstChild.nodeValue == thesis["unitdate"]: #create c with refid and level <c id="{refid}" level="item"> c = dom.createElement("c") c.attributes["id"] = thesis["refid"] c.attributes["level"] = "item" sub.appendChild(c) #create did did = dom.createElement("did") c.appendChild(did) #create unittitle tag unittitle = dom.createElement("unittitle") did.appendChild(unittitle) unittitle.appendChild(dom.createTextNode(thesis["unittitle"])) #create langmaterial tag langmaterial = dom.createElement("langmaterial") did.appendChild(langmaterial) language = dom.createElement("language") language.attributes["langcode"] = "eng"
infile = open(os.path.join(root, filename), "r") content = infile.read() infile.close() try: dom = xml.dom.minidom.parseString(content) except xml.parsers.expat.ExpatError, err: print str(err), fzpFilename continue # print "testing", fzpFilename fzp = dom.documentElement tags = None tagsNodes = fzp.getElementsByTagName("tags") if tagsNodes.length == 0: tags = dom.createElement("tags") fzp.insertBefore(tags, fzp.firstChild) elif tagsNodes.length == 1: tags = tagsNodes.item(0) else: print "multiple tags elements in", fzpFilename continue # print "got properties", properties tagNodes = tags.getElementsByTagName("tag") smd = None for tag in tagNodes: tag.normalize() # make sure all the text is in one node t = None for child in tag.childNodes:
for filename in files: if not filename.endswith(".fzp"): continue fzpFilename = os.path.join(root, filename) try: dom = xml.dom.minidom.parse(fzpFilename) except xml.parsers.expat.ExpatError, err: print str(err), fzpFilename continue fzp = dom.documentElement versionNodes = fzp.getElementsByTagName("version") version = None if (versionNodes.length == 0): version = dom.createElement("version") fzp.insertBefore(version, fzp.firstChild) elif (versionNodes.length == 1): txt = "" version = versionNodes.item(0) else: print "multiple version elements in", fzpFilename continue while version.hasChildNodes(): oldChild = version.firstChild version.removeChild(oldChild) oldChild.unlink() vtext = dom.createTextNode("4") version.appendChild(vtext)
continue id = g.getAttribute("id") if id: if id.find("xpin") >= 0: g.setAttribute("id", id.replace("xpin", "pin")) elif id.find("pinx") >= 0: g.setAttribute("id", id.replace("pinx", "pin")) print g.getAttribute("id") p1.parentNode.removeChild(p1) p2.parentNode.removeChild(p2) c1.parentNode.removeChild(c1) np1 = dom.createElement("path") g.appendChild(np1) np1.setAttribute("fill", p1.getAttribute("fill")) r = float(c1.getAttribute("r")) cx = float(c1.getAttribute("cx")) cy = float(c1.getAttribute("cy")) np1.setAttribute( "d", ''.join([ "M", ` cx - r - r `, ",", ` cy `, " a", ` r * 2 `, ",", ` r * 2 `, " ", ` 0 `, " ", ` 0 `, " ", ` 1 `, " ", ` r * 4 `, ",", ` 0 ` ])) np2 = dom.createElement("path")
infile = open(os.path.join(root, filename), "r") content = infile.read() infile.close() try: dom = xml.dom.minidom.parseString(content) except xml.parsers.expat.ExpatError, err: print str(err), fzpFilename continue #print "testing", fzpFilename fzp = dom.documentElement properties = None propertiesNodes = fzp.getElementsByTagName("properties") if (propertiesNodes.length == 0): properties = dom.createElement("properties") fzp.insertBefore(properties, fzp.firstChild) elif (propertiesNodes.length == 1): properties = propertiesNodes.item(0) else: print "multiple properties elements in", fzpFilename continue #print "got properties", properties propertyNodes = properties.getElementsByTagName("property") package = None for property in propertyNodes: name = property.getAttribute("name") #print "got name", name
def testElement(): dom = Document() dom.appendChild(dom.createElement("abc")) confirm(dom.documentElement) dom.unlink()
def convert_to_html(self, node): protocol = self.get_protocol() dom = protocol.document lines = [s.strip() for s in getText(node).split('\n')] # get rid of rubbish at beginning and end while not lines[0]: del lines[0] while not lines[-1]: del lines[-1] # split lines by empty elements paragraphs = [list(group) for k, group in itertools.groupby(lines, bool) if k] # remove old text node node.removeChild(node.childNodes[0]) def add_text_node(element, words, padding=False): if not words: return if padding: words += ' ' text = ' '.join(words) t = dom.createTextNode(text) element.appendChild(t) # add anchor tags for stuff we can link to def add_wl_links(paragraph, text): words = text.split() punctuation = '.,!"\'' current = [] for i, word in enumerate(words): if not word.startswith('wl_'): current += [word] continue ending = '' if not word[-1].isalpha() and word[-1] in punctuation: ending = word[-1] word = word[:-1] t = protocol.lookup(word) if t is None: current += [word + ending] else: add_text_node(p, current, len(current) > 0) current = [] a = dom.createElement('a') a.setAttribute('href', t.get_url()) add_text_node(a, [word]) p.appendChild(a) current += [ending] add_text_node(p, current) # add new paragraph nodes for text in [' '.join(x) for x in paragraphs]: p = dom.createElement('p') add_wl_links(p, text) node.appendChild(p) return node.toxml().encode('ascii', 'xmlcharrefreplace')
copper0 = None for g in gNodes: if g.getAttribute("id") == "copper1": copper1 = g if g.getAttribute("id") == "copper0": copper0 = g if copper1: continue if not copper0: print "copper0 not found in", svgFilename continue print "adding copper1 to", svgFilename copper1 = dom.createElement("g") copper1.setAttribute("id", "copper1") copper0.parentNode.insertBefore(copper1, copper0) copper1.appendChild(copper0) outfile = open(svgFilename, 'wb') s = dom.toxml("UTF-8") # s = re.sub('\s*\n\s*\n', '', s) # ghosts of removed (and unlinked) nodes seem to generate newlines, so tighten up the xml outfile.write(s) outfile.flush() outfile.close() if __name__ == "__main__": main()
needsCopper1 = 0 for pcbView in pcbViewNodes: layersNodes = pcbView.getElementsByTagName("layers") if layersNodes.length == 1: viewsLayers = layersNodes.item(0) copper0 = None copper1 = None for layer in viewsLayers.getElementsByTagName("layer"): if layer.getAttribute("layerId") == "copper1": copper1 = layer elif layer.getAttribute("layerId") == "copper0": copper0 = layer if not copper1 and copper0: needsCopper1 = 1 copper1 = dom.createElement("layer") copper1.setAttribute("layerId", "copper1") viewsLayers.appendChild(copper1) else: break else: connectorsView = pcbView copper1 = None copper0 = None for p in connectorsView.getElementsByTagName("p"): if p.getAttribute("layer") == "copper1": copper1 = p elif p.getAttribute("layer") == "copper0": copper0 = p
infile = open(os.path.join(root, filename), "r") content = infile.read() infile.close() try: dom = xml.dom.minidom.parseString(content) except xml.parsers.expat.ExpatError, err: print str(err), fzpFilename continue #print "testing", fzpFilename fzp = dom.documentElement tags = None tagsNodes = fzp.getElementsByTagName("tags") if (tagsNodes.length == 0): tags = dom.createElement("tags") fzp.insertBefore(tags, fzp.firstChild) elif (tagsNodes.length == 1): tags = tagsNodes.item(0) else: print "multiple tags elements in", fzpFilename continue #print "got properties", properties tagNodes = tags.getElementsByTagName("tag") smd = None for tag in tagNodes: tag.normalize() #make sure all the text is in one node t = None for child in tag.childNodes: