Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting XML from HTML?

Reference page

The XML is embedded under the <pre> tag of the returned HTML page. I can extract the contents of the <pre> tag, but I am unable to convert this to XML correctly. I tried using the to_xml method of the NodeSet class, but it seems that the line endings (\n) are messing up the parsing.

Here is a snippet of my code:

url = "http://www.ncbi.nlm.nih.gov/pubmed/?term=NS044283[GR]&dispmax=200&report=xml"
doc = Nokogiri::XML(open(url))
pre = doc.xpath('//pre')
xml = pre.to_xml
contents = Nokogiri::XML(xml)
articles = contents.xpath('\\PubmedArticle')
(article = [])
like image 786
RailinginDFW Avatar asked Aug 28 '26 03:08

RailinginDFW


2 Answers

Since you're going to use Nokogiri to parse it anyway, just call content instead of to_xml:

require 'nokogiri'
require 'open-uri'
url = "http://www.ncbi.nlm.nih.gov/pubmed/?term=NS044283[GR]&dispmax=200&report=xml"
doc = Nokogiri::XML(open(url))
pre = doc.xpath('//pre')
xml = "<root>" + pre.text + "</root>"
contents = Nokogiri::XML(xml)
articles = contents.css('PubmedArticle')
puts contents.css('ArticleTitle').map{|x| x.content}.count   
=> 25
like image 153
Jesse Wolgamott Avatar answered Aug 29 '26 16:08

Jesse Wolgamott


The document being retrieved isn't valid XML or HTML. Shame on those who created it.

Here's the first 200 characters, showing some confusion on their part:

require 'open-uri'
url = "http://www.ncbi.nlm.nih.gov/pubmed/?term=NS044283[GR]&dispmax=200&report=xml"
puts open(url).read[0..200]

which returns:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<pre>
&lt;PubmedArticle&gt;
    &lt;Medl

Luckily, or, maybe determinedly, Nokogiri works around that by being somewhat lenient with malformed HTML.

like image 40
the Tin Man Avatar answered Aug 29 '26 16:08

the Tin Man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!