Python xml.etree.ElementTree 模块,parse() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.etree.ElementTree.parse()

项目:ssd.pytorch    作者:amdegroot    | 项目源码 | 文件源码
def pull_item(self, index):
        img_id = self.ids[index]

        target = ET.parse(self._annopath % img_id).getroot()
        img = cv2.imread(self._imgpath % img_id)
        height, width, channels = img.shape

        if self.target_transform is not None:
            target = self.target_transform(target, width, height)

        if self.transform is not None:
            target = np.array(target)
            img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
            # to rgb
            img = img[:, :, (2, 1, 0)]
            # img = img.transpose(2, 0, 1)
            target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
        return torch.from_numpy(img).permute(2, 0, 1), target, height, width
        # return torch.from_numpy(img), target, height, width
项目:onto-lstm    作者:pdasigi    | 项目源码 | 文件源码
def read_preposition_senses(self):
        num_senses_per_prep = []
        for filename in os.listdir(self.prep_senses_dir):
            if '.defs.xml' in filename:
                prep_str = filename.replace('.defs.xml', '')
                xml_root = ElementTree.parse("%s/%s" % (self.prep_senses_dir, filename)).getroot()
                senses = []
                for child_el in xml_root.getchildren():
                    sense_id = child_el.findtext('senseid')
                    if sense_id is not None:
                        # This will add strings like 'into-1(1)'
                        senses.append("%s-%s" % (prep_str, sense_id))
                num_senses_per_prep.append(len(senses))
                self.prep_senses[prep_str] = senses
        num_preps = len(self.prep_senses)
        print >>sys.stderr, "Read senses for %d prepositions." % num_preps
        print >>sys.stderr, "Senses per preposition: %f" % (float(sum(num_senses_per_prep))/num_preps)

    # TODO: Take a coarse-grained mapping file and implement the following function.
项目:service-fabric-cli    作者:Azure    | 项目源码 | 文件源码
def find_service_manifest(xml_file):
    """Find the path to the first service manifest for an application"""
    root = ET.parse(xml_file).getroot()

    import_elem = root.find('fabric:ServiceManifestImport', XML_NS)
    if import_elem is None:
        raise ValueError('Could not find service manifest import section')

    ref_elem = import_elem.find('fabric:ServiceManifestRef', XML_NS)
    if ref_elem is None:
        raise ValueError('Could not find service manifest reference section')

    manifest_name = ref_elem.attrib.get('ServiceManifestName', None)
    if not manifest_name:
        raise ValueError('Could not find service manifest name')
    return os.path.join(os.path.dirname(xml_file), manifest_name,
                        'ServiceManifest.xml')
项目:service-fabric-cli    作者:Azure    | 项目源码 | 文件源码
def parse_service_type(xml_file):
    """Determines the first avaliable service type and the associated
    type name"""
    root = ET.parse(xml_file).getroot()
    service_type = root.find('fabric:ServiceTypes', XML_NS)
    if service_type is None:
        raise ValueError('Could not find service types in service manifest')
    service_type_kind = None
    if 'StatelessServiceType' in service_type[0].tag:
        service_type_kind = 'stateless'
    else:
        # For now we only support stateless services for service creation
        raise ValueError('Unsupported service type')
    service_type_name = service_type[0].attrib.get('ServiceTypeName', None)
    if not service_type_name:
        raise ValueError('Could not find service type name')

    return (service_type_kind, service_type_name)
项目:squeezeDet-hand    作者:fyhtea    | 项目源码 | 文件源码
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        obj_struct['pose'] = obj.find('pose').text
        obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text),
                              int(bbox.find('ymin').text),
                              int(bbox.find('xmax').text),
                              int(bbox.find('ymax').text)]
        objects.append(obj_struct)

    return objects
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'softPkg'
        rootClass = softPkg
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'devicepkg'
        rootClass = devicepkg
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'deviceconfiguration'
        rootClass = deviceconfiguration
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'domainmanagerconfiguration'
        rootClass = domainmanagerconfiguration
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'profile'
        rootClass = profile
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'softwareassembly'
        rootClass = softwareassembly
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'softwarecomponent'
        rootClass = softwarecomponent
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:core-framework    作者:RedhawkSDR    | 项目源码 | 文件源码
def parse(inFileName):
    doc = parsexml_(inFileName)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'properties'
        rootClass = properties
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
##     sys.stdout.write('<?xml version="1.0" ?>\n')
##     rootObj.export(sys.stdout, 0, name_=rootTag,
##         namespacedef_='',
##         pretty_print=True)
    return rootObj
项目:HandDetection    作者:YunqiuXu    | 项目源码 | 文件源码
def parse_rec(filename):
  """ Parse a PASCAL VOC xml file """
  tree = ET.parse(filename)
  objects = []
  for obj in tree.findall('object'):
    obj_struct = {}
    obj_struct['name'] = obj.find('name').text
    obj_struct['pose'] = obj.find('pose').text
    obj_struct['truncated'] = int(obj.find('truncated').text)
    obj_struct['difficult'] = int(obj.find('difficult').text)
    bbox = obj.find('bndbox')
    obj_struct['bbox'] = [int(bbox.find('xmin').text),
                          int(bbox.find('ymin').text),
                          int(bbox.find('xmax').text),
                          int(bbox.find('ymax').text)]
    objects.append(obj_struct)

  return objects
项目:HandDetection    作者:YunqiuXu    | 项目源码 | 文件源码
def parse_rec(filename):
  """ Parse a PASCAL VOC xml file """
  tree = ET.parse(filename)
  objects = []
  for obj in tree.findall('object'):
    obj_struct = {}
    obj_struct['name'] = obj.find('name').text
    obj_struct['pose'] = obj.find('pose').text
    obj_struct['truncated'] = int(obj.find('truncated').text)
    obj_struct['difficult'] = int(obj.find('difficult').text)
    bbox = obj.find('bndbox')
    obj_struct['bbox'] = [int(bbox.find('xmin').text),
                          int(bbox.find('ymin').text),
                          int(bbox.find('xmax').text),
                          int(bbox.find('ymax').text)]
    objects.append(obj_struct)

  return objects
项目:HandDetection    作者:YunqiuXu    | 项目源码 | 文件源码
def parse_rec(filename):
  """ Parse a PASCAL VOC xml file """
  tree = ET.parse(filename)
  objects = []
  for obj in tree.findall('object'):
    obj_struct = {}
    obj_struct['name'] = obj.find('name').text
    obj_struct['pose'] = obj.find('pose').text
    obj_struct['truncated'] = int(obj.find('truncated').text)
    obj_struct['difficult'] = int(obj.find('difficult').text)
    bbox = obj.find('bndbox')
    obj_struct['bbox'] = [int(bbox.find('xmin').text),
                          int(bbox.find('ymin').text),
                          int(bbox.find('xmax').text),
                          int(bbox.find('ymax').text)]
    objects.append(obj_struct)

  return objects
项目:mongodb-monitoring    作者:jruaux    | 项目源码 | 文件源码
def parse(stream):
        """Parse a stream containing XML into an ``InputDefinition``.

        :param stream: stream containing XML to parse.
        :return: definition: an ``InputDefinition`` object.
        """
        definition = InputDefinition()

        # parse XML from the stream, then get the root node
        root = ET.parse(stream).getroot()

        for node in root:
            if node.tag == "configuration":
                # get config for each stanza
                definition.inputs = parse_xml_data(node, "stanza")
            else:
                definition.metadata[node.tag] = node.text

        return definition
项目:Splunk_CBER_App    作者:MHaggis    | 项目源码 | 文件源码
def parse(stream):
        """Parse a stream containing XML into an ``InputDefinition``.

        :param stream: stream containing XML to parse.
        :return: definition: an ``InputDefinition`` object.
        """
        definition = InputDefinition()

        # parse XML from the stream, then get the root node
        root = ET.parse(stream).getroot()

        for node in root:
            if node.tag == "configuration":
                # get config for each stanza
                definition.inputs = parse_xml_data(node, "stanza")
            else:
                definition.metadata[node.tag] = node.text

        return definition
