Bio.Entrez.Parser module

Parser for XML results returned by NCBI’s Entrez Utilities.

This parser is used by the read() function in Bio.Entrez, and is not intended be used directly.

The question is how to represent an XML file as Python objects. Some XML files returned by NCBI look like lists, others look like dictionaries, and others look like a mix of lists and dictionaries.

My approach is to classify each possible element in the XML as a plain string, an integer, a list, a dictionary, or a structure. The latter is a dictionary where the same key can occur multiple times; in Python, it is represented as a dictionary where that key occurs once, pointing to a list of values found in the XML file.

The parser then goes through the XML and creates the appropriate Python object for each element. The different levels encountered in the XML are preserved on the Python side. So a subelement of a subelement of an element is a value in a dictionary that is stored in a list which is a value in some other dictionary (or a value in a list which itself belongs to a list which is a value in a dictionary, and so on). Attributes encountered in the XML are stored as a dictionary in a member .attributes of each element, and the tag name is saved in a member .tag.

To decide which kind of Python object corresponds to each element in the XML, the parser analyzes the DTD referred at the top of (almost) every XML file returned by the Entrez Utilities. This is preferred over a hand- written solution, since the number of DTDs is rather large and their contents may change over time. About half the code in this parser deals with parsing the DTD, and the other half with the XML itself.

class Bio.Entrez.Parser.NoneElement(tag, attributes, key=None)

Bases: object

NCBI Entrez XML element mapped to None.

__init__(self, tag, attributes, key=None)

Create a NoneElement.

__eq__(self, other)

Define equality with other None objects.

__ne__(self, other)

Define non-equality.

__repr__(self)

Return a string representation of the object.

__hash__ = None
class Bio.Entrez.Parser.IntegerElement(value, tag, attributes, key=None)

Bases: int

NCBI Entrez XML element mapped to an integer.

static __new__(cls, value, tag, attributes, key=None)

Create an IntegerElement.

__repr__(self)

Return a string representation of the object.

class Bio.Entrez.Parser.StringElement(value, tag, attributes, key=None)

Bases: str

NCBI Entrez XML element mapped to a string.

static __new__(cls, value, tag, attributes, key=None)

Create a StringElement.

__repr__(self)

Return a string representation of the object.

class Bio.Entrez.Parser.UnicodeElement(value, tag, attributes, key=None)

Bases: str

NCBI Entrez XML element mapped to a unicode string.

static __new__(cls, value, tag, attributes, key=None)

Create a UnicodeElement.

__repr__(self)

Return a string representation of the object.

class Bio.Entrez.Parser.ListElement(tag, attributes, allowed_tags, key=None)

Bases: list

NCBI Entrez XML element mapped to a list.

__init__(self, tag, attributes, allowed_tags, key=None)

Create a ListElement.

__repr__(self)

Return a string representation of the object.

store(self, value)

Append an element to the list, checking tags.

class Bio.Entrez.Parser.DictionaryElement(tag, attrs, allowed_tags, repeated_tags=None, key=None)

Bases: dict

NCBI Entrez XML element mapped to a dictionaray.

__init__(self, tag, attrs, allowed_tags, repeated_tags=None, key=None)

Create a DictionaryElement.

__repr__(self)

Return a string representation of the object.

store(self, value)

Add an entry to the dictionary, checking tags.

exception Bio.Entrez.Parser.NotXMLError(message)

Bases: ValueError

Failed to parse file as XML.

__init__(self, message)

Initialize the class.

__str__(self)

Return a string summary of the exception.

exception Bio.Entrez.Parser.CorruptedXMLError(message)

Bases: ValueError

Corrupted XML.

__init__(self, message)

Initialize the class.

__str__(self)

Return a string summary of the exception.

exception Bio.Entrez.Parser.ValidationError(name)

Bases: ValueError

XML tag found which was not defined in the DTD.

Validating parsers raise this error if the parser finds a tag in the XML that is not defined in the DTD. Non-validating parsers do not raise this error. The Bio.Entrez.read and Bio.Entrez.parse functions use validating parsers by default (see those functions for more information).

__init__(self, name)

Initialize the class.

__str__(self)

Return a string summary of the exception.

class Bio.Entrez.Parser.DataHandler(validate, escape)

Bases: object

Data handler for parsing NCBI XML from Entrez.

global_dtd_dir = 'c:\\program files\\python37\\lib\\site-packages\\Bio\\Entrez\\DTDs'
global_xsd_dir = 'c:\\program files\\python37\\lib\\site-packages\\Bio\\Entrez\\XSDs'
local_dtd_dir = ''
local_xsd_dir = ''
__init__(self, validate, escape)

Create a DataHandler object.

read(self, handle)

Set up the parser and let it parse the XML results.

parse(self, handle)

Parse the XML in the given file handle.

xmlDeclHandler(self, version, encoding, standalone)

Set XML handlers when an XML declaration is found.

startNamespaceDeclHandler(self, prefix, uri)

Handle start of an XML namespace declaration.

endNamespaceDeclHandler(self, prefix)

Handle end of an XML namespace declaration.

schemaHandler(self, name, attrs)

Process the XML schema (before processing the element).

startElementHandler(self, tag, attrs)

Handle start of an XML element.

startRawElementHandler(self, name, attrs)

Handle start of an XML raw element.

startSkipElementHandler(self, name, attrs)

Handle start of an XML skip element.

endStringElementHandler(self, tag)

Handle end of an XML string element.

endRawElementHandler(self, name)

Handle start of an XML raw element.

endSkipElementHandler(self, name)

Handle start of an XML skip element.

endErrorElementHandler(self, name)

Handle start of an XML error element.

endElementHandler(self, name)

Handle end of an XML element.

endIntegerElementHandler(self, tag)

Handle end of an XML integer element.

characterDataHandlerRaw(self, content)

Handle character data as-is (raw).

characterDataHandlerEscape(self, content)

Handle character data by encoding it.

skipCharacterDataHandler(self, content)

Handle character data by skipping it.

parse_xsd(self, root)

Parse an XSD file.

elementDecl(self, name, model)

Call a call-back function for each element declaration in a DTD.

This is used for each element declaration in a DTD like:

<!ELEMENT       name          (...)>

The purpose of this function is to determine whether this element should be regarded as a string, integer, list, dictionary, structure, or error.

open_dtd_file(self, filename)

Open specified DTD file.

open_xsd_file(self, filename)

Open specified XSD file.

save_dtd_file(self, filename, text)

Save DTD file to cache.

save_xsd_file(self, filename, text)

Save XSD file to cache.

externalEntityRefHandler(self, context, base, systemId, publicId)

Handle external entity reference in order to cache DTD locally.

The purpose of this function is to load the DTD locally, instead of downloading it from the URL specified in the XML. Using the local DTD results in much faster parsing. If the DTD is not found locally, we try to download it. If new DTDs become available from NCBI, putting them in Bio/Entrez/DTDs will allow the parser to see them.

property directory

Directory for caching XSD and DTD files.