def validate(self): if not hasattr(self, 'xsd'): self.xsd=etree.XMLSchema(etree.parse(XSD)) xml = etree.fromstring(self.Xml) return self.xsd.validate(xml)
def _parsed_msg(self): """parse self._body xml string and set it to self.__dict__ """ modified_key = self._modify_key("_parsed_msg") if modified_key in self.__dict__: return self.__dict__[modified_key] if self._msg is None: raise ValueError(u"self._msg should not be None") self.__dict__[modified_key] = etree.fromstring(self._msg) return self.__dict__[modified_key]
def alpino_parse(sent, host='zardoz.service.rug.nl', port=42424): s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((host,port)) sent = sent + "\n\n" sentbytes= sent.encode('utf-8') s.sendall(sentbytes) bytes_received= b'' while True: byte = s.recv(8192) if not byte: break bytes_received += byte #print(bytes_received.decode('utf-8'), file=sys.stderr) xml = etree.fromstring(bytes_received) return xml
def send_cmd_int(cls, cmd, msg_type): '''Construct NX-API message. Send commands through NX-API. Only single command for show commands. Internal usage''' if msg_type == "cli_show" or msg_type == "cli_show_ascii": if " ;" in cmd: raise cmd_exec_error("Only single show command supported in internal api") req_msg_str = cls.req_obj.get_req_msg_str(msg_type=msg_type, input_cmd=cmd, out_format=cls.out_format, do_chunk=cls.do_chunk, sid=cls.sid) (resp_headers, resp_str) = \ cls.req_fetcher.get_resp(req_msg_str, cls.cookie, cls.timeout) if 'Set-Cookie' in resp_headers: cls.cookie = resp_headers['Set-Cookie'] content_type = resp_headers['Content-Type'] root = etree.fromstring(resp_str) body = root.findall('.//body') code = root.findall('.//code') msg = root.findall('.//msg') output = "" status = 0 if len(body) != 0: if msg_type == 'cli_show': output = etree.tostring(body[0]) else: output = body[0].text if output == None: output = "" if code[0].text == "200": status = 0 else: status = int(code[0].text) return [output, status, msg[0].text]
def parse_with_lxml(): root = etree.fromstring(CONTENT) for log in root.xpath("//log"): print log.text
def test_tree_generation(self): with open('tests/instruments/companies.xml') as fp: tree = etree.fromstring(remove_nbsp(fp.read()), parser=self.parser) document_tree = create_document_tree(tree) with open('tests/companiesact_doc_tree.json') as fp: self.assertEqual(json.dumps(document_tree), fp.read().strip())