def parseAndroidAudioPolicyEngineConfigurationFile( audiopolicyengineconfigurationfile): logging.info("Checking Audio Policy Engine Configuration file {}".format( audiopolicyengineconfigurationfile)) # # extract all product strategies name from audio policy engine configuration file # strategy_names = [] oldWorkingDir = os.getcwd() print "Current working directory %s" % oldWorkingDir newDir = os.path.join(oldWorkingDir, audiopolicyengineconfigurationfile.name) policy_engine_in_tree = ET.parse(audiopolicyengineconfigurationfile) os.chdir(os.path.dirname(os.path.normpath(newDir))) print "new working directory %s" % os.getcwd() policy_engine_root = policy_engine_in_tree.getroot() EI.include(policy_engine_root) os.chdir(oldWorkingDir) for strategy in policy_engine_root.iter('ProductStrategy'): strategy_names.append(strategy.get('name')) return strategy_names
def main(): parser = argparse.ArgumentParser(description='Create text with wrapping directives from rtt.') parser.add_argument("rtt_file", help="RTT file.") parser.add_argument("-r", "--heading_regexp", default="", help="Regexp to identify a chapter heading") parser.add_argument("-b", "--heading_blocks", type=int, choices=[1, 2], default=1, help="The number of blocks in a chapter heading.") args = parser.parse_args() rtt_file = os.path.abspath(args.rtt_file) if not os.path.isfile(rtt_file): print(rtt_file, "is not a file\n", file="stderr") parser.print_help() sys.exit(-1) rtt = et.parse(rtt_file) pages = rtt.find("pages") os.chdir(os.path.dirname(rtt_file)) ei.include(pages) open_mf = pages.attrib["microformatting-open"] close_mf = pages.attrib["microformatting-close"] pages_text = [] first_page_text = pages.find("page/text") if first_page_text.text[0] != "\n": first_page_text.text = "\n" + first_page_text.text for p in pages.findall("page"): pages_text.append(process_page(p, open_mf, close_mf, args.heading_regexp, args.heading_blocks)) sys.stdout.write("\n".join(pages_text))
def parse(self, file, baseUrl="", ownerDoc=None): absUrl = convertToAbsUrl(file, baseUrl) fp = urllib.request.urlopen(absUrl) try: tree = ElementTreeExtension() treeWrapper = self.treeWrapperClass(self, tree, self.useCaching) parser = ExtXMLTreeBuilder(file, absUrl, self, treeWrapper) treeWrapper.getTree().parse(fp, parser) fp.close() # XInclude support if self.processXInclude: loaderInst = ExtXIncludeLoader(self.parse, absUrl, ownerDoc) try: ElementInclude.include( treeWrapper.getTree().getroot(), loaderInst.loader) except IOError as errInst: raise GenXmlIfError( "%s: IOError: %s" % (file, str(errInst))) except ExpatError as errstr: fp.close() raise GenXmlIfError("%s: ExpatError: %s" % (file, str(errstr))) except ElementInclude.FatalIncludeError as errInst: fp.close() raise GenXmlIfError("%s: XIncludeError: %s" % (file, str(errInst))) return treeWrapper
def resolve_xinclude(source_folder, source_ext): """""" # Process all files in the source folder with the give extension pattern = "{}/**/*{}".format(source_folder, source_ext) matches = glob.glob(pattern, recursive=True) for filepath in matches: # print("Parsing", filepath) tree = ET.parse(filepath) root = tree.getroot() EI.include(root, loader=loader) # resolves xi:include elements # Write output file filename = os.path.basename(filepath) basename, ext = os.path.splitext(filename) out_filename = "{}.sbt".format(basename) output_folder = os.path.dirname(filepath) out_filepath = os.path.join(output_folder, out_filename) with open(out_filepath, "wb") as f: s = ET.tostring(root) # TODO: would be good to clean up trailing whitespace # however, we'd have to do some str-bytes conversion # s = "\n".join([ln.strip() for ln in s.splitlines()]) f.write(s) print("Wrote", out_filepath) return
def loader(href, parse, encoding=None): if parse == "xml": with open(href, "rb") as file: data = ET.parse(file).getroot() EI.include(data, loader=loader) else: raise RuntimeError("Unable to handle recursive include.") return data
def get_bocca_objects(element): objs = [] ElementInclude.include(element) for child in element.getchildren(): if child.tag == 'project': objs.extend(get_bocca_objects(child)) elif child.tag in bocca_object_names: objs.append(child) return objs
def create_project(project_dir, rtt_file): project_file = project_dir + "/project" data = project_data.ProjectData(project_file, True) rtt = et.parse(rtt_file).getroot() ei.include(rtt) process_metadata(rtt, data) process_pages(rtt, data, project_dir) data.save() #also unlocks os.chmod(project_file, 0660)
def parse_xml( fname ): """Returns a parsed xml tree""" # handle deprecation warning for XMLParsing a file with DOCTYPE class DoctypeSafeCallbackTarget( ElementTree.TreeBuilder ): def doctype( *args ): pass tree = ElementTree.ElementTree() root = tree.parse( fname, parser=ElementTree.XMLParser( target=DoctypeSafeCallbackTarget() ) ) ElementInclude.include( root ) return tree
def parse_xml( fname ): """Returns a parsed xml tree""" # handle deprecation warning for XMLParsing a file with DOCTYPE class DoctypeSafeCallbackTarget( ElementTree.TreeBuilder ): def doctype( *args ): pass tree = ElementTree.ElementTree() root = tree.parse( fname, parser=ElementTree.XMLParser( target=DoctypeSafeCallbackTarget() ) ) ElementInclude.include( root ) return tree
def testURLInvocation_XInclude_ETINCLUDE(self): #Reset the server back to "0" r = requests.get(self._URL_+"/reset") r = requests.get(self._URL_ +"/getCounter") request_content = r.text.replace("\r\n","") self.assertEqual("0", request_content) with self.assertRaises(IOError): root = self.helperParse('../../xml_files_windows/url_invocation_xinclude.xml') # Resolve xi:include references _ETINCLUDE.include(root)
def testURLInvocation_XInclude_ETINCLUDE(self): #Reset the server back to "0" r = requests.get(self._URL_ + "/reset") r = requests.get(self._URL_ + "/getCounter") request_content = r.text.replace("\r\n", "") self.assertEqual("0", request_content) with self.assertRaises(IOError): root = self.helperParse( '../../xml_files_windows/ssrf/url_invocation_xinclude.xml') # Resolve xi:include references _ETINCLUDE.include(root)
def testXInclude_ETINCLUDE(self): root = self.helperParse('../../xml_files_windows/xinclude.xml') # Resolve xi:include references _ETINCLUDE.include(root) # The Inclusion of xi:include should now have happened. # Therefore we can get an iterator over the newly included element # 'content' elem_data = root.iter('content') for elem in elem_data: self.assertEqual("it_works", elem.text) self.assertEqual("content", elem.tag)
def testXInclude_ETINCLUDE(self): root = self.helperParse('../../xml_files_windows/xinclude.xml') # Resolve xi:include references _ETINCLUDE.include(root) # The Inclusion of xi:include should now have happened. # Therefore we can get an iterator over the newly included element # 'content' elem_data = root.iter('content') for elem in elem_data: self.assertEqual("it_works", elem.text) self.assertEqual("content", elem.tag)
def test_include_xml(self): from xml.etree import ElementTree, ElementInclude tree = ElementTree.parse("files/document.xml") root = tree.getroot() ElementInclude.include(root) tree.write("files/tree_created.xml") fo = open("files/tree_created.xml", "r+") lines = fo.readlines() self.assertEqual([ '<document>\n', ' <document>\n', ' <para>This is a paragraph.</para>\n', '</document>\n', '</document>' ], lines)
def load(self, filename): try: doc = etree.parse(filename) root = doc.getroot() einclude.include(root) self.logger.info("Parsing sources.") self.__parse_sources(root) self.logger.info("Parsing servers.") self.__parse_servers(root) self.logger.info("Parsing rules.") self.__parse_rules(root) except ExpatError, e: self.logger.error("The XML file is invalid; %s." % (e)) self.errors += 1
def parseString (self, text, baseUrl="", ownerDoc=None): absUrl = convertToAbsUrl ("", baseUrl) tree = ElementTreeExtension() treeWrapper = self.treeWrapperClass(self, tree, self.useCaching) parser = ExtXMLTreeBuilder("", absUrl, self, treeWrapper) parser.feed(text) treeWrapper.getTree()._setroot(parser.close()) # XInclude support if self.processXInclude: loaderInst = ExtXIncludeLoader (self.parse, absUrl, ownerDoc) ElementInclude.include(treeWrapper.getTree().getroot(), loaderInst.loader) return treeWrapper
def main(): if len(sys.argv) != 2 or not os.path.isfile(sys.argv[1]): print("Usage: ", sys.argv[0], "rtt_file") sys.exit(-1) rtt = et.parse(sys.argv[1]) pages = rtt.find("pages") os.chdir(os.path.dirname(sys.argv[1])) ei.include(pages) open_mf = pages.attrib["microformatting-open"] close_mf = pages.attrib["microformatting-close"] pages_text = [] first_page_text = pages.find("page/text") if first_page_text.text[0] != "\n": first_page_text.text = "\n" + first_page_text.text for p in pages.findall("page"): pages_text.append(process_page(p, open_mf, close_mf, heading_regexp)) sys.stdout.write("\n".join(pages_text))
def __load_target_xml(archtype: QL_ARCH, ostype: QL_OS) -> ElementTree.ElementTree: filename, base_url = QlGdbFeatures.__get_xml_path(archtype) tree = ElementTree.parse(filename) # NOTE: this is needed to load xinclude hrefs relative to the main xml file. starting # from python 3.9 ElementInclude.include has an argument for that called 'base_url'. # this is a workaround for earlier python versions such as 3.8 # <WORKAROUND> def my_loader(base: PurePath): def __wrapped(href: str, parse, encoding=None): abshref = base / href return ElementInclude.default_loader(str(abshref), parse, encoding) return __wrapped # </WORKAROUND> # inline all xi:include elements ElementInclude.include(tree.getroot(), loader=my_loader(base_url)) # patch xml osabi element with the appropriate abi tag osabi = tree.find('osabi') if osabi is not None: # NOTE: the 'Windows' abi tag is supported starting from gdb 10. # earlier gdb versions use 'Cygwin' instead abitag = { QL_OS.LINUX: 'GNU/Linux', QL_OS.FREEBSD: 'FreeBSD', QL_OS.MACOS: 'Darwin', QL_OS.WINDOWS: 'Windows', QL_OS.UEFI: 'Windows', QL_OS.DOS: 'Windows', QL_OS.QNX: 'QNX-Neutrino' }.get(ostype, 'unknown') osabi.text = abitag return tree
def parseAndroidAudioPolicyConfigurationFile(audiopolicyconfigurationfile): logging.info("Checking Audio Policy Configuration file {}".format( audiopolicyconfigurationfile)) # # extract all devices addresses from audio policy configuration file # address_criteria_mapping_table = { 'sink': "OutputDevicesAddressesType", 'source': "InputDevicesAddressesType" } address_criteria = { 'OutputDevicesAddressesType': [], 'InputDevicesAddressesType': [] } old_working_dir = os.getcwd() print("Current working directory %s" % old_working_dir) new_dir = os.path.join(old_working_dir, audiopolicyconfigurationfile.name) policy_in_tree = ET.parse(audiopolicyconfigurationfile) os.chdir(os.path.dirname(os.path.normpath(new_dir))) print("new working directory %s" % os.getcwd()) policy_root = policy_in_tree.getroot() EI.include(policy_root) os.chdir(old_working_dir) for device in policy_root.iter('devicePort'): for key in address_criteria_mapping_table.keys(): if device.get('role') == key and device.get('address'): logging.info("{}: <{}>".format(key, device.get('address'))) address_criteria[address_criteria_mapping_table[key]].append( device.get('address')) for criteria in address_criteria: values = ','.join(address_criteria[criteria]) logging.info("{}: <{}>".format(criteria, values)) return address_criteria
def parse (self, file, baseUrl="", ownerDoc=None): absUrl = convertToAbsUrl (file, baseUrl) fp = urllib.urlopen (absUrl) try: tree = ElementTreeExtension() treeWrapper = self.treeWrapperClass(self, tree, self.useCaching) parser = ExtXMLTreeBuilder(file, absUrl, self, treeWrapper) treeWrapper.getTree().parse(fp, parser) fp.close() # XInclude support if self.processXInclude: loaderInst = ExtXIncludeLoader (self.parse, absUrl, ownerDoc) try: ElementInclude.include(treeWrapper.getTree().getroot(), loaderInst.loader) except IOError, errInst: raise GenXmlIfError, "%s: IOError: %s" %(file, str(errInst)) except ExpatError, errstr: fp.close() raise GenXmlIfError, "%s: ExpatError: %s" %(file, str(errstr))
def fromFile(self, filename): r""" Following is the header as given in zend-config:: /** * fromFile(): defined by Reader interface. * * @see ReaderInterface::fromFile() * @param string $filename * @return array * @throws Exception\RuntimeException */ """ if not os.path.isfile(filename) and not os.access(filename, os.R_OK): raise RuntimeException( "Xml: File %s Doesn't Exist or Not Readable" % filename) self.directory = os.path.dirname(filename.rstrip(os.sep)) or '.' try: xmlcontent = '' with open(filename, "r") as file: for line in file: if "@include" in line: include = line.split(":")[1].strip() with open(os.path.join(self.directory, include), "r") as includedfile: for includedline in includedfile: xmlcontent += includedline else: xmlcontent += line self.root = ElementTree.fromstring(xmlcontent) ElementInclude.include(self.root, self.loader) except ElementTree.ParseError as e: raise RuntimeException("Xml: Error Reading XML file \"%s\": %s" % (filename, e)) return self.process(self.root)
def _parse_xml(fname): tree = ElementTree.parse(fname) root = tree.getroot() ElementInclude.include(root) return tree
#!/usr/bin/python3 import xml.etree.ElementTree as et import xml.etree.ElementInclude as ei import sys rtt = et.parse(sys.argv[1]) ei.include(rtt.getroot()) error_found = False for page in rtt.findall(".//page"): messages = [] img_lines = len(page.find("image/lines").text.split(",")) text = page.find("text").text text_lines = len([X for X in text.splitlines() if len(X)]) if(img_lines != text_lines): messages.append("Line count mismatch: image %d text %d" % (img_lines, text_lines)) if text.find("'") != -1: messages.append("Single straight quote found") if text.find('"') != -1: messages.append("Double straight quote found") if text.find("--") != -1: messages.append("Double hyphen found") if messages: error_found = True print(page.attrib["id"]) for m in messages: print(" ", m) if not error_found: print("No errors found")
def _parse_xml(fname): tree = ElementTree.parse(fname) root = tree.getroot() ElementInclude.include(root) return tree
def runCmds(): #1517 is VDSLR2 EqptType. This is demo. We should first get the EqptType from BID if tstItem_name is not None: if tstItem_name.upper() == "ALL": print g_ERR_TBD exit(0) cmdMapFile = None pCmdMapping = None boardInfo = getBidEx() if boardInfo is not None: if arg_plat_sw_automation: setPlatSwAutomation() cmdMapFile = getCmdMapFile(boardInfo['EqptType']) if cmdMapFile is not None: pCmdMapping = ET.parse(cmdMapFile) # ESA-760 depth = 0 while (depth < m_XINCLUDE_DEPTH): EI.include(pCmdMapping.getroot()) depth += 1 # debug #ET.dump(pCmdMapping.getroot()) else: print g_ERR_CMD_MAPPING_FILE_NOT_FOUND exit(-1) else: print g_ERR_USE_DEFAULT_CMD_MAP cmdMapFile = getCmdMapFile('1517') if cmdMapFile is not None: pCmdMapping = ET.parse(cmdMapFile) else: print g_ERR_CMD_MAPPING_FILE_NOT_FOUND exit(-1) bTstItem = False for tstKey, tstVal in getTstItemMapping(boardInfo['EqptType']): testCmd = tstKey.upper() if testCmd == tstItem_name: bTstItem = True cmdParse = None testCmd = None childNotEmpty = False cmdMappingList = pCmdMapping.findall(tstVal) # ESA-760 if not cmdMappingList: itemXPath = m_XPATH_PATTERN + tstVal cmdMappingList = pCmdMapping.findall(itemXPath) # ESAA-523 milog = miLog() passCnt = 0 failCnt = 0 summaryMsg = "" detailMsg = "" debugMsg = "" tstNameFmtStr = "\nTestName:[{}] \n".format(tstItem_name) tstNameEndFmtStr = "\nTestName:[/{}] \n".format(tstItem_name) summaryMsg = tstNameFmtStr + summaryMsg summaryMsg = summaryMsg + g_HEADERFMTSTR startTime = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(time.time())) tmpDebugStr = "" tmpDetailStr = "" if arg_dump_single_dbg: milog.dumpTest(tstItem_name) continue if arg_dump_single_detail: milog.dumpTest(tstItem_name, g_DUMP_DETAIL_STR) continue # ESA-1227 debugMsg = tstNameFmtStr milog.prt_debug(debugMsg) # ESA-747 if not cmdMappingList: testResult = g_cmp_FAIL tmpDebugStr = g_ERR_ITEM_NOT_FOUND_F(cmdMapFile) else: # # ESA-1227 detailMsg = tstNameFmtStr milog.prt_detail(detailMsg) # debugMsg = tstNameFmtStr # milog.prt_debug(debugMsg) testResult = g_cmp_PASS # ESA-1476. Only get the last item as the findall returns a list # containing all matching elements in *document* order. cmdMappingList = cmdMappingList[-1:] for cmdMappingElem in cmdMappingList: for child in cmdMappingElem.getchildren(): #print child.attrib['name'],':',child.text.strip() if cmdParse is None: childNotEmpty = True cmdParse = testCmdParse(child) else: cmdParse.add(child) if cmdParse: if cmdParse.ready(): testCmdHandler = cmdParse.getCmdHandler() if testCmdHandler is not None: testCmdHandler.preProcess(verbose=arg_verbose, \ rstshown=arg_rstShown, killcard=arg_forced,\ repeated=arg_repeated) testCmdHandler.process() # ESAA-480 tmpResult = testCmdHandler.postProcess() # ESA-1227 #tmpDetailStr = testCmdHandler.getDetailMsg() # ESA-1283. Move log from the end to here since there # is one case of hybrid(e.g. SGMIIKA2CPU test which is # the combination of BCM and linux commands). # EX-25985 does the same as debug log(add ending label) # to get the detail information for the phyconn test # if this test pass. #tmpDetailStr = tmpDetailStr + tstNameEndFmtStr #milog.prt_detail(tmpDetailStr) if tmpResult == g_cmp_FAIL: testResult = g_cmp_FAIL # ESAA-523 #if testCmdHandler.getDebugMsg() is not None: # tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg() # EX-25985 #tmpDebugStr = tmpDebugStr + tstNameEndFmtStr # ESA-1283 Put log there since there is one hybrid case. #milog.prt_debug(tmpDebugStr) cmdParse = None else: print g_ERR_HANDLER_NOT_FOUND else: print g_ERR_TEST_CMD_EMPTY #The cmdParse should be None. If it's not, there is one possibility #that the value of next in last command of config xml file(config/xxx.xml) #is still yes. To cover this inadvertent incorrectness, add code below if cmdParse is not None: #No ready() invoked. Forced to run the handler testCmdHandler = cmdParse.getCmdHandler() if testCmdHandler is not None: testCmdHandler.preProcess(verbose=arg_verbose, \ rstshown=arg_rstShown, killcard=arg_forced,\ repeated=arg_repeated) testCmdHandler.process() # ESAA-480 tmpResult = testCmdHandler.postProcess() #tmpDetailStr = testCmdHandler.getDetailMsg() # ESA-1283 # EX-25985 does the same as debug log(add ending label) #tmpDetailStr = tmpDetailStr + tstNameEndFmtStr #milog.prt_detail(tmpDetailStr) if tmpResult == g_cmp_FAIL: testResult = g_cmp_FAIL # ESAA-523 #tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg() # EX-25985 #tmpDebugStr = tmpDebugStr + tstNameEndFmtStr # ESA-1283 #milog.prt_debug(tmpDebugStr) cmdParse = None else: print g_ERR_HANDLER_NOT_FOUND if childNotEmpty: tmpDetailStr = testCmdHandler.getDetailMsg() # ESA-1283. Move log from the end to here since there # is one case of hybrid(e.g. SGMIIKA2CPU test which is # the combination of BCM and linux commands). # EX-25985 does the same as debug log(add ending label) # to get the detail information for the phyconn test # if this test pass. tmpDetailStr = tmpDetailStr + tstNameEndFmtStr milog.prt_detail(tmpDetailStr) if testResult == g_cmp_FAIL: tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg( ) if testResult == g_cmp_FAIL: # ESAA-523 #if testCmdHandler.getDebugMsg() is not None: #tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg() # EX-25985 tmpDebugStr = tmpDebugStr + tstNameEndFmtStr # ESA-1283 Put log there since there is one hybrid case. milog.prt_debug(tmpDebugStr) # ESAA-523 endTime = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(time.time())) if testResult == g_cmp_PASS: passCnt += 1 else: failCnt += 1 tmpStr = "| {:>4} | {:>4} | {} | {} |\n".format( str(passCnt), str(failCnt), startTime, endTime) summaryMsg = summaryMsg + tmpStr summaryMsg = summaryMsg + g_FOOTERFMRSTR milog.prt_summary(summaryMsg) # ESAA-480 if arg_rstShown: print testResult if bTstItem == False: if tstItem_name is None: usage() else: print g_ERR_TEST_ITEM_NOT_FOUND
import defusedxml.ElementTree as _ET import xml.etree.ElementInclude as _ETINCLUDE tree = _ET.parse('../../xml_files_windows/standard.xml') root = tree.getroot() print root.tag print root.text ''' # for using XInclude # This code helped a lot with troubeshooting ElementInclude.include: # http://stackoverflow.com/questions/23684996/using-python-elementtree-elementinclude-and-xpointer-to-access-included-xml-file import xml.etree.ElementTree as _ET import xml.etree.ElementInclude as _ETINCLUDE tree = _ET.parse('../../xml_files_windows/xinclude.xml') root = tree.getroot() _ETINCLUDE.include(root) elem_data = root.iter('content') for elem in elem_data: print elem.tag print elem.text '''
def LoadNav(self, path: str) -> List[nav.NavItem]: tree = ET.parse(path) root = tree.getroot() ElementInclude.include(root, self._loader) return parser.ParseNav(root)
def parse_xml(fname): """Returns a parsed xml tree""" tree = ElementTree.parse(fname) root = tree.getroot() ElementInclude.include(root) return tree
def XInclude_Tutorial(file): tree = ET.parse(file) root = tree.getroot() EI.include(root) tree.write('include.xml')
def LoadDocument(self, path: str) -> document.Document: tree = ET.parse(path) root = tree.getroot() ElementInclude.include(root, self._loader) return parser.ParseDocument(root)
def parse2tok(p, tmap, pel=False): tree = ET.parse(p) cwd=os.getcwd() os.chdir(os.path.dirname(p)) root = tree.getroot() EI.include(root) parent_map = {c: p for p in root.iter() for c in p} ls = root.findall(f'.//{tei_xmlns}line') if len(ls) == 0: ls = root.findall(f'.//{tei_xmlns}seg') if len(ls) == 0: ls = root.findall(f'.//{tei_xmlns}l') if len(ls) == 0: ls = root.findall(f'.//{tei_xmlns}p') tok=[] divs=[] if ls[0].tag.endswith('p'): tl=[] for b in ls: res=[] p_content(b, res) tl.append(res) return tl, ["p"] for b in ls: p=parent_map[b] if pel: px = p.tag.replace(f'{tei_xmlns}', '') + "/" else: px = "" if (p.tag == '{http://www.tei-c.org/ns/1.0}head'): pdiv = parent_map[p] if f'{xml_xmlns}id' in pdiv.attrib: pdivid=pdiv.attrib[f'{xml_xmlns}id'] else: pdivid='' divs.append((len(tok), b.text, pdivid)) pm=[p] while p in parent_map: p=parent_map[p] pm.append(p) px="p" for p in pm: #print(p.tag) if p.tag in tmap: px = tmap[p.tag][1] break #tag = b.tag.replace(f'{tei_xmlns}', '') tag="" if "rend" in b.attrib: tag = ";" + b.attrib["rend"] if f'{xml_xmlns}id' in b.attrib: id=b.attrib[f'{xml_xmlns}id'] else: id="noid" tx = linechildren(b) tx = tx.replace("\n", "") seq=line2arr(tx) cnt = 0 for s in seq: cnt += 1 tok.append((px + tag, id, cnt, s)) os.chdir(cwd) return (tok, divs)
import defusedxml.ElementTree as _ET import xml.etree.ElementInclude as _ETINCLUDE tree = _ET.parse('../../xml_files_windows/standard.xml') root = tree.getroot() print root.tag print root.text ''' # for using XInclude # This code helped a lot with troubeshooting ElementInclude.include: # http://stackoverflow.com/questions/23684996/using-python-elementtree-elementinclude-and-xpointer-to-access-included-xml-file import xml.etree.ElementTree as _ET import xml.etree.ElementInclude as _ETINCLUDE tree = _ET.parse('../../xml_files_windows/xinclude.xml') root = tree.getroot() _ETINCLUDE.include(root) elem_data = root.iter('content') for elem in elem_data: print elem.tag print elem.text '''
def load_menus(path): """Load the file path.""" menus = ElementTree.parse(path) ElementInclude.include(menus.getroot(), loader=Loader(path)) return menus
class CommentedTreeBuilder(XmlET.TreeBuilder): # Python 2.7 uses ElementTree 1.3.x. def comment(self, data): self.start(XmlET.Comment, {}) self.data(data) self.end(XmlET.Comment) def parse_xml(file_name, lh, include_comments=False): """Returns a parsed xml tree with comments intact.""" fh = open(file_name, 'r') try: if include_comments: tree = XmlET.parse( fh, parser=XmlET.XMLParser(target=CommentedTreeBuilder())) else: tree = XmlET.parse( fh, parser=XmlET.XMLParser(target=DoctypeSafeCallbackTarget())) except Exception, e: fh.close() lh.write("Exception attempting to parse %s:\n%s\n\n" % (file_name, str(e))) return None fh.close() root = tree.getroot() ElementInclude.include(root) return tree
def doctype(*args): pass class CommentedTreeBuilder(XmlET.TreeBuilder): # Python 2.7 uses ElementTree 1.3.x. def comment(self, data): self.start(XmlET.Comment, {}) self.data(data) self.end(XmlET.Comment) def parse_xml(file_name, lh, include_comments=False): """Returns a parsed xml tree with comments intact.""" fh = open(file_name, 'r') try: if include_comments: tree = XmlET.parse(fh, parser=XmlET.XMLParser(target=CommentedTreeBuilder())) else: tree = XmlET.parse(fh, parser=XmlET.XMLParser(target=DoctypeSafeCallbackTarget())) except Exception, e: fh.close() lh.write("Exception attempting to parse %s:\n%s\n\n" % (file_name, str(e))) return None fh.close() root = tree.getroot() ElementInclude.include(root) return tree