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/.

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 Rex

Hello,

give the following a try.

str = &quot;&lt;note&gt;&#92;n&lt;to&gt;Tove&lt;/to&gt;&#92;n&lt;from&gt;Jani&lt;/from&gt;&#92;n&lt;heading&gt;Reminder&lt;/heading&gt;&#92;n&lt;body&gt;Don&#39;t forget me this weekend!&lt;/body&gt;&#92;n&lt;/note&gt;&quot;
str.each_line { |s| puts s }

Thanks
Josh

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

Hello,

Sry I forgot to add the each on the end

Please try this following instead
str = &quot;&lt;note&gt;&#92;n&lt;to&gt;Tove&lt;/to&gt;&#92;n&lt;from&gt;Jani&lt;/from&gt;&#92;n&lt;heading&gt;Reminder&lt;/heading&gt;&#92;n&lt;body&gt;Don&#39;t forget me this weekend!&lt;/body&gt;&#92;n&lt;/note&gt;&quot;
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 &#39;/n&#39; of course you can do as you wish inside the block.

Thanks
Josh

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