Forum: Ruby on Rails Display XML file, with some formatting.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (Guest)
on 2006-05-30 19:01
Hi.

Im quite new to this Rails thing, but it seems like a good product so
why not try it out whit a little prototype i need to build.

What i want is to load any given XML file, and display it with some
formatting.
I have some XSD shemes to validate the XML files against, but its not
vital rigth now.
But how should i approach this task?

At first it would proberly be a good idea to load the file into my rails
app. My guess is that i could use something like

file = File.new( "mydoc.xml" )
doc = REXML::Document.new file
require "rexml/document"

But is there a way that i (at first) just can go trough the entire XML
tree and display it (something like when jo open af XML file in IE/FF)?

And perhaps someone can point me to a place i can read about XML-file
manipulating with Rails.

Best Regards:
Lasse Thomsen
23172b6630dc631a134c9bad2fec2a39?d=identicon&s=25 Chris (Guest)
on 2006-05-30 19:13
Lasse Thomsen wrote:

> file = File.new( "mydoc.xml" )
> doc = REXML::Document.new file
> require "rexml/document"
>
That looks a bit backwards.

I believe the last two lines whould be swapped:

file = File.new( "mydoc.xml" )
require "rexml/document"
doc = REXML::Document.new file
doc.write($stdout,0) #=> writes the XML on STDOUT with indenting to show
structure

If you specify -1 (instead of 0) it doesn't do any indenting
See
http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/class...

cheers
Chris
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (Guest)
on 2006-05-30 19:30
> I believe the last two lines whould be swapped:
>
> file = File.new( "mydoc.xml" )
> require "rexml/document"
> doc = REXML::Document.new file
> doc.write($stdout,0) #=> writes the XML on STDOUT with indenting to show
> structure
>
> If you specify -1 (instead of 0) it doesn't do any indenting
> See
> http://ruby-doc.org/stdlib/libdoc/rexml/rdoc/class...
>
> cheers
> Chris

Thanks for help, where should i place the file so that my rails web
application can see it? perhaps in the pulic folder ? and then just make
a dir called xml, throw my xml file in there?

And read the file like: file = File.new("/xml/mydoc.xml") ?

Because i have just tried this and got the following error:
No such file or directory - /xml/mydoc.xml

Or is there some where i should edit to tell where my webroot is?

Best Regards:
Lasse Stig Thomsen
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (Guest)
on 2006-05-30 20:33
> Thanks for help, where should i place the file so that my rails web
> application can see it? perhaps in the pulic folder ? and then just make
> a dir called xml, throw my xml file in there?
>
> And read the file like: file = File.new("/xml/mydoc.xml") ?
>
> Because i have just tried this and got the following error:
> No such file or directory - /xml/mydoc.xml
>
> Or is there some where i should edit to tell where my webroot is?
>
> Best Regards:
> Lasse Stig Thomsen

Did some more diging, realised that the rails root is relative to from
where i start the webrick server.
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (lassethomsen)
on 2006-05-30 23:43
All my output i going to the ruby console instead of the browser.
If i do some render_text around the doc.write($stdout,0) the browser
shows the XML file in one line with no indentation.

But in the ruby console there is still the correct display?

How do i direct the stdout to the browser?
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (lassethomsen)
on 2006-05-31 19:10
Lasse Thomsen wrote:
> All my output i going to the ruby console instead of the browser.
> If i do some render_text around the doc.write($stdout,0) the browser
> shows the XML file in one line with no indentation.
>
> But in the ruby console there is still the correct display?
>
> How do i direct the stdout to the browser?

Not anyone who can help me read an xml file and display the content in
the browser? the stdout solutiuon seems to be a dead end.
1e3a20bf0afbde2d07f690363dcd2705?d=identicon&s=25 Michel Benevento (Guest)
on 2006-05-31 20:15
(Received via mailing list)
>
> Lasse Thomsen wrote:

> Not anyone who can help me read an xml file and display the content in
> the browser? the stdout solutiuon seems to be a dead end.
>


You should apply an xsl to it I think:

put this in your controller:

require 'xml/xslt'

 def show_xml
    xslt = XML::XSLT.new()
    xslt.xsl = File.open('path/to/xslfile.xsl', 'r').read
    xslt.xml = File.open('path/to/xmlfile.xml', 'r').read
    render_text xslt.serve()
  end

and call <%= render_component :action => 'show_text' %> in your view.

You may have to install the xml/xsl gem (I forget)

HTH,
Michel

Lasse Thomsen <lassethomsen@...> writes:
7f9987921820d421b6baf4a7e989ad4c?d=identicon&s=25 Lasse Thomsen (lassethomsen)
on 2006-05-31 21:19
Michel Benevento wrote:
>
> You should apply an xsl to it I think:
>
> put this in your controller:
>
> require 'xml/xslt'
>
>  def show_xml
>     xslt = XML::XSLT.new()
>     xslt.xsl = File.open('path/to/xslfile.xsl', 'r').read
>     xslt.xml = File.open('path/to/xmlfile.xml', 'r').read
>     render_text xslt.serve()
>   end
>
> and call <%= render_component :action => 'show_text' %> in your view.
>
> You may have to install the xml/xsl gem (I forget)

Thanks, this is just what i have been looking for. Atleast it looks like
the way i would want to go. Tomorrow i will create til XSL files for my
XML. And later look into how i should validate them against som XSD
schema's i have. If anyone has hints in validating agains XSD files i
would be nice to hear, but at first im gonna make the displaying work.
Maybe ill start get more hang on RoR when i get this working. (you learn
a little everyday, and im working on the "Depot" webshop from the agile
web dev with rails book, so i hope to catch it all up quick)
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-05-31 21:59
(Received via mailing list)
Hi Michel,

Michel Benevento wrote:

> You may have to install the xml/xsl gem (I forget)

Where can I get more info on this gem?  I really need something like
this.
I checked and it's not included in the standard distro of Instant Rails
I'm
using.

Thanks,
Bill
1e3a20bf0afbde2d07f690363dcd2705?d=identicon&s=25 Michel Benevento (Guest)
on 2006-05-31 23:45
(Received via mailing list)
>
> > You may have to install the xml/xsl gem (I forget)
>
> Where can I get more info on this gem?  I really need something like this.
> I checked and it's not included in the standard distro of Instant Rails I'm
> using.


http://raa.ruby-lang.org/project/ruby-xslt/

Looks like you'll have to install this by hand...
7df957b83bab4999cedadda5485d438d?d=identicon&s=25 john paul (jonjone)
on 2011-08-11 12:40
Hi sir,


      I have one doubt i am doing one ror project , image upload process
, I did image upload and save the dir(public/images ) i can retrive the
image and display the browser , pls guide me

Thanks
john
30b1a136b1253c58a3aa359e59df32ca?d=identicon&s=25 Miquel Cubel (Guest)
on 2011-08-11 17:12
(Received via mailing list)
Hi,

     Some ides to check:
     - Check that the image is physically move the directory
app/public/image
     - Check the security permissions of the folder... maybe name image
can't be access to the user running the web server.
     - Check the size if it's zero... there can be size limit or problem
in the passenger / web service (typical webrick vs ngnix)
     - Does it fail only in production or also in development? Maybe
some different configurations?
     - The source in the image while printing... "<img
src="/images/imageName.extension">

     I hope will help you,

El 11/08/2011 12:40, john paul escribi:
This topic is locked and can not be replied to.