项目:code-uai16    作者:thanhan    | 项目源码 | 文件源码
def get_pub_dic_xml(file_name = 'data/proton-beam-all.xml'):
    tree = ET.parse(file_name)
    root = tree.getroot()[0]

    # Create dic of : id -> text features
    pub_dic = {}
    for pub in root:
        rec_number = int (get_text (pub.find('rec-number')))
        abstract   = get_text (pub.find('abstract'))
        title      = get_text (pub.find('titles')[0])
        text = title + abstract
        for kw in pub.find('keywords'):
            text = text + kw.text + ' '
        pub_dic[rec_number] = text

    return pub_dic
项目:gns3-documentation-template    作者:GNS3    | 项目源码 | 文件源码
def youtube_channel(channel_id):
    import xml.etree.ElementTree as ET
    import urllib.request
    import datetime

    url = "https://www.youtube.com/feeds/videos.xml?max-results=50&channel_id=" + channel_id
    videos = []
    with urllib.request.urlopen(url) as f:
        root = ET.parse(f).getroot()
        for entry in root.iter('{http://www.w3.org/2005/Atom}entry'):
            video = {}
            video['id'] = entry.find('{http://www.youtube.com/xml/schemas/2015}videoId').text
            video['date'] = datetime.datetime.strptime(entry.find('{http://www.w3.org/2005/Atom}published').text, '%Y-%m-%dT%H:%M:%S+00:00')
            video['title'] = entry.find('{http://www.w3.org/2005/Atom}title').text
            videos.append(video)
    return videos
项目:gns3-documentation-template    作者:GNS3    | 项目源码 | 文件源码
def youtube_channel(channel_id):
    import xml.etree.ElementTree as ET
    import urllib.request
    import datetime

    url = "https://www.youtube.com/feeds/videos.xml?max-results=50&channel_id=" + channel_id
    videos = []
    with urllib.request.urlopen(url) as f:
        root = ET.parse(f).getroot()
        for entry in root.iter('{http://www.w3.org/2005/Atom}entry'):
            video = {}
            video['id'] = entry.find('{http://www.youtube.com/xml/schemas/2015}videoId').text
            video['date'] = datetime.datetime.strptime(entry.find('{http://www.w3.org/2005/Atom}published').text, '%Y-%m-%dT%H:%M:%S+00:00')
            video['title'] = entry.find('{http://www.w3.org/2005/Atom}title').text
            videos.append(video)
    return videos
项目:gps_track_pod    作者:iwanders    | 项目源码 | 文件源码
def parse_file(self):
        if self.path.endswith("gz"):
            f = gzip.GzipFile(self.path)
        else:
            f = open(self.path)
        tree = ET.parse(f)
        f.close()
        root = tree.getroot()
        current_urbs = {}
        for child in root:
            p = USBPacket(child)
            urb_id = p["usb.urb_id"]
            urb_status = p["usb.urb_status"]
            urb_type = p["usb.urb_type"]
            if (urb_type == URB_TYPE_SUBMIT):
                current_urbs[urb_id] = p
            if (urb_type == URB_TYPE_COMPLETED):
                if (urb_id not in current_urbs):
                    print("Urb id not present: {:x}".format(urb_id))
                else:
                    submit = current_urbs[urb_id]
                    completed = p
                    self.usb_transaction(submit, completed)
                    del current_urbs[urb_id]
项目:mx-rfcn    作者:giorking    | 项目源码 | 文件源码
def parse_voc_rec(filename):
    """
    parse pascal voc record into a dictionary
    :param filename: xml file path
    :return: list of dict
    """
    import xml.etree.ElementTree as ET
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_dict = dict()
        obj_dict['name'] = obj.find('name').text
        obj_dict['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_dict['bbox'] = [int(bbox.find('xmin').text),
                            int(bbox.find('ymin').text),
                            int(bbox.find('xmax').text),
                            int(bbox.find('ymax').text)]
        objects.append(obj_dict)
    return objects
项目:adversarial-frcnn    作者:xiaolonw    | 项目源码 | 文件源码
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        obj_struct['pose'] = obj.find('pose').text
        obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text),
                              int(bbox.find('ymin').text),
                              int(bbox.find('xmax').text),
                              int(bbox.find('ymax').text)]
        objects.append(obj_struct)

    return objects
项目:faster-rcnn-resnet    作者:Eniac-Xie    | 项目源码 | 文件源码
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        obj_struct['pose'] = obj.find('pose').text
        obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text),
                              int(bbox.find('ymin').text),
                              int(bbox.find('xmax').text),
                              int(bbox.find('ymax').text)]
        objects.append(obj_struct)

    return objects
项目:labelImage    作者:tsuzukit    | 项目源码 | 文件源码
def parseXML(self):
        assert self.filepath.endswith(XML_EXT), "Unsupport file format"
        parser = etree.XMLParser(encoding=ENCODE_METHOD)
        xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
        filename = xmltree.find('filename').text
        try:
            verified = xmltree.attrib['verified']
            if verified == 'yes':
                self.verified = True
        except KeyError:
            self.verified = False

        for object_iter in xmltree.findall('object'):
            bndbox = object_iter.find("bndbox")
            label = object_iter.find('name').text
            # Add chris
            difficult = False
            if object_iter.find('difficult') is not None:
                difficult = bool(int(object_iter.find('difficult').text))
            self.addShape(label, bndbox, difficult)
        return True
项目:ssd.pytorch    作者:amdegroot    | 项目源码 | 文件源码
def parse_rec(filename):
    """ Parse a PASCAL VOC xml file """
    tree = ET.parse(filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        obj_struct['pose'] = obj.find('pose').text
        obj_struct['truncated'] = int(obj.find('truncated').text)
        obj_struct['difficult'] = int(obj.find('difficult').text)
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text) - 1,
                              int(bbox.find('ymin').text) - 1,
                              int(bbox.find('xmax').text) - 1,
                              int(bbox.find('ymax').text) - 1]
        objects.append(obj_struct)

    return objects
项目:ssd.pytorch    作者:amdegroot    | 项目源码 | 文件源码
def pull_anno(self, index):
        '''Returns the original annotation of image at index

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.

        Argument:
            index (int): index of img to get annotation of
        Return:
            list:  [img_id, [(label, bbox coords),...]]
                eg: ('001718', [('dog', (96, 13, 438, 332))])
        '''
        img_id = self.ids[index]
        anno = ET.parse(self._annopath % img_id).getroot()
        gt = self.target_transform(anno, 1, 1)
        return img_id[1], gt
项目:text    作者:pytorch    | 项目源码 | 文件源码
def clean(path):
        for f_xml in glob.iglob(os.path.join(path, '*.xml')):
            print(f_xml)
            f_txt = os.path.splitext(f_xml)[0]
            with io.open(f_txt, mode='w', encoding='utf-8') as fd_txt:
                root = ET.parse(f_xml).getroot()[0]
                for doc in root.findall('doc'):
                    for e in doc.findall('seg'):
                        fd_txt.write(e.text.strip() + '\n')

        xml_tags = ['<url', '<keywords', '<talkid', '<description',
                    '<reviewer', '<translator', '<title', '<speaker']
        for f_orig in glob.iglob(os.path.join(path, 'train.tags*')):
            print(f_orig)
            f_txt = f_orig.replace('.tags', '')
            with io.open(f_txt, mode='w', encoding='utf-8') as fd_txt, \
                    io.open(f_orig, mode='r', encoding='utf-8') as fd_orig:
                for l in fd_orig:
                    if not any(tag in l for tag in xml_tags):
                        fd_txt.write(l.strip() + '\n')
项目:xmlschema    作者:brunato    | 项目源码 | 文件源码
def test_to_dict_from_etree(self):
        vh_xml_tree = _ElementTree.parse('examples/vehicles/vehicles.xml')
        col_xml_tree = _ElementTree.parse('examples/collection/collection.xml')

        xml_dict = self.vh_schema.to_dict(vh_xml_tree)
        self.assertNotEqual(xml_dict, _VEHICLES_DICT)  # XSI namespace unmapped

        xml_dict = self.vh_schema.to_dict(vh_xml_tree, namespaces=self.namespaces)
        self.assertEqual(xml_dict, _VEHICLES_DICT)

        xml_dict = xmlschema.to_dict(vh_xml_tree, self.vh_schema.url, namespaces=self.namespaces)
        self.assertEqual(xml_dict, _VEHICLES_DICT)

        xml_dict = self.col_schema.to_dict(col_xml_tree)
        self.assertNotEqual(xml_dict, _COLLECTION_DICT)

        xml_dict = self.col_schema.to_dict(col_xml_tree, namespaces=self.namespaces)
        self.assertEqual(xml_dict, _COLLECTION_DICT)

        xml_dict = xmlschema.to_dict(col_xml_tree, self.col_schema.url, namespaces=self.namespaces)
        self.assertEqual(xml_dict, _COLLECTION_DICT)
项目:unsupervised-2017-cvprw    作者:imatge-upc    | 项目源码 | 文件源码
def _parse_annotation_xml(filepath):
    tree = et.parse(filepath)
    data = tree.getroot()[1]    # <data>
    for sf in data:             #     <sourcefile> ... find first non-empty sourcefile node
        if len(list(sf)) != 0:
            break
    file = sf[0]                #         <file>
    objs = sf[1:]               #         <object> ...

    num_objs   = len(objs)
    num_frames = int(file.find("./*[@name='NUMFRAMES']/*[@value]").attrib['value'])
    parsed_bbx = np.zeros([num_frames, num_objs, 4])
    for i, obj in enumerate(objs):  # iterate <object> nodes
        loc = obj.find("./*[@name='Location']")
        for bbx in loc:
            span = re.findall(r'\d+', bbx.attrib['framespan'])
            beg, end = int(span[0]), int(span[1])
            h = int(bbx.attrib['height'])
            w = int(bbx.attrib['width'])
            x = int(bbx.attrib['x'])
            y = int(bbx.attrib['y'])
            parsed_bbx[beg-1:end, i] = [h, w, x, y]

    return parsed_bbx
项目:ns3-rdma    作者:bobzhuyb    | 项目源码 | 文件源码
def translate_to_text(results_file, text_file):
    f = open(text_file, 'w')
    import xml.etree.ElementTree as ET
    et = ET.parse (results_file)
    for test in et.findall('Test'):
        node_to_text (test, f)

    for example in et.findall('Example'):
        result = example.find('Result').text
        name = example.find('Name').text
        if not example.find('Time') is None:
            time_real = example.find('Time').get('real')
        else:
            time_real = ''
        output = "%s: Example \"%s\" (%s)\n" % (result, name, time_real)
        f.write(output)

    f.close()

#
# A simple example of writing an HTML file with a test result summary.  It is 
# expected that this will eventually be made prettier as time progresses and
# we have time to tweak it.  This may end up being moved to a separate module
# since it will probably grow over time.
#
项目:PySIGNFe    作者:thiagopena    | 项目源码 | 文件源码
def _le_xml(self, arquivo):
        if arquivo is None:
            return False

        if not isinstance(arquivo, basestring):
            arquivo = etree.tounicode(arquivo)

        if arquivo is not None:
            if isinstance(arquivo, basestring): 
                if NAMESPACE_NFSE in arquivo:
                    arquivo = por_acentos(arquivo)
                if u'<' in arquivo:
                    self._xml = etree.fromstring(tira_abertura(arquivo))
                else:
                    arq = open(arquivo)
                    txt = ''.join(arq.readlines())
                    txt = tira_abertura(txt)
                    arq.close()
                    self._xml = etree.fromstring(txt)
            else:
                self._xml = etree.parse(arquivo)
            return True

        return False
项目:PySIGNFe    作者:thiagopena    | 项目源码 | 文件源码
def validar(self):
        arquivo_esquema = self.caminho_esquema + self.arquivo_esquema

        # Aqui é importante remover a declaração do encoding
        # para evitar erros de conversão unicode para ascii
        xml = tira_abertura(self.xml).encode(u'utf-8')

        esquema = etree.XMLSchema(etree.parse(arquivo_esquema))

        if not esquema.validate(etree.fromstring(xml)):
            for e in esquema.error_log:
                if e.level == 1:
                    self.alertas.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))
                elif e.level == 2:
                    self.erros.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))

        return esquema.error_log
项目:addon-lxdone    作者:OpenNebula    | 项目源码 | 文件源码
def xml_start(xml):
    'Stores $xml file in $dicc dictionary'

    def xml_parse(root, dicc, way=""):
        new_way = way + "/" + root.tag
        value = []
        try:
            value = dicc[new_way]
        except:
            value = []
        value.append(root.text)
        dicc.__setitem__(new_way, value)
        for new_root in root._children:
            xml_parse(new_root, dicc, new_way)

    tree = ET.parse(xml, parser=None)
    root = tree.getroot()
    dicc = {}
    xml_parse(root, dicc, way="")
    return dicc
项目:FileFinder    作者:Mondei1    | 项目源码 | 文件源码
def getByID(id):
    try:
        en_tree = ET.parse('lib/lang/en.xml')
        de_tree = ET.parse('lib/lang/de.xml')
    except FileNotFoundError:
        print(BOLD + RED + "ERROR: Didn't find en.xml or de.xml in lib/lang/\n" +
              "       Please clone this project again from GitHub!" + ENDC)
        sys.exit(0)

    string = '''.//*[@id='%ID%']'''
    string = str(string).replace("%ID%", id)

    if language.__contains__("de"):
        return de_tree.findall(string)[0].text
    else:
        return en_tree.findall(string)[0].text
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def append_result_files(self, dst_resultfile, kw_resultfile_list, dst_root='Testcase', childtag='Keyword'):
        """Append kw/system result files into a testcase result file"""
        try:
            finstring = ''
            for kw_file in kw_resultfile_list:
                if kw_file is not None and kw_file is not False:
                    tree = self.xml_utils().get_tree_from_file(kw_file)
                    self.root = tree.getroot()
                    for child in self.root:
                        if child.tag == childtag:
                            finstring = finstring+self.xml_utils().convert_element_to_string(child)
            tc_string = ' '
            if self.file_utils().fileExists(dst_resultfile):
                tc_tree = ET.parse(dst_resultfile)
                tc_root = tc_tree.getroot()
                for tc_child in tc_root:
                    tc_string = tc_string+self.xml_utils().convert_element_to_string(tc_child)
            finalresult = '\n'.join(['<{0}>'.format(dst_root), tc_string + finstring,
                                     '</{0}>'.format(dst_root)])
            with open(dst_resultfile, 'w') as resultfile:
                resultfile.write(finalresult)
                resultfile.flush()
                resultfile.close()
        except Exception, err:
            print_info('unexpected error: {0}'.format(str(err)))
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def parse_tree(self, node):
        """
            Method to parse ElementTree to ConfigurationElement Tree
        :param node:
        :return:
        """
        for key in node.attrib:
            self.attributes[key] = node.attrib[key]
        self.attributes['xml_tag'] = node.tag
        # self.attributes['xml_element'] = node

        for child in node:
            try:
                if not child.attrib['name'] in self.children:
                    child_config = ConfigurationElement(child.attrib['name'])
                    child_config.parse_data(child, root=False)
                    self.children[child.attrib['name']] = child_config
                else:
                    self.children[child.attrib['name']].parse_tree(child)
            except KeyError:
                print_error("No name attribute for node " + child.tag + ". Tree with root "
                            "at node " + child.tag + " not parsed.")
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def evaluate_argument_value(xpath_or_tagname, datafile):
    """This function takes checks if the given xpath_or_tagname exists in the
    datafile and returns its value. Else returns None."""
    tree = ET.parse(datafile)
    root = tree.getroot()
    if xpath_or_tagname.startswith(root.tag + "/"):
        xpath_or_tagname = xpath_or_tagname[len(root.tag + "/"):]
        try:
            xpath_or_tagname = root.find(xpath_or_tagname).text
        except Exception:
            print_error("Invalid xpath: {0}".format(root.tag + "/" + xpath_or_tagname))
            xpath_or_tagname = None
    else:
        print_error("Invalid xpath: {0}".format(xpath_or_tagname))
        xpath_or_tagname = None
    return xpath_or_tagname
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getChildTextbyParentAttribute (datafile, pnode, patt, pattval, cnode):
    """
    Seraches XML file for the parent node with a specific value. Finds the child node and returns
    its text
    datafile = xml file searched
    pnode = parent node
    patt = parent node attribute
    patval = parent node attribute value
    cnode = child node
    """
    tree = ElementTree.parse(datafile)
    root = tree.getroot()
    value = False
    for node in root.findall(pnode):
        attribute = node.get(patt)
        if attribute == pattval:
            cnode = node.find(cnode)
            if cnode is not None:
                value = cnode.text
            else:
                return None
            break
    return value
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getChildTextbyParentTag (datafile, pnode, cnode):
    """
    Seraches XML file for the first parent. Finds the child node and returns its text
    datafile = xml file searched
    pnode = parent node
    cnode = child node
    """
    value = False
    tree = ElementTree.parse(datafile)
    root = tree.getroot()
    node = root.find(pnode)
    if node is not None:
        child = node.find(cnode)
        if child is not None:
            value = child.text
            return value
        else:
            # print_info("could not find cnode under the given pnode")
            return value
    else:
        # print_info("could not find pnode in the provided file")
        return value
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getChildAttributebyParentTag (datafile, pnode, cnode, cattrib):
    """Find the attribute in child node by traversing through the parent node
    in the given file
    datafile = xml file searched
    pnode = parent node
    cnode = child node
    cattrob = child node attrib
    """
    tree = ElementTree.parse(datafile)
    root = tree.getroot()
    node = root.find(pnode)
    if node is not None:
        child = node.find(cnode)
        if child is not None:
            value = child.get(cattrib)
            return value
        else:
            # print_info("could not find cnode under the given pnode")
            return False
    else:
        # print_info("could not find pnode in the provided file")
        return False
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def verifyParentandChildrenMatch (datafile, pnode, cnode, cvalue, rnode, rvalue):
    """
    Searches XML file for the parent node. Finds the 1st child node and checks its value
    if value is a match, then search for second child and check if its value matches
    datafile = xml file searched
    pnode = parent node
    cnode = child node
    cvalue = child node value
    rnode = reference node
    rvalue = refernce node value
    """
    tree = ElementTree.parse(datafile)
    root = tree.getroot()
    status = False
    for node in root.findall(pnode):
        value = node.find(cnode).text
        if value == cvalue:

            if node.find(rnode) is not None:
                cnodev = node.find(rnode).text
                # print_debug("-D- cnodev: '%s', rvalue : '%s'" % (cnodev, rvalue))
                if cnodev == rvalue:
                    # print_debug("-D- BREAK END METHOD verifyParentandChildrenMatch_Status '%s'" % status)
                    return True
    return status
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getElementsListWithTagAttribValueMatch(datafile, tag, attrib, value):
    """
    This method takes an xml document as input and finds all the sub elements (parent/children)
    containing specified tag and an attribute with the specified value.

    Returns a list of matching elements.

    Arguments:
    datafile = input xml file to be parsed.
    tag = tag value of the sub-element(parent/child) to be searched for.
    attrib = attribute name for  the sub-element with above given tag should have.
    value = attribute value that the sub-element with above given tag, attribute should have.
    """
    element_list = []
    root = ElementTree.parse(datafile).getroot()
    for element in root.iterfind(".//%s[@%s='%s']" % (tag, attrib, value)):
        element_list.append(element)
    return element_list
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getElementWithTagAttribValueMatch(start, tag, attrib, value):
    """
    When start is an xml datafile, it finds the root and first element with:
        tag, attrib, value.
    Or when it's an xml element, it finds the first child element with:
        tag, attrib, value.
    If there is not a match, it returns False.
    """
    node = False
    if isinstance(start, (file, str)):
        # check if file exist here
        if file_Utils.fileExists(start):
            node = ElementTree.parse(start).getroot()
        else:
            print_warning('The file={0} is not found.'.format(start))
    elif isinstance(start, ElementTree.Element):
        node = start
    if node is not False and node is not None:
        elementName = ".//%s[@%s='%s']" % (tag, attrib, value)
        element = node.find(elementName)
    else:
        element = node
    return element
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getConfigElementTextWithSpecificXpath(datafile, xpath):
    """
    This method takes an xml document as input and finds the first sub element (parent/children)
    containing specified xpath which should be a filepath to a netconf config file

    Returns the element text attribute

    Arguments:
    parent = parent element
    xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
    """
    root = ElementTree.parse(datafile).getroot()
    elem1 = root.find(xpath).text

    elem2_root = ElementTree.parse(elem1)
    elem2 = elem2_root.find('config')
    elem2_string = ElementTree.tostring(elem2)
    return elem2_string
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def getChildElementWithSpecificXpath(start, xpath):
    """
    This method takes a xml file or parent element as input and finds the first child
    containing specified xpath

    Returns the child element.

    Arguments:
    start = xml file or parent element
    xpath = a valid xml path value as supported by python, refer https://docs.python.org/2/library/xml.etree.elementtree.html
    """
    node = False
    if isinstance(start, (file, str)):
        # check if file exist here
        if file_Utils.fileExists(start):
            node = ElementTree.parse(start).getroot()
        else:
            print_warning('The file={0} is not found.'.format(start))
    elif isinstance(start, ElementTree.Element):
        node = start
    if node is not False or node is not None:
        element = node.find(xpath)
    else:
        element = False
    return element
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def del_tags_from_xml(xml, tag_list=[]):
    """
        It deletes the tags either by their names or xpath

        Arguments:
            1.xml: It takes xml file path or xml string as input
            2.tag_list: It contains list of tags which needs to be removed
        Returns:
            It returns xml string
    """
    if os.path.exists(xml):
        tree = ElementTree.parse(xml)
        root = tree.getroot()
    else:
        root = ElementTree.fromstring(xml)
    for tag in tag_list:
        if 'xpath=' in tag:
            tag = tag.strip('xpath=')
            req_tags = getChildElementsListWithSpecificXpath(root, tag)
        else:
            req_tags = getChildElementsListWithSpecificXpath(root, ".//{0}".format(tag))
        recursive_delete_among_children(root, req_tags)

    xml_string = ElementTree.tostring(root, encoding='utf-8', method='xml')
    return xml_string
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def convert_xml_to_list_of_dict(file_name):
    """
        Takes xml file path as input and
        converts to list of dictionaries
        Arguments:
            file_name : It takes xml file path as input
        Returns:
            list_of_dict: list of dictionaries where keys
            are tag names and values are respective text of the tag.
    """
    tree = ElementTree.parse(file_name)
    root = tree.getroot()
    list_of_dict = []
    for child in root:
        subchild_dict = OrderedDict()
        for subchild in child:
            subchild_dict[subchild.tag] = subchild.text
        list_of_dict.append(subchild_dict)

    return list_of_dict

#2016/06/22 ymizugaki add begin
项目:warriorframework    作者:warriorframework    | 项目源码 | 文件源码
def get_node(filename, node_name):
    """ Searches for the specified node in the xml tree.
    return type: xml.etree.ElementTree.Element

    :Arguments:

    1. node_name (str) = Name of the node to be searched
    2. filename (str) = path of the .xml that has to be searched.

    :Returns:

    node = xml.etree.ElementTree.Element/boolean False

    """
    root = ElementTree.parse(filename).getroot()
    node = root.find(node_name)
    if node is not None:
        return node
    else:
        return False