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 = [])
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
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>
<PubmedArticle>
<Medl
Luckily, or, maybe determinedly, Nokogiri works around that by being somewhat lenient with malformed HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With