1# 2# Copyright (C) 2018 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import logging 18 19from xml.etree import ElementTree 20 21 22def GetAttributes(xml_file, tag, attrs): 23 """Parses attributes in the given tag from xml file. 24 25 Args: 26 xml_file: The xml file object. 27 tag: a string, tag to parse from tag xml_file. 28 attrs: a list of attributes to look up inside the tag. 29 30 Returns: 31 A dict containing values of the attributes in tag from the xml file. 32 """ 33 result = {} 34 for _, elem in ElementTree.iterparse(xml_file, ("start", )): 35 if elem.tag == tag: 36 for attr in attrs: 37 if attr in elem.attrib: 38 result[attr] = elem.attrib[attr] 39 else: 40 logging.warning( 41 "Cannot find an attribute {} in <{}>".format( 42 attr, tag)) 43 return result 44