Forum: Ruby on Rails read SVG file child elements

7321def3ca60ed77b58b018e7e4f8d7e?d=identicon&s=25 Astm Ali (astm)
on 2014-01-21 18:01
Hello Guys
I'm new in Ruby and now I'm trying to type my first app
now i have SVG file contain rows such as

<text x="10"  y="20"
      style="font-family: Helvetica;
             font-size  : 24;
             fill       : #ff0000;
             stroke     : #000000;">First label</text>
    <text x="85"  y="150"
      style="font-family: Helvetica;
             font-size  : 24;
             fill       : #ff0000;
             stroke     : #000000;">Second label</text>

now when i want to get the labels form the file
i was used non professional way to get them
buy using this code

f=File.open(@svgFile)
@data=Nokogiri::XML(f)
@data=@data.to_s                   # convert to string
@data=@data.split('<text')[1..-1]  # convert to array and remove first
element
f.close

<% @data.each do |data|%>
<%  @data=data.gsub(%r{</?[^>]+?>},'') %>
<%  @data=data.split('>')[1][0..-7]%>
<%= @data %><br>
<% end%>


so now if i want to get the full data to every element such as
name= First label
stroke =#000000
font-size=24

how i can get the child data in the svg file with professional code
i don't like the way to remove html tags  to get the data  ???
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2014-01-21 18:28
(Received via mailing list)
On Jan 21, 2014, at 12:01 PM, Astm Ali wrote:

>      style="font-family: Helvetica;
> @data=@data.to_s                   # convert to string
>
> so now if i want to get the full data to every element such as
> name= First label
> stroke =#000000
> font-size=24
>
> how i can get the child data in the svg file with professional code
> i don't like the way to remove html tags  to get the data  ???

You're starting out on the right track, using Nokogiri. You don't need
to use string manipulation, though, because SVG is just XML inside, so
you can interrogate the actual data structure in Noko using either xpath
or css syntax. Have a quick read through the Nokogiri documentation at
http://nokogiri.org or google the topic.

Walter
7321def3ca60ed77b58b018e7e4f8d7e?d=identicon&s=25 Astm Ali (astm)
on 2014-01-23 13:00
Dear Walter

I know that my old way not the true way

now I can open my svg file using the Nokogiri Slop by using this code


@test = Nokogiri::Slop(File.open("./file.svg"))
<% @test.xpath('//text').map do |i|%>
<%=i%><br>
<%  end  %>

now I can get the all text records
now when i'm trying to get the (stroke) to every record it always gives
me error

I'm using this command
<%=i.("[@stroke]").text.content %>

so how i can get the  font-family,font-size,fill ,stroke
to every record ???
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2014-01-23 14:59
(Received via mailing list)
On Jan 23, 2014, at 7:00 AM, Astm Ali wrote:

> <%  end  %>
>
> now I can get the all text records
> now when i'm trying to get the (stroke) to every record it always gives
> me error
>
> I'm using this command
> <%=i.("[@stroke]").text.content %>
>
> so how i can get the  font-family,font-size,fill ,stroke
> to every record ???

Attributes are not the same thing as child tags. To access them,
assuming you have a reference to a node like this:

@test.xpath('//text').each do | text_node |
  puts text_node['font-family']
  puts text_node['stroke']
  ...
end

You can get any attribute of the tag using the square-brackets notation.

Walter
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.