Ruby XML Format Data Parsing library Nokogiri using advanced _ruby topics

Source: Internet
Author: User
Tags uuid xmlns xpath


First, the basic grammar
1. Get the Nokogiri object directly as a string:

Html_doc = nokogiri::html (" 
 

Html_doc and Xml_doc here are Nokogiri files.

2. Nokogiri objects can also be obtained through file handles:

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

3. Can also be obtained directly from the website:

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

Second, XML file resolution instance
common ways to crawl fields from xml/html files:

Now there is a file called Shows.xml, which reads as follows:

 <root> <sitcoms> <sitcom> <name>married with children</name>
    <characters> <character>al bundy</character> <character>bud bundy</character> <character>marcy darcy</character> </characters> </sitcom> <sitcom> <name>p Erfect strangers</name> <characters> <character>larry appleton</character> <characte R>balki bartokomous</character> </characters> </sitcom> </sitcoms> <dramas> < drama> <name>the a-team</name> <characters> <character>john "Hannibal" Smith</char Acter> <character>templeton "Face" peck</character> <character> "B.A." Baracus</character> <character> "Howling Mad" murdock</character> </characters> </drama > </dramas> </root> 

If you want to find out all the contents of the character tag, you can do this:

@doc = Nokogiri::xml (File.Open ("Shows.xml"))
@doc. XPath ("//character")

XPath and CSS methods, which return a list of nodes, similar to an array, whose content is the node that matches the rules found from the file.

Check out the list of character nodes in the dramas node:

@doc. XPath ("//dramas//character")

More Readable CSS methods:

characters = @doc. css ("sitcoms name")
# => ["<name>married with Children</name>", "<name> Perfect strangers</name> "]

When the query results are known to be unique, if you want to return this result directly instead of the list, you can use At_xpath or at_css directly:

@doc. CSS ("dramas name"). "<name>the a-team</name>"
@doc. At_css ("dramas name")  # = > "<name>the a-team</name>"

Third, namespaces
namespaces play a big role in situations where multiple labels are available.
For example, there is such a parts.xml file:

<parts>
 <!--Alice ' s Auto parts Store-->
 <inventory xmlns= "http://alicesautoparts.com/" >
  <tire>all weather</tire>
  <tire>studded</tire>
  <tire>extra wide</ tire>
 </inventory>

 <!--Bob ' s Bike shop-->
 <inventory xmlns= "http://bobsbikes.com/" >
  <tire>street</tire>
  <tire>mountain</tire>
 </inventory>
</parts>

You can use a unique URL as a namespaces to differentiate between different tires tags:

@doc = Nokogiri::xml (File.read ("Parts.xml"))
car_tires = @doc. XPath ('//car:tire ', ' car ' => ') http:// alicesautoparts.com/')
bike_tires = @doc. XPath ('//bike:tire ', ' Bike ' => ' http://bobsbikes.com/')

To make namespace easier to use, Nokogiri automatically binds any namespace that is found at the root node.
Nokogiri automatically associates the provided URL, which can reduce the amount of code.
For example, there is such a atom.xml file:

<feed xmlns= "Http://www.w3.org/2005/Atom" >

 <title>example feed</title> <link
 " http://example.org/"/>
 <updated>2003-12-13T18:30:02Z</updated>
 <author>
  < Name>john doe</name>
 </author>
 <id>urn:uuid:60a76c80-d399-11d9-b93c-0003939e0af6 </id>

 <entry>
  <title>atom-powered Robots Run amok</title>
  <link href= "http ://example.org/2003/12/13/atom03 "/>
  <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
  <updated>2003-12-13T18:30:02Z</updated>
  <summary>some text.</summary>
 </entry>
</feed>

Following the convention mentioned above, xmlns is automatically bound and no longer manually assigned to xmlns:

@doc. XPath ('//xmlns:title ')
# => ["<title>example feed</title>", "<title>atom-powered Robots Run amok</title> "]

Similarly, the use of CSS:

@doc. CSS (' Xmlns|title ')

And when using CSS, if the namespaces name is xmlns, then the word itself can be ignored:

@doc. css (' title ')


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.