XmlParsers

Various XML parsers. See also XmlModes for XML editing environments.

libxml2 (New in Emacs 24) and dom.el (New in Emacs 25)

Starting with Emacs 24.1, Emacs is by default compiled to use the library libxml2, such as in the function libxml-parse-xml-region. Like the included xml.el library, it creates a list structure of each node in the XML or HTML tree.

Starting with Emacs 25.1,there is a new library, dom.el, for working with the output of the XML and HTML parsing functions (including that from xml.el). That output is a list-based representation of the standardized Document Object Model, or “DOM”.

These features are used, for example, in eww, a web browser included with Emacs.

Documentation is available on-line at the following locations:

A basic example:

(let* ((xml-dom-tree (with-temp-buffer
                       (insert-file-contents "my-file.xml")
                       (libxml-parse-xml-region (point-min) (point-max))))
       ;; Get first level children with "img" tag.
       (img-nodes (dom-by-tag xml-dom-tree 'img))
  ;; Further processing
  ...)

xml.el

This parser by EmmanuelBriot is part of Emacs since version 21, and is part of Gnus.

Docs: http://www.gnu.org/software/emacs/manual/html_node/elisp/Parsing-HTML_002fXML.html

Here’s a link to the file in Emacs’s sources:

See XmlParserExamples for some usage examples.

xml-parse.el

This parser by JohnWiegley is available from his homepage. It is probably deprecated. The data structure returned looks very much like the one returned by xml.el, but element names are strings instead of symbols. On the other hand, it contains additional code to pretty-print the xml data structures such that it can be used as an XML indentation fix.

You can get the source from here:

dom.el

The XML parsers create a lisp data structure for you and they provide access methods to this data structure. The following is a DOM implementation – you feed it the result of xml.el and then you can use the standard DOM interface.

You can get the source from here:

xpath.el

If you work with a DOM, a lot of the work you will be doing involves finding nodes in the document tree. Instead of looping through it yourself, writing lots of awkward and brittle code, you could use XPATH to do this for you. XPATH allows you to use path-like strings to specify nodes.

The following code, for example, reads an XML file, creates a DOM, and uses an XPATH to find chapter titles.

    (let* ((data (car (xml-parse-file "sample.xml")))
           (doc (dom-make-document-from-xml data)))
      (xpath-resolve (dom-document-element doc)
                     "descendant::chapter/child::title"))

You can get the source from here:

This requires partial (or full) installation of CEDET (see below), as well as:

To use xpath.el you need Wisent, a Bison-like parser generator in Elisp. Wisent is part of the Semantic package (see: SemanticBovinator), but as far as I can see it is not in the released version of Semantic. You can get it along with the entire CEDET beta tar-ball: http://sourceforge.net/project/showfiles.php?group_id=17886&release_id=192965

As far as I can see, you need only these three files:

If your Emacs version already has CEDET pre-packaged with it (e.g., 23.3), xpath-parser.el may be edited to require the correct symbols to take advantage of the pre-packaged Wisent:

  1. Go to your Emacs Lisp directory (e.g., <Emacs installation directory>\lisp in Windows).
  2. Look for the Wisent package in the CEDET package (e.g., <Emacs installation directory>\lisp\cedet\semantic\wisent).
  3. Look for and open the file where wisent-parse is defined.
  4. Note that file’s provide line (e.g., (provide ‘semantic/wisent/wisent)).
  5. Look for and open the file where wisent-compile-grammar is defined.
  6. Note that file’s provide line (e.g., (provide ‘semantic/wisent/comp)).
  7. Edit xpath-parser.el so that the correct provided symbols are required (e.g., (require ‘semantic/wisent/wisent) and (require ‘semantic/wisent/comp) instead of ‘wisent and ‘wisent-comp)).

Note that most of the xpath-parser is generated by a BNF file:

The xpath-parser requires wisent, which is part of the SemanticBovinator (CVS only, at the end of 2001).

Uh, oh! I modified the s-expr grammar in xpath-parser.el without being aware of this … :-/ – OliverScholz

Related links:

xslt.el

XSLT and XPATH together make an XSL engine. XEmacs comes with xslide and xslt-process.

Related links:


CategoryCode CategoryXML CategoryExtensions CategoryData