User:Jarle Pahr/XML

From OpenWetWare

Jump to: navigation, search

Notes on Extensible Markup Language (XML): https://en.wikipedia.org/wiki/XML


http://stackoverflow.com/questions/3536893/what-are-the-pros-and-cons-of-xml-and-json



Contents

XML in Python

https://wiki.python.org/moin/PythonXml

http://docs.python.org/2/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

http://www.boddie.org.uk/python/XML_intro.html

http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python

http://oreilly.com/catalog/pythonxml/chapter/ch01.html

https://wiki.python.org/moin/MiniDom

http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/

lXML

http://lxml.de/3.3/lxmldoc-3.3.0beta3.pdf#chapter.7

https://pypi.python.org/pypi/lxml

http://lxml.de/3.3/lxmldoc-3.3.0beta3.pdf

http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/index.html

http://stackoverflow.com/questions/4808753/find-occurrence-using-multiple-attributes-in-elementtree-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

XPath

http://lxml.de/1.3/xpathxslt.html

http://www.w3.org/TR/xpath/

http://docs.python.org/3.3/library/xml.etree.elementtree.html#elementtree-xpath

http://lxml.de/xpathxslt.html

root.findall("./country/neighbor")

http://effbot.org/zone/element-xpath.htm

http://www.w3.org/TR/xpath/#section-Introduction

Examples:

/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

http://json.online-toolz.com/tools/xml-json-convertor.php

http://www.freeformatter.com/xml-to-json-converter.html

http://www.thomasfrank.se/xml_to_json.html

http://www.utilities-online.info/xmltojson/#.Us83F7SQySo

Namespaces

http://www.w3schools.com/xml/xml_namespaces.asp

http://stackoverflow.com/questions/14853243/parsing-xml-with-namespace-in-python-elementtree

Personal tools