Remove newline characters in d anchors

hi

i am working on this code where i need to list the files in a dir and
store it in a xml, which i managed to do. however in the generated xml
there are newline characters (\n) appearing before and after the actual
text value of each node.

below is the code i am using

require 'rexml/document'
require 'find'

doc = REXML::Document.new
main = doc.add_element 'main'

main.attributes['name'] = ""

# Find.find('./'){|path| puts path[2,path.length] }
Find.find('./'){|path|
  if path != './dirlisting.rb'
    if ! File::directory? ( path )
      newfile = main.add_element 'file'
      newfile.text = path[2,path.length]
  end
  end
}

outfile = open('dirlist.xml', 'w')
doc.write(outfile, 0)

# open('dirlist.xml', 'w') { |f| f << doc}

below is the sample output in the xml wid newline characters before &
after each text value within the anchors
<main name=''>
<file>
340330_10150281813536470_96585976469_8235727_1982670115_o.jpg
</file>
<file>
507999446_JUN2009.pdf
</file>
<file>
8.10669690.00.00.100193.pdf
</file>
<file>
Asset List_Tracker.xls
</file>
<file>
BM.jpg
</file>
<file>
BM2.JPG
</file>
<file>
</main>

how can i get rid of those line breaks in each nodes and have a clean
node with just the text value?

Attachments:
http://www.ruby-forum.com/attachment/6825/dirlisting.rb

···

--
Posted via http://www.ruby-forum.com/.

hi

i am working on this code where i need to list the files in a dir and
store it in a xml, which i managed to do. however in the generated xml
there are newline characters (\n) appearing before and after the actual
text value of each node.

below is the code i am using

require 'rexml/document'
require 'find'

doc = REXML::Document.new
main = doc.add_element 'main'

main.attributes['name'] = ""

# Find.find('./'){|path| puts path[2,path.length] }
Find.find('./'){|path|
if path != './dirlisting.rb'
if ! File::directory? ( path )
newfile = main.add_element 'file'
newfile.text = path[2,path.length]

I think you want

newfile.text = path[2...path.length]

or

newfile.text = path[2..-1]

That won't solve your nl issue but is more correct.

end
end
}

outfile = open('dirlist.xml', 'w')
doc.write(outfile, 0)

You do not close the file properly. Better do

open('dirlist.xml', 'w') {|outfile| doc.write(outfile)}

how can i get rid of those line breaks in each nodes and have a clean
node with just the text value?

You want REXML::Formatters::Pretty#compact

irb(main):001:0> doc = REXML::Document.new
"<root><foo>123</foo><bar><foo>444</foo></bar></root>"
=> <UNDEFINED> ... </>
irb(main):002:0> fmt = REXML::Formatters::Pretty.new
=> #<REXML::Formatters::Pretty:0x10615890 @indentation=2, @level=0,
@ie_hack=false, @width=80, @compact=false>
irb(main):003:0> fmt.compact
=> false

irb(main):004:0> fmt.compact=true
=> true
irb(main):005:0> fmt.write(doc, $stdout)
<root>
  <foo>123</foo>
  <bar>
    <foo>444</foo>
  </bar>
</root>=> [<?xml ... ?>, <root> ... </>]

irb(main):006:0> fmt.compact=false
=> false
irb(main):007:0> fmt.write(doc, $stdout)
<root>
  <foo>
    123
  </foo>
  <bar>
    <foo>
      444
    </foo>
  </bar>
</root>=> [<?xml ... ?>, <root> ... </>]

Kind regards

robert

···

On Thu, Dec 8, 2011 at 8:54 AM, mythpills P. <pillay.mithun@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

i just made the first 2 edits which u suggested and it worked :slight_smile:

Thanks Robert for being there on every posts of mine and putting some
sense in my empty head

w.r.t 2 the format code not sure how i can implement in my program.. but
anyways the prob of newline is resolved.

will look forward to your support in my ruby adventure. respect!

···

--
Posted via http://www.ruby-forum.com/.