Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:53:41+00:00 2026-06-09T11:53:41+00:00

When I look in an XML file, it looks fine, and starts with <?xml

  • 0

When I look in an XML file, it looks fine, and starts with <?xml version="1.0" encoding="utf-16le" standalone="yes"?>

But when I read it in Ruby and print it to stout, there are two ?s in front of that: ??<?xml version="1.0" encoding="utf-16le" standalone="yes"?>

Where do these come from, and how do I remove them? Parsing it like this with REXML fails immediately. Removing the first to characters and then parsing it, gives me this error:

REXML::ParseException: #<REXML::ParseException: malformed XML: missing tag start
Line:
Position:
Last 80 unconsumed characters:
<?xml version="1.0" encoding="utf-16le" s>

What is the right way to handle this?

Edit: Below is my code. The ftp.get downloads the xml from an ftp server. (I wonder if that might be relevant.)

xml = ftp.get
puts xml
until xml[0,1] == "<"  # to remove the 2 invalid characters
  puts xml[0,2]
  xml.slice! 0
end
puts xml
document = REXML::Document.new(xml)

The last puts prints the correct xml. But because of the two invalid characters, I’ve got the feeling something else went wrong. It shouldn’t be necessary to remove anything. I’m at a loss what the problem might be, though.

Edit 2: I’m using Net::FTP to download the XML, but with this new method that lets me read the contents into a string instead of a file:

class Net::FTP

  def gettextcontent(remotefile, &block) # :yield: line
    f = StringIO.new()
    begin
      retrlines("RETR " + remotefile) do |line|
        f.puts(line)
        yield(line) if block
      end
    ensure
      f.close
      return f
    end
  end
end

Edit 3: It seems to be caused by StringIO (in Ruby 1.8.7) not supporting unicode. I’m not sure if there’s a workaround for that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:53:42+00:00Added an answer on June 9, 2026 at 11:53 am

    To answer my own question, the real problem here is that encoding support in Ruby 1.8.7 is lacking. StringIO is particular seems to make a mess of it. REXML also has trouble handling unicode in Ruby 1.8.7.

    The most attractive solution would be of course to upgrade to 1.9.3, but that’s not practical for this project right now.

    So what I ended up doing is, avoid StringIO and simply download to a file on disk, and then instead of processing the XML with REXML, use nokogiri instead.

    Together, that solves all my problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Xml Should look like: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <results> <version>1.0</version> <status>ok</status> <lastUpdate>2011-11-21 09:23:59.0</lastUpdate>
I have this PHP file: <?php header('Content-type: text/xml; charset=UTF-8'); ?><?xml version=1.0 encoding=UTF-8 ?> <Module>
Am creating an xml file using xmlwriter,i thought it works fine but when i
My XML file looks something like this: <MyXml> <Version> 9.3.2 </Version> <Resources> <Sets> <ItemCollection>
i have xsd and xml file. xml parse fine when validation is turned off.
I have defined a dp dimension in my XML file like so: <?xml version=1.0
I have the following script to look up an XML file and produce an
I have an xml file which looks like this: <section> <title> title of section
Our C++ application reads configuration data from XML files that look something like this:
I'm just starting to look into using LINQ for my database (and XML, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.