Display XML file, with some formatting


#1

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


#2

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/classes/REXML/Document.html#M000466

cheers
Chris


#3

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/classes/REXML/Document.html#M000466

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


#4

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.


#5

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.


#6

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:


#7

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?


#8

Michel B. 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)


#9

Hi Michel,

Michel B. 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


#10

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…


#11

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


#12

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… "

 I hope will help you,

El 11/08/2011 12:40, john paul escribi: