Python lxml.etree.ElementTree() Examples
The following are 30
code examples of lxml.etree.ElementTree().
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
lxml.etree
, or try the search function
.
Example #1
Source File: xmldiff.py From naoqi_bridge with BSD 3-Clause "New" or "Revised" License | 7 votes |
def sortFile(fileobj): with open(fileobj['filename'], 'r') as original: # parse the XML file and get a pointer to the top xmldoc = le.parse(original) xmlroot = xmldoc.getroot() # create a new XML element that will be the top of # the sorted copy of the XML file newxmlroot = le.Element(xmlroot.tag) # create the sorted copy of the XML file sortAttrs(xmlroot, newxmlroot) sortElements(list(xmlroot), newxmlroot) # write the sorted XML file to the temp file newtree = le.ElementTree(newxmlroot) with open(fileobj['tmpfilename'], 'wb') as newfile: newtree.write(newfile, pretty_print=True) # # sort each of the specified files
Example #2
Source File: sim_xml.py From simLAB with GNU General Public License v2.0 | 6 votes |
def getPathFromFile(self, file): if file == None: return pathXml = etree.ElementTree(self.root).getpath(file) pathXml = pathXml.split("mf")[1] path = "./mf[@id='3F00']" for _file in pathXml.split('/'): if not _file: #path = types.addToPath(path, "/") continue absPath = types.addToPath(path, _file) id = etree.ElementTree(self.root).xpath(absPath)[0].attrib['id'] #"./mf/df[@id='ADF0']" fileId = "%s[@id='%s']" %(_file.split('[')[0], id) path = types.addToPath(path, fileId) return path
Example #3
Source File: phoneset2ipamap.py From Living-Audio-Dataset with Apache License 2.0 | 6 votes |
def create_output(phone_map,output_file,nuclei=[],add_nuclei=False): root = ET.Element("ipa_mapping") tree = ET.ElementTree(root) for ipa, phone in phone_map.items(): mapping = ET.SubElement(root, "map") mapping.attrib["pron"] = phone mapping.attrib["ipa"] = ipa if add_nuclei: if phone in nuclei: mapping.attrib["nucleus"] = "true" else: mapping.attrib["nucleus"] = "false" mapping_output = open(output_file,"wb") tree.write( mapping_output,pretty_print=True, xml_declaration=True,encoding="utf-8")
Example #4
Source File: wiktionary2lex.py From Living-Audio-Dataset with Apache License 2.0 | 6 votes |
def output2lex(output,filename): lexicon_output = open(filename,"wb") lexicon = ET.Element("lexicon") tree = ET.ElementTree(lexicon) for list_item in output: lex_entry = ET.SubElement(lexicon,"lex") if list_item["pron"] is not None: lex_entry.attrib["ipa"]=list_item["pron"] if list_item["x-sampa"] is not None: lex_entry.attrib["x-sampa"]=list_item["x-sampa"] lex_entry.text=list_item["word"] tree.write( lexicon_output,pretty_print=True, xml_declaration=True,encoding="utf-8")
Example #5
Source File: io.py From cvcalib with Apache License 2.0 | 6 votes |
def save_opencv_xml_file(path, xml_generator): """ Save something in opencv's XML format @param path: path where to save the file @param xml_generator: function that accepts an LXML root element as a parameter and generates all the necessary XML to go in the file """ root = etree.Element("opencv_storage") xml_generator(root) et = etree.ElementTree(root) with open(path, 'wb') as f: et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True) # little hack necessary to replace the single quotes (that OpenCV doesn't like) with double quotes s = open(path).read() s = s.replace("'", "\"") with open(path, 'w') as f: f.write(s) f.flush()
Example #6
Source File: pyra.py From arya with Apache License 2.0 | 6 votes |
def main(): """Use locals() to create a PyraTree, then get JSON/XML representations of the PyraTree data and print them.""" # Put Arya-generated code here polUni = cobra.model.pol.Uni('') fvTenant = cobra.model.fv.Tenant(polUni, 'pod1') # Create the tree tree = PyraTree('polUni', locals()) # Acquire a copy of the tree to use, e.g to save/modify/post. dict_tree = tree.dtree xml_tree = tree.etree json_tree = tree.jtree # Example output print(ElementTree.tostring(xml_tree, encoding='utf-8')) print(json.dumps(json_tree, indent=4, sort_keys=True))
Example #7
Source File: urdf.py From scikit-robot with MIT License | 6 votes |
def save(self, file_obj): """Save this URDF to a file. Parameters ---------- file_obj : str or file-like object The file to save the URDF to. Should be the path to the ``.urdf`` XML file. Any paths in the URDF should be specified as relative paths to the ``.urdf`` file instead of as ROS resources. Returns ------- urdf : :class:`.URDF` The parsed URDF. """ if isinstance(file_obj, six.string_types): path, _ = os.path.split(file_obj) else: path, _ = os.path.split(os.path.realpath(file_obj.name)) node = self._to_xml(None, path) tree = ET.ElementTree(node) tree.write(file_obj, pretty_print=True, xml_declaration=True, encoding='utf-8')
Example #8
Source File: ocrd_xml_base.py From core with Apache License 2.0 | 6 votes |
def __init__(self, filename=None, content=None): """ Args: filename (string): content (string): """ # print(self, filename, content) if filename is None and content is None: raise Exception("Must pass 'filename' or 'content' to " + self.__class__.__name__) elif content: self._tree = ET.ElementTree(ET.XML(content, parser=ET.XMLParser(encoding='utf-8'))) else: self._tree = ET.ElementTree() filename = filename.replace('file://', '') if not exists(filename): raise Exception('File does not exist: %s' % filename) self._tree.parse(filename)
Example #9
Source File: teamofrivals.py From psychsim with MIT License | 6 votes |
def mapSave(regions,filename): """ Saves a region map to an XML file """ root = ET.Element('map') for name,table in regions.items(): node = ET.SubElement(root,'region') node.set('name',name) if table.has_key('value'): node.set('value',str(table['value'])) if table.has_key('occupants'): node.set('occupants',str(table['occupants'])) node.set('owner',str(table['owner'])) for neighbor in table['neighbors']: subnode = ET.SubElement(node,'neighbor') subnode.set('name',neighbor) tree = ET.ElementTree(root) tree.write(filename,pretty_print=True) return tree
Example #10
Source File: client.py From pyvas with MIT License | 6 votes |
def _send_request(self, request): """Send XML data to OpenVAS Manager and get results""" block_size = 1024 if etree.iselement(request): root = etree.ElementTree(request) root.write(self.socket, encoding="utf-8") else: if isinstance(request, six.text_type): request = request.encode("utf-8") self.socket.send(request) parser = etree.XMLTreeBuilder() while True: response = self.socket.recv(block_size) parser.feed(response) if len(response) < block_size: break root = parser.close() return root
Example #11
Source File: xmi.py From dkpro-cassis with Apache License 2.0 | 6 votes |
def serialize(self, sink: Union[IO, str], cas: Cas, pretty_print=True): xmi_attrs = {"{http://www.omg.org/XMI}version": "2.0"} root = etree.Element(etree.QName(self._nsmap["xmi"], "XMI"), nsmap=self._nsmap, **xmi_attrs) self._serialize_cas_null(root) # Find all fs, even the ones that are not directly added to a sofa for fs in sorted(cas._find_all_fs(), key=lambda a: a.xmiID): self._serialize_feature_structure(cas, root, fs) for sofa in cas.sofas: self._serialize_sofa(root, sofa) for view in cas.views: self._serialize_view(root, view) doc = etree.ElementTree(root) etree.cleanup_namespaces(doc, top_nsmap=self._nsmap) doc.write(sink, xml_declaration=True, pretty_print=pretty_print)
Example #12
Source File: spider.py From etlpy with Apache License 2.0 | 6 votes |
def CrawData(self, url): if self.Login !="" and self.haslogin == False: self.HttpItem.opener = self.autologin(self.Login); self.haslogin = True; html = self.HttpItem.GetHTML(url); root =None if html=='' else etree.HTML(html); if root is None: return {} if self.IsMultiData == 'One' else []; tree = etree.ElementTree(root); if isinstance(self.CrawItems, list) and len(self.CrawItems) == 0: return {'Content': html}; return self.GetDataFromCrawItems(tree );
Example #13
Source File: process_includes.py From nyoka with Apache License 2.0 | 6 votes |
def prep_schema_doc(infile, outfile, inpath, options): doc1 = etree.parse(infile) root1 = doc1.getroot() params = Params() params.parent_url = infile params.base_url = os.path.split(inpath)[0] inserts = [] if not options.no_collect_includes: collect_inserts(root1, params, inserts, options) root2 = copy.copy(root1) clear_includes_and_imports(root2) for insert_node in inserts: root2.append(insert_node) else: root2 = root1 if not options.no_redefine_groups: process_groups(root2) raise_anon_complextypes(root2) fix_type_names(root2, options) doc2 = etree.ElementTree(root2) if sys.version_info.major == 2: doc2.write(outfile) else: outfile.write(etree.tostring(root2).decode('utf-8')) return doc2
Example #14
Source File: recipe-578492.py From code with MIT License | 6 votes |
def __init__(self, *a, **k): # Importing names from *a and **k or using defaults self.ffpath = k.setdefault('ffpath', None) self.root = k.setdefault('root', None) if 'tree' not in k else k['tree'].getroot() if len(a) > 0: etype = type(et.Element("a")) ettype = type(et.ElementTree()) for s in a: if isinstance(s, (etype,ettype)): if self.root == None: self.root = s.getroot() if isinstance(s,ettype) else s elif isinstance(s, str): if self.ffpath == None: self.ffpath = s else: raise ValueError("XML\'s initializer only accepts string, ElementTree or Element") if self.ffpath != None and self.root == None: try: self.root = et.parse(self.ffpath).getroot() except (IOError, et.ParseError): # TODO Populate tree and save it raise
Example #15
Source File: anno_coco2voc.py From Dataset_to_VOC_converter with MIT License | 6 votes |
def parse_instance(content, outdir): categories = {d['id']: d['name'] for d in content['categories']} # merge images and annotations: id in images vs image_id in annotations merged_info_list = list(map(cytoolz.merge, cytoolz.join('id', content['images'], 'image_id', content['annotations']))) # convert category id to name for instance in merged_info_list: instance['category_id'] = categories[instance['category_id']] # group by filename to pool all bbox in same file for name, groups in cytoolz.groupby('file_name', merged_info_list).items(): anno_tree = instance2xml_base(groups[0]) # if one file have multiple different objects, save it in each category sub-directory filenames = [] for group in groups: filenames.append(os.path.join(outdir, re.sub(" ", "_", group['category_id']), os.path.splitext(name)[0] + ".xml")) anno_tree.append(instance2xml_bbox(group, bbox_type='xyxy')) for filename in filenames: etree.ElementTree(anno_tree).write(filename, pretty_print=True) print("Formating instance xml file {} done!".format(name))
Example #16
Source File: anno_coco2voc.py From Dataset_to_VOC_converter with MIT License | 6 votes |
def parse_keypoints(content, outdir): keypoints = dict(zip(range(1, len(content['categories'][0]['keypoints'])+1), content['categories'][0]['keypoints'])) # merge images and annotations: id in images vs image_id in annotations merged_info_list = map(cytoolz.merge, cytoolz.join('id', content['images'], 'image_id', content['annotations'])) # convert category name to person for keypoint in merged_info_list: keypoint['category_id'] = "person" # group by filename to pool all bbox and keypoint in same file for name, groups in cytoolz.groupby('file_name', merged_info_list).items(): filename = os.path.join(outdir, os.path.splitext(name)[0]+".xml") anno_tree = keypoints2xml_base(groups[0]) for group in groups: anno_tree = keypoints2xml_object(group, anno_tree, keypoints, bbox_type="xyxy") doc = etree.ElementTree(anno_tree) doc.write(open(filename, "w"), pretty_print=True) print("Formating keypoints xml file {} done!".format(name))
Example #17
Source File: anno_hda2voc.py From Dataset_to_VOC_converter with MIT License | 6 votes |
def parse_anno_file(inputdir, outputdir): # annotation sub-directories in hda annotation input directory assert os.path.exists(inputdir) sub_dirs = os.listdir(inputdir) for sub_dir in sub_dirs: print("Parsing annotations of camera: ", sub_dir) anno_file = os.path.join(inputdir, sub_dir, "Detections/allD.txt") annos = anno_file2dict(anno_file) outdir = os.path.join(outputdir, "Annotations", sub_dir) if not os.path.exists(outdir): os.makedirs(outdir) for filename, anno in annos.items(): anno_tree = instance2xml_base(anno) outfile = os.path.join(outdir, os.path.splitext(filename)[0]+".xml") print("Generating annotation xml file of picture: ", filename) etree.ElementTree(anno_tree).write(outfile, pretty_print=True)
Example #18
Source File: discogs_dump_extractor.py From soweego with GNU General Public License v3.0 | 6 votes |
def _g_process_et_items(path, tag) -> Iterable[Tuple]: """ Generator: Processes ElementTree items in a memory efficient way """ context: etree.ElementTree = etree.iterparse( path, events=('end',), tag=tag ) for event, elem in context: yield event, elem # delete content of node once we're done processing # it. If we don't then it would stay in memory elem.clear()
Example #19
Source File: sim_files.py From simLAB with GNU General Public License v2.0 | 6 votes |
def readXml(self, simType): path = os.path.dirname(__file__) if simType == types.TYPE_USIM: path = os.path.join(path, "sim_files_3g.xml") else: path = os.path.join(path, "sim_files_2g.xml") tree = etree.ElementTree() if not os.path.exists(path): logging.warning("File %s not exists" %path) logging.info("Create xml") if simType == types.TYPE_USIM: root = etree.Element('sim_3G') else: root = etree.Element('sim_2G') else: parser = etree.XMLParser(remove_blank_text=True) root = etree.parse(path, parser).getroot() return path, root
Example #20
Source File: format_xml.py From penelope with MIT License | 6 votes |
def write(dictionary, args, output_file_path): try: print_debug("Creating XML tree...", args.debug) dictionary_elem = etree.Element("dictionary") for index in dictionary.entries_index_sorted: entry = dictionary.entries[index] entry_elem = etree.SubElement(dictionary_elem, "entry") key_elem = etree.SubElement(entry_elem, "key") key_elem.text = entry.headword def_elem = etree.SubElement(entry_elem, "def") def_elem.text = entry.definition tree = etree.ElementTree(dictionary_elem) print_debug("Creating XML tree... done", args.debug) print_debug("Writing to file '%s'..." % (output_file_path), args.debug) tree.write( output_file_path, pretty_print=True, xml_declaration=True ) print_debug("Writing to file '%s'... success" % (output_file_path), args.debug) return [output_file_path] except: print_error("Writing to file '%s'... failure" % (output_file_path)) return None
Example #21
Source File: soupparser.py From learn_python3_spider with MIT License | 5 votes |
def parse(file, beautifulsoup=None, makeelement=None, **bsargs): """Parse a file into an ElemenTree using the BeautifulSoup parser. You can pass a different BeautifulSoup parser through the `beautifulsoup` keyword, and a diffent Element factory function through the `makeelement` keyword. By default, the standard ``BeautifulSoup`` class and the default factory of `lxml.html` are used. """ if not hasattr(file, 'read'): file = open(file) root = _parse(file, beautifulsoup, makeelement, **bsargs) return etree.ElementTree(root)
Example #22
Source File: soupparser.py From aws-lambda-lxml with GNU General Public License v3.0 | 5 votes |
def parse(file, beautifulsoup=None, makeelement=None, **bsargs): """Parse a file into an ElemenTree using the BeautifulSoup parser. You can pass a different BeautifulSoup parser through the `beautifulsoup` keyword, and a diffent Element factory function through the `makeelement` keyword. By default, the standard ``BeautifulSoup`` class and the default factory of `lxml.html` are used. """ if not hasattr(file, 'read'): file = open(file) root = _parse(file, beautifulsoup, makeelement, **bsargs) return etree.ElementTree(root)
Example #23
Source File: sax.py From aws-lambda-lxml with GNU General Public License v3.0 | 5 votes |
def _get_etree(self): "Contains the generated ElementTree after parsing is finished." return ElementTree(self._root)
Example #24
Source File: utils.py From clusterdock with Apache License 2.0 | 5 votes |
def __init__(self, properties=None, root_name='configuration', source_file=None): if source_file: parser = etree.XMLParser(remove_blank_text=True) self.tree = etree.parse(source_file, parser) self.root = self.tree.getroot() else: self.root = etree.Element(root_name) self.tree = etree.ElementTree(self.root) if properties: for the_property in properties: self.add_property(the_property, properties[the_property])
Example #25
Source File: snapshot_handler.py From fssim with MIT License | 5 votes |
def save_to_xml(self, path): root = etree.Element("track") self.save_array_to_xml(root, "cones", self.cones) self.save_array_to_xml(root, "pos", np.transpose(self.cur_state)) tree = etree.ElementTree(root) tree.write(path + '.xml', pretty_print=True, xml_declaration=True, encoding='UTF-8')
Example #26
Source File: manifestparser.py From dtf with Apache License 2.0 | 5 votes |
def to_string(self): """Generate XML string list of items""" temp_manifest_f = tempfile.NamedTemporaryFile() root = etree.Element('Items') # Add binaries self.item_to_xml_binaries(root, self.items) # Add libraries self.item_to_xml_libraries(root, self.items) # Add modules self.item_to_xml_modules(root, self.items) # Add packages self.item_to_xml_packages(root, self.items) # Write it all out export_tree = etree.ElementTree(root) export_tree.write(temp_manifest_f, pretty_print=True) temp_manifest_f.flush() return temp_manifest_f
Example #27
Source File: xmi.py From pyecore with BSD 3-Clause "New" or "Revised" License | 5 votes |
def save(self, output=None, options=None): self.options = options or {} output = self.open_out_stream(output) self.prefixes.clear() self.reverse_nsmap.clear() serialize_default = \ self.options.get(XMIOptions.SERIALIZE_DEFAULT_VALUES, False) nsmap = {XMI: XMI_URL} if len(self.contents) == 1: root = self.contents[0] self.register_eobject_epackage(root) tmp_xmi_root = self._go_across(root, serialize_default) else: tag = QName(XMI_URL, 'XMI') tmp_xmi_root = Element(tag) for root in self.contents: root_node = self._go_across(root, serialize_default) tmp_xmi_root.append(root_node) # update nsmap with prefixes register during the nodes creation nsmap.update(self.prefixes) xmi_root = Element(tmp_xmi_root.tag, nsmap=nsmap) xmi_root[:] = tmp_xmi_root[:] xmi_root.attrib.update(tmp_xmi_root.attrib) xmi_version = QName(XMI_URL, 'version') xmi_root.attrib[xmi_version] = '2.0' tree = ElementTree(xmi_root) tree.write(output, pretty_print=True, xml_declaration=True, encoding=tree.docinfo.encoding) output.flush() self.uri.close_stream()
Example #28
Source File: sax.py From stopstalk-deployment with MIT License | 5 votes |
def _get_etree(self): "Contains the generated ElementTree after parsing is finished." return ElementTree(self._root)
Example #29
Source File: sax.py From aws-lambda-lxml with GNU General Public License v3.0 | 5 votes |
def _get_etree(self): "Contains the generated ElementTree after parsing is finished." return ElementTree(self._root)
Example #30
Source File: soupparser.py From stopstalk-deployment with MIT License | 5 votes |
def parse(file, beautifulsoup=None, makeelement=None, **bsargs): """Parse a file into an ElemenTree using the BeautifulSoup parser. You can pass a different BeautifulSoup parser through the `beautifulsoup` keyword, and a diffent Element factory function through the `makeelement` keyword. By default, the standard ``BeautifulSoup`` class and the default factory of `lxml.html` are used. """ if not hasattr(file, 'read'): file = open(file) root = _parse(file, beautifulsoup, makeelement, **bsargs) return etree.ElementTree(root)