Parsing xml string into seprate line of string

HI,

Thanks for the help.

i have a xml and want to parse it line by line. suppose i have
abc = "
123
123
123
123
"

and i want to parse it line by line. there could be any number of line.
my output should be seperate string like
“”
“123”
“123”
“123”
“123”
“”

how can i acheive this one using any string function or regular
expression.

Please help.

Thanks
Saurabh

If you just want to split string by lines, abc.split(/\r?\n/) should
do the trick.

If what you actually want is to parse the XML data, I suggest using an
actual XML parser - Nokogiri, if you are doing some heavy lifting, or
xml-simple for, well, simple things.

– Matma R.

Hello,

give the following a try.

str =
"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't
forget me this weekend!</body>\n</note>"
str.each_line { |s| puts s }

Thanks
Josh

>________________________________
>From: saurabh a. <[email protected]>
>To: ruby-talk ML <[email protected]>
>Sent: Thursday, August 11, 2011 1:48 PM
>Subject: parsing xml string into seprate line of string
>
>HI,
>
>Thanks for the help.
>
>i have a xml and want to parse it line by line. suppose i have
>abc = "<a1>
> <a2>123</a2>
> <a3>123</a2>
> <a4>123</a2>
> <a5>123</a2>
> </a1>"
>
>and i want to parse it line by line. there could be any number of
line.
>my output should be seperate string like
>"<a1>"
>"<a2>123</a2>"
>"<a3>123</a3>"
>"<a4>123</a4>"
>"<a5>123</a2>"
>"</a1>"
>
>how can i acheive this one using any string function or regular
>expression.
>
>Please help.
>
>Thanks
>Saurabh
>
>–
>Posted via http://www.ruby-forum.com/.
>
>
>
>

Hello,

Sry I forgot to add the each on the end

Please try this following instead
str =
"<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't
forget me this weekend!</body>\n</note>"
str.each_line.each { |s| print s.strip }

I simply use the each_line to get and enumerator then call the each
method on that. Then inside the block make sure to strip the
'/n' of course you can do as you wish inside the block.

Thanks
Josh

>________________________________
>From: saurabh a. <[email protected]>
>To: ruby-talk ML <[email protected]>
>Sent: Thursday, August 11, 2011 1:48 PM
>Subject: parsing xml string into seprate line of string
>
>HI,
>
>Thanks for the help.
>
>i have a xml and want to parse it line by line. suppose i have
>abc = "<a1>
> <a2>123</a2>
> <a3>123</a2>
> <a4>123</a2>
> <a5>123</a2>
> </a1>"
>
>and i want to parse it line by line. there could be any number of
line.
>my output should be seperate string like
>"<a1>"
>"<a2>123</a2>"
>"<a3>123</a3>"
>"<a4>123</a4>"
>"<a5>123</a2>"
>"</a1>"
>
>how can i acheive this one using any string function or regular
>expression.
>
>Please help.
>
>Thanks
>Saurabh
>
>–
>Posted via http://www.ruby-forum.com/.
>
>
>
>