本文整理汇总了Python中xml.etree.cElementTree.ElementTree.remove方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.remove方法的具体用法?Python ElementTree.remove怎么用?Python ElementTree.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.remove方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: class
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import remove [as 别名]
class _BibleBooksCodesConverter:
"""
Class for reading, validating, and converting BibleBooksCodes.
This is only intended as a transitory class (used at start-up).
The BibleBooksCodes class has functions more generally useful.
"""
def __init__( self ): # We can't give this parameters because of the singleton
"""
Constructor: expects the filepath of the source XML file.
Loads (and crudely validates the XML file) into an element tree.
"""
self._filenameBase = "BibleBooksCodes"
# These fields are used for parsing the XML
self._treeTag = "BibleBooksCodes"
self._headerTag = "header"
self._mainElementTag = "BibleBookCodes"
# These fields are used for automatically checking/validating the XML
self._compulsoryAttributes = ()
self._optionalAttributes = ()
self._uniqueAttributes = self._compulsoryAttributes + self._optionalAttributes
self._compulsoryElements = ( "nameEnglish", "referenceAbbreviation", "referenceNumber" )
self._optionalElements = ( "expectedChapters", "SBLAbbreviation", "OSISAbbreviation", "SwordAbbreviation", "CCELNumber", "ParatextAbbreviation", "ParatextNumber", "NETBibleAbbreviation", "ByzantineAbbreviation", "possibleAlternativeBooks" )
#self._uniqueElements = self._compulsoryElements + self.optionalElements
self._uniqueElements = self._compulsoryElements # Relax the checking
# These are fields that we will fill later
self._XMLheader, self._XMLtree = None, None
self.__DataDicts = {} # Used for import
self.titleString = self.versionString = self.dateString = ''
# end of __init__
def loadAndValidate( self, XMLFilepath=None ):
"""
Loads (and crudely validates the XML file) into an element tree.
Allows the filepath of the source XML file to be specified, otherwise uses the default.
"""
if self._XMLtree is None: # We mustn't have already have loaded the data
if XMLFilepath is None:
XMLFilepath = os.path.join( "DataFiles", self._filenameBase + ".xml" )
self.__load( XMLFilepath )
if Globals.strictCheckingFlag:
self.__validate()
else: # The data must have been already loaded
if XMLFilepath is not None and XMLFilepath!=self.__XMLFilepath: logging.error( _("Bible books codes are already loaded -- your different filepath of '{}' was ignored").format( XMLFilepath ) )
return self
# end of loadAndValidate
def __load( self, XMLFilepath ):
"""
Load the source XML file and remove the header from the tree.
Also, extracts some useful elements from the header element.
"""
assert( XMLFilepath )
self.__XMLFilepath = XMLFilepath
assert( self._XMLtree is None or len(self._XMLtree)==0 ) # Make sure we're not doing this twice
if Globals.verbosityLevel > 2: print( _("Loading BibleBooksCodes XML file from '{}'...").format( self.__XMLFilepath ) )
self._XMLtree = ElementTree().parse( self.__XMLFilepath )
assert( self._XMLtree ) # Fail here if we didn't load anything at all
if self._XMLtree.tag == self._treeTag:
header = self._XMLtree[0]
if header.tag == self._headerTag:
self.XMLheader = header
self._XMLtree.remove( header )
if len(header)>1:
logging.info( _("Unexpected elements in header") )
elif len(header)==0:
logging.info( _("Missing work element in header") )
else:
work = header[0]
if work.tag == "work":
self.versionString = work.find("version").text
self.dateString = work.find("date").text
self.titleString = work.find("title").text
else:
logging.warning( _("Missing work element in header") )
else:
logging.warning( _("Missing header element (looking for '{}' tag)".format( self._headerTag ) ) )
if header.tail is not None and header.tail.strip(): logging.error( _("Unexpected '{}' tail data after header").format( element.tail ) )
else:
logging.error( _("Expected to load '{}' but got '{}'").format( self._treeTag, self._XMLtree.tag ) )
# end of __load
def __validate( self ):
"""
Check/validate the loaded data.
"""
assert( self._XMLtree )
uniqueDict = {}
for elementName in self._uniqueElements: uniqueDict["Element_"+elementName] = []
for attributeName in self._uniqueAttributes: uniqueDict["Attribute_"+attributeName] = []
expectedID = 1
for j,element in enumerate(self._XMLtree):
if element.tag == self._mainElementTag:
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from xml.etree.cElementTree import ElementTree [as 别名]
# 或者: from xml.etree.cElementTree.ElementTree import remove [as 别名]
class _BibleOrganizationalSystemsConverter:
"""
Class for handling and converting BibleOrganizationalSystems.
"""
def __init__( self ):
"""
Constructor: expects the filepath of the source XML file.
Loads (and crudely validates the XML file) into an element tree.
"""
self._filenameBase = "BibleOrganizationalSystems"
# These fields are used for parsing the XML
self._treeTag = "BibleOrganizationalSystems"
self._headerTag = "header"
self._mainElementTag = "BibleOrganizationalSystem"
# These fields are used for automatically checking/validating the XML
self._compulsoryAttributes = ( "type", )
self._optionalAttributes = ()
self._uniqueAttributes = ()
self._compulsoryElements = ( "referenceAbbreviation", "languageCode", )
self._optionalElements = ( "name", "publicationDate", "versificationSystem", "punctuationSystem", "bookOrderSystem", "booksNamesSystem", "derivedFrom", "usesText", )
self._uniqueElements = ()
self._allowedMultiple = ( "name", )
# These are fields that we will fill later
self.title, self.version, self.date = None, None, None
self.header, self._XMLtree = None, None
self.__dataDicts = None
# Get the data tables that we need for proper checking
self._ISOLanguages = ISO_639_3_Languages().loadData()
self._BibleBooksCodes = BibleBooksCodes().loadData()
self._BibleBookOrderSystems = BibleBookOrderSystems().loadData()
self._BiblePunctuationSystems = BiblePunctuationSystems().loadData()
self._BibleVersificationSystems = BibleVersificationSystems().loadData()
self._BibleBooksNamesSystems = BibleBooksNamesSystems().loadData()
# end of __init__
def __str__( self ):
"""
This method returns the string representation of a Bible book code.
@return: the name of a Bible object formatted as a string
@rtype: string
"""
result = ""
if self.title: result += ('\n' if result else '') + self.title
if self.version: result += ('\n' if result else '') + " Version: {}".format( self.version )
if self.date: result += ('\n' if result else '') + " Date: {}".format( self.date )
result += ('\n' if result else '') + " Num entries = {}".format( len(self._XMLtree) )
return result
# end of __str__
def loadAndValidate( self, XMLFilepath=None ):
"""
Loads (and crudely validates the XML file) into an element tree.
Allows the filepath of the source XML file to be specified, otherwise uses the default.
"""
if self._XMLtree is None: # We mustn't have already have loaded the data
if XMLFilepath is None:
XMLFilepath = os.path.join( "DataFiles", self._filenameBase + ".xml" )
self._load( XMLFilepath )
if Globals.strictCheckingFlag:
self._validate()
return self
# end of loadAndValidate
def _load( self, XMLFilepath ):
"""
Load the source XML file and remove the header from the tree.
Also, extracts some useful elements from the header element.
"""
assert( XMLFilepath )
self.XMLFilepath = XMLFilepath
assert( self._XMLtree is None or len(self._XMLtree)==0 ) # Make sure we're not doing this twice
if Globals.verbosityLevel > 2: print( _("Loading BibleOrganisationalSystems XML file from '{}'...").format( self.XMLFilepath ) )
self._XMLtree = ElementTree().parse( self.XMLFilepath )
assert( self._XMLtree ) # Fail here if we didn't load anything at all
if self._XMLtree.tag == self._treeTag:
header = self._XMLtree[0]
if header.tag == self._headerTag:
self.header = header
self._XMLtree.remove( header )
if len(header)>1:
logging.info( _("Unexpected elements in header") )
elif len(header)==0:
logging.info( _("Missing work element in header") )
else:
work = header[0]
if work.tag == "work":
self.version = work.find("version").text
self.date = work.find("date").text
self.title = work.find("title").text
else:
logging.warning( _("Missing work element in header") )
#.........这里部分代码省略.........