Skip to content

Parser

pybeerxml.parser.Parser

Reads BeerXML files or strings and returns a list of Recipe objects.

A single BeerXML document may contain multiple <RECIPE> elements; all three parse methods always return a list. Unknown XML fields are logged at ERROR level and silently ignored so that non-standard files do not raise.

Examples:

>>> from pybeerxml import Parser
>>> parser = Parser()
>>> recipes = parser.parse("recipe.beerxml")
>>> for recipe in recipes:
...     print(recipe.name, recipe.og)

node_to_object(node, beerxml_object)

Map a single XML node onto an object attribute.

The node tag is lower-cased and used as the attribute name. Numeric string values are coerced to float automatically. The BeerXML field YIELD is mapped to _yield because yield is a Python keyword.

Parameters:

Name Type Description Default
node Element

XML element to map.

required
beerxml_object BeerXMLObject

Target object to receive the attribute value.

required

nodes_to_object(nodes, beerxml_object)

Map all child XML nodes onto an object's attributes.

Parameters:

Name Type Description Default
nodes Element

Parent XML element whose children will be mapped.

required
beerxml_object BeerXMLObject

Target object to receive the attribute values.

required

parse(xml_file)

Parse a BeerXML file from disk.

Parameters:

Name Type Description Default
xml_file str

Path to the .beerxml file.

required

Returns:

Type Description
list[Recipe]

A list of Recipe objects found in the file.

Raises:

Type Description
FileNotFoundError

If xml_file does not exist.

ParseError

If the file is not valid XML.

parse_from_string(xml_string)

Parse BeerXML content from a string.

Parameters:

Name Type Description Default
xml_string str

A valid BeerXML document as a string.

required

Returns:

Type Description
list[Recipe]

A list of Recipe objects found in the document.

Raises:

Type Description
ParseError

If xml_string is not valid XML.

parse_recipe(recipe_node)

Parse a single <RECIPE> element into a Recipe object.

Parameters:

Name Type Description Default
recipe_node Element

The <RECIPE> XML element.

required

Returns:

Type Description
Recipe

A populated Recipe instance.

parse_tree(tree)

Parse an already-constructed ElementTree.

Useful when you need full control over XML loading (e.g. custom encoding handling).

Parameters:

Name Type Description Default
tree ElementTree[Any]

A parsed XML tree.

required

Returns:

Type Description
list[Recipe]

A list of Recipe objects found in the tree.