User:Jarle Pahr/XML

From OpenWetWare
Jump to: navigation, search

Notes on Extensible Markup Language (XML):

XML in Python


The packages ElementTree and lxml uses an "Element Tree" view of .xml files differing from the Document Object Model (DOM).

The Element class in lxml contains the following attributes:

  • .tag: The name of the element.
  • .text: The text inside the element, up to the first child element. Equals None if no text.
  • .tail: The text following the element.
  • .attrib: Python dictionary with the elements attributes and their values.

ElementTree methods:

Element.find(match): Finds first subelement matching the argument, which can be either a path or tag name.

Element.findall(elementtype) : Get all child elements of the type elementtype.

Element.get(name): get the attribute value of the attribute name.

Element.attrib.keys(): Show attribute keys of the element.

Element.iterfind(): Finds all matching (by tag or path) subelements and returns an iterable.

Element.items() : Returns attributes and their values Element.set(key, value) : Set attribute to value




/doc/chapter[5]/section[2] : Selection section #2 of chapter #5.

para[@type="warning"] select para node with type attribute with value "warning".

Seee also:

  • SBML
  • JSON

XML to JSON converters