pandaman pandaman - 2 years ago 79
Ruby Question

Why is Nokogiri returning gibberish when parsing response from XML API?

I'm requesting data from an API and trying to parse with Nokogiri following the website (

But when I print the "parsed" data, it doesn't look like its parsed.

[#<Nokogiri::XML::Element:0x1101368 name="jobtitle" children=[#<Nokogiri::XML::Text:0x11008b4 "Software Engineer – Austin, TX">]>,...

The data is there but its surrounded by this type of content:


My code:

data = open("<api_url>")
@doc = Nokogiri::XML(data)
p @doc.xpath("//jobtitle")

What is happening here?
Does anyone have experience with Nokogiri and can advise on how to parse XML properly?

Note: This is being done in Rails.

Answer Source

You're printing out a Nokogiri object, not raw XML. Use .content to print out the nodes content, or .to_xml to print out XML from the parsed file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download