Skip to content

Dreamcat4's bindings for libxml-ruby. In development. This gem will provide convenience functions for both reading and writing XML in ruby. Implemented as wrapper for underlying libxml2 / SAX. Aims are to be faster (very much) and simpler (little bit) than hpricot. This project is a continuation / extension of libxml_helper.rb by Phil Bogle.

License

Notifications You must be signed in to change notification settings

constantine-nikolaou/libxml-bindings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libxml-bindings

This introduction text is taken from thebogles.com/blog/an-hpricot-style-interface-to-libxml#, credit to Phil Bogle.

Convenience functions

You can call to_xml_doc on any string to convert it into an XML::Document:

>> s = ‘<foo id=“1”><author>p. bogle</author><bar>content</bar><bar>cont2</bar></foo>’ >> root = s.to_xml_doc.root

The at() method returns the first Node matching the given xpath:

>> root.at(“author”) => <author>p. bogle</author>

The search() method returns a list of Nodes matching the given xpath:

>> root.search(“bar”) => [<bar>content</bar>, <bar>content2</bar>]

search() can also be called with a block to iterate through each of the matching nodes:

>> root.search(“bar”) do |bar| puts bar.xpath; end /foo/bar /foo/bar

Namespace helpers

The handling of default namespaces in libxml-ruby is awkward because you have to remember to pass along an array of namespace strings to every find() method call, and because you have to repeat yourself about the href of the default namespace.

The helpers add a register_default_namespace function that makes this simpler.

Suppose you had XML like the following

<?xml version=“1.0” encoding=“UTF-8”?> <feed xmlns=“www.w3.org/2005/Atom” xmlns:openSearch=“a9.com/-/spec/opensearchrss/1.0/” xmlns:gContact=“schemas.google.com/contact/2008" xmlns:gd=”schemas.google.com/g/2005“> <title type="text">Phil Bogle’s Contacts</title> … </feed>

Then you could say the following and have it work as expected:

root.register_default_namespace(“atom”) root.search(“atom:title”)

Copyright © 2009 dreamcat4. See LICENSE for details. Copyright © 2008 Phil Bogle. See LICENSE for details.

About

Dreamcat4's bindings for libxml-ruby. In development. This gem will provide convenience functions for both reading and writing XML in ruby. Implemented as wrapper for underlying libxml2 / SAX. Aims are to be faster (very much) and simpler (little bit) than hpricot. This project is a continuation / extension of libxml_helper.rb by Phil Bogle.

Resources

License

Stars

Watchers

Forks

Packages

No packages published