Ruby uses the Rexml library to parse XML format data _ruby topics

Source: Internet
Author: User
Tags xpath

Rexml is a processor written entirely in Ruby, with a variety of APIs, two classic APIs that are distinguished by dom-like and sax-like. The first is to read the entire file into memory and then store it as a layered form (that is, a tree). The second is "parse as you Go", which is more appropriate when your files are large and memory is limited.
Rexml has the following characteristics:

    • 100% write in Ruby
    • Can be used to parse sax and Dom
    • Light weight, less than 2000 lines of code
    • Provides complete API support
    • Ruby built in

Let's take a look at how to use it, assuming we have the following XML file:

<collection shelf= "New arrivals" > <movie title= "enemy Behind" > <type>war, thriller</type> < format>dvd</format> <year>2003</year> <rating>PG</rating> <stars>10</ Stars> <description>talk about a us-japan war</description> </movie> <movie title= "Transformer S "> <type>anime, Science fiction</type> <format>DVD</format> <year>1989</year > <rating>R</rating> <stars>8</stars> <description>a schientific fiction</ description> </movie> <movie title= "Trigun" > <type>anime, action</type> <format>dvd
 </format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>vash the stampede!</description> </movie> <movie title= "Ishtar" > <type> Comedy</type> <format>VHS</format> <rating>pg</rating> <stars>2</stars> <description>viewable boredom</description>
 </movie> </collection>

Parse Dom:

Require ' rexml/document '
include rexml
xmlfile = file.new ("Movies.xml")
xmldoc = document.new (xmlfile)
root = Xmldoc.root
puts "root element:" + root.attributes["shelf"]
Xmldoc.elements.each ("collection/ Movie ") {
 |e| puts" movie title: "+ e.attributes[" title "]
}
Xmldoc.elements.each (" Collection/movie/type " {
 |e| puts "Movie Type:" + e.text
}
Xmldoc.elements.each ("Collection/movie/description") {
 |e| Puts "Movie Description:" + E.text
}

Using XPath:

Require ' rexml/document '
include rexml
xmlfile = file.new ("Movies.xml")
xmldoc = Document.new (xmlfile
movie = Xpath.first (xmldoc, "//movie")
P movie
Xpath.each (xmldoc, "//type") {|e|
puts E.text} Names = Xpath.match (xmldoc, "//format"). Map {|x| X.text}
p names

To prepare for a rainy!

PS: About Rexml security issues
The Ruby official website issued a security notice on August 23: http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/, When Ruby is currently using the XML parsing library rexml parsing XML files with nested recursive elements, a denial of service attack will occur, causing the server to run out of resources!
All of the rails applications that use the XML file parsing feature have these flaws and need to be repaired. The fixes in rails are as follows:
1, Rails2.0.2 and previous versions
Download the repair file, copy it to the Rails_root/lib directory, and add the statement to the ENVIRONMENT.RB

Require ' Rexml-expansion-fix '

2, Rails 2.1.0 version
Download the repair file and copy it to the Rails_root/config/initializers directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.