XML Parsing Error: junk after document element. Rails builder

Asked
Viewd1686

1

I am trying to send send an xml Doc from an action

The XML is created by the following method

def media_xml 
    x = Builder::XmlMarkup.new
    x.instruct!
    x.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    x.options{
        x.videos{
            for m in self.media
                x.imageName("static-video-image.png", "target"=>"_self", "html"=>"", "flv"=> m.filename+".flv", "autoStart"=>"false")
            end
        }
    }
    x
end

In the controller I use the following

def media
    @inspection = @tag.inspections.find params[:id]
    respond_to do |format|
        format.html { render :nothing => true }
        format.xml { render :xml => @inspection.media_xml }
    end
end

And the following XML is the result

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<options>
  <videos>
    <imageName html="" flv="3504_1245270846028.flv" autoStart="false" target="_self">static-video-image.png</imageName>
  </videos>
</options>

<respond_to?:to_xml/><to_xml/> 

Because of the "" the parser on the other end gives the following error

XML Parsing Error: junk after document element

Why does rails put this in the document and how do i get rid of it?

Thank you!

2 ответов

4

Turns out that what was happening is the Builder::XmlMarkup.new was being returned from the media_xml method

This caused any subsiquent calls on that object to add more tags instead of calling the function.

def media_xml 
    x = Builder::XmlMarkup.new
    x.instruct!
    x.declare! :DOCTYPE, :html, :PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    x.options{
            x.videos{
                    for m in self.media
                            x.imageName("static-video-image.png", "target"=>"_self", "html"=>"", "flv"=> m.filename+".flv", "autoStart"=>"false")
                    end
            }
    }
    #x <= removing this fixed the problem
end
1

Привет, большое спасибо! У меня была такая же проблема, и я чесал голову, пытаясь понять, что же, черт возьми, происходит.

У меня была такая простая функция

 def media_xml
  request =  Builder::XmlMarkup.new

  request.instruct! :xml, :version => "1.0", :encoding=>"UTF-8"

  request.favourites{
    request.food("banana") 
  }

  render :xml => media_xml
end
 

.. и он давал ту же глупую ошибку. Итак, после прочтения вашего сообщения я разделил конструктор xml на другую функцию

 def build_xml
  request =  Builder::XmlMarkup.new

  request.instruct! :xml, :version => "1.0", :encoding=>"UTF-8"

  request.favourites{
    request.food("banana") 
  }
end
 

Оказывается, даже если я помещу одну строку в конце функции build_xml (), например, «запрос на возврат», она создаст ошибку, поскольку попытается добавить ее к объекту XML.