Tutorial on using Nokogiri packages in Ruby to manipulate XML format data _ruby topics

Source: Internet
Author: User
Tags xpath

Installation

For Ubuntu, you need to install LIBXML2, libxslt these two components:

$ apt-get Install LIBXML2 libxslt

Then you can:

$ gem Install Nokogiri

Available options
Nokogiri provides some options for parsing files, which are commonly used:

    • Noblanks: Delete Empty node
    • Noent: Alternative Entities
    • NoError: Hide Error Reporting
    • STRICT: Accurate parsing, throwing an error when parsing to a file exception
    • Nonet: Disables any network connections during parsing.

Options can be used as examples (via block invocation):

doc = Nokogiri::xml (File.Open ("Blossom.xml")) do |config|
Config.strict.nonet End

Or

doc = Nokogiri::xml (File.Open ("Blossom.xml")) do |config|
Config.options = nokogiri::xml::P arseoptions::strict | Nokogiri::xml::P arseoptions::nonet End

Analytical

Can be parsed from files, strings, URLs, and so on. By these two methods nokogiri::html, Nokogiri::xml:

Read string:

Html_doc = nokogiri::html (" 
 

Read file:

f = File.Open ("Blossom.xml")
doc = Nokogiri::xml (f)
F.close

Read URL:

Require ' Open-uri '
doc = nokogiri::html (open ("http://www.threescompany.com/"))

Finding nodes

You can search using XPath and CSS selector: for example, given an XML:

<books>
 <book>
 <title>Stars</title>
 </book>
 <book>
 <title>Moon</title>
 </book>
</books>

Xpath:

@doc. XPath ("//title")

Css:

@doc. CSS ("book title")

Modify Node Contents

title = @doc. CSS ("book title") Firsto
title.content = ' new title '
puts @doc. to_html

# =>
...
 <title>new title</title>
...

Modifying the structure of a node

First_title = @doc. At_css (' title ')
Second_book = @doc. css (' book '). Last

# You can put the first title in the second book
First_ Title.parent = Second_book

# can also be placed freely.
second_book.add_next_sibling (first_title)

# can also modify the corresponding class
first_title.name = ' H2 '
first_title[' Class ']= ' Red_color '
puts @doc. to_html # => 

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.