Xml anyone?
-
Hey, I know this has come up before, but the search is not returning any results ("xml" is too small of a string to search for). I am looking at doing a project where I need to work with xml. How is the best way in ruby to write xml? Its not a lot, so I suppose I could just do it longhand, but I has hoping to be able to have something more elegant. Any ideas?
Chris
-
yea - not being able to search for keywords smaller than four words are annoying.
I use google to search SCF then:
ruby xml site:http://forums.sketchucation.com
A couple of topics:
http://forums.sketchucation.com/viewtopic.php?f=180&t=20297
http://forums.sketchucation.com/viewtopic.php?f=180&t=11888Whenever I've looked into this it always seemed that there where too many dependencies. Or the libraries made changes to the core Ruby which I didn't like. Or it wasn't cross platform.
I think in one case there was a conflict with the Set class - which SU ship a modified simple version versus the one in the standard Ruby.I'd be nice to have another look and find a good solution. XML is such a widespread format.
-
If your going to do your own quick and dirty solution, a few points:
Normally you have a "
level
" variable, that keeps track of what nesting level your at.
It starts at 0.
You use it to prependlevel
number of tab characters (which can be a string var holding 4 spaces,) to the output.
Each time you want to write subtags you increment thelevel
var.
When you done writing subtags, you decrementlevel
, and write the closing tag.You can write to a file as you go... or write to a string var, and when done, write the entire string to a file (which is likely safer.)
-
And just to add a little more color to Dan's good points.
The normal coding pattern for writing structured files like XML is to track insetting as Dan suggests and to have a function for each tag type you're going to handle that gets invoked.
Then it becomes a case of you walking down your internal structures and invoking these tag writing functions as you go.
Adam
-
I have done the level thing. Works fine.
And I've worked with a lot of XML documents at my day job too. (XSL, XSLT, XPath, XML Schemas (XSD), DTDs, etc.)
I've used it with Javascript too (parsing documents, extracting nodes, etc.)
Quite a handy language.
-
Great advice folks, thanks so much so far.
What I didn't realize is how complicated it is to "write to a file" (well, it sure seems complicated if you've never tried to do it before). I don't get it. I'm confused with the File class and the IO class. Anyone care to help whip up a few lines of code as an example of how one would write a simple string to a file? Here is what I am trying to do.
def write_file
my_text = "Write this line of text to the file"
save_path = UI.savepanel "Save File", "", "SU_text.xml"
####What am I missing in here. Some File.new or IO.new stuff I'm guessing?
endThanks so much to anyone who is patient enough to help,
Chris
-
HeyHey!, I got it to work. I found a pretty clean example from my Ruby book. SO I'll finish my own example, please correct it if its wrong or is missing important things. Thanks!
def write_file my_text = "Write this line of text to the file" save_path = UI.savepanel "Save File", "", "SU_text.xml" myfile = File.new( save_path, "w" ) myfile.puts my_text myfile.close end
-
That'll work.
-
You could squeeze in a
myfile.flush
just beforemyfile.close
to free up the buffer... BUT it's just a nicety for the sort of file work we are likely to do here - I often don't!
It is very important to 'close' the file at some point when you are finished writing etc... otherwise it stays locked and is then no longer editable/openable until Sketchup closes. My OBJexporter opens the OBJ file and writes to it, using various iterative methods and sub-methods [@file] and when it's finally done it 'closes' it... -
You can open a file for read/write using a block so it's automatically closed when the block is done. I think it even closes the files if you should throw an error.
-
Here's the way to process data in your file following the END directive using the DATA constant.
DATA.each('\n') {|t| puts t } puts "....done." ; __END__ Programmers Todd Texas Homely Chris California Handsome
-
@chris fullmer said:
Anyone care to help whip up a few lines of code as an example of how one would write a simple string to a file?
Here's an example of a utility script (it won't be disto'd, just creates the text file that will be distro'd. The plugin will use the resultant text file.)
In this example the
String
I am writing is a hash string. The plugin will read the hashstring file into aString
reference, theneval
it back into aHash
object, which is then used by aUI.inputbox
to choose the desired font properties. (I posted a pic of the actual inputbox in the beta forum, if interested,... but basically the fontnames get loaded into a dropdown edit control.)<span class="syntaxdefault"></span><span class="syntaxcomment"># by Dan Rathbun ; 2011-02-28<br /></span><span class="syntaxdefault">module Dan<br /> module Utility<br /><br /> font_hash </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">%</span><span class="syntaxdefault">q</span><span class="syntaxkeyword">[</span><span class="syntaxcomment">#<br /># Font Hash to use for RubyConsole Toolbar<br />#<br /># To add a preferred font, insert a hash item below where;<br /># the key is the recognized fontname, and<br /># the value is the font filename. See; "Windows/Fonts" folder.<br />#<br /># The list will be alpha sorted after loading, and any fonts<br /># whoose filename does not exist in the "Windows/Fonts" folder,<br /># will be removed from the values, by the inputbox method.<br />#<br /></span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># start curly<br /><br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Enter your custom fonts below,<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># and remove the comment marker (#)<br /><br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># "" => "",<br /><br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Standard monospaced fonts that ship with Windows;<br /><br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Bitstream Vera Sans Mono"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"VeraMono.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Bitstream Vera Sans Mono Bold"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"VeraMoBd.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Consolas"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Consola.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Consolas Bold"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Consolab.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Courier New"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"COUR.TTF"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Courier New Bold"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"COURBD.TTF"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"DejaVu Sans Mono"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"DejaVuMonoSans.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"DejaVu Sans Mono Bold"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"DejaVuMonoSansBold.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCPEUR"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isocpeur.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCP"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isocp___.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCP2"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isocp2__.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCP3"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isocp3__.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCT"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isoct___.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCT2"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isoct2__.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCT3"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isoct3__.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"ISOCTEUR"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"isocteur.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Lucida Console"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"LUCON.TTF"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Monospace 821 BT"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"monos.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Monospace 821 Bold BT"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"monosb.ttf"</span><span class="syntaxkeyword">,<br /></span><span class="syntaxdefault"> </span><span class="syntaxstring">"Monotxt"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> </span><span class="syntaxstring">"monotxt_.ttf"<br /><br /></span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># end curly<br /></span><span class="syntaxkeyword">]<br /><br /></span><span class="syntaxdefault"> cd </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Dir</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">getwd<br /> Dir</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">chdir</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">expand_path</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">dirname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">__FILE__</span><span class="syntaxkeyword">)))<br /></span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">open</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'monofonts.txt'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'w'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">io</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> io</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">puts</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">font_hash</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> Dir</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">chdir</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">cd</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> <br /> cd</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil<br /> <br /> font_hash</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">nil<br /> GC</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start<br /> <br /> end </span><span class="syntaxcomment"># module<br /></span><span class="syntaxdefault">end </span><span class="syntaxcomment"># module </span><span class="syntaxdefault"></span>
This block form of the
File.open
method will automatically close theIO
object when the block is finshed.The Ruby Docs are a bit confusing regarding
IO
andFile
, unless you realize that classFile
, is a subclass of classIO
, and inherits allIO
's methods. (A few of theses inherited methods are overriden in theFile
subclass, to make them explicitly work better for files.)I use the
%
string delimiters in the string defintion.
%q
is a single quoted string.
%Q
is a double quoted string.
You can choose any delimiter you wish, which can either be the same character for the start and end delimiters, OR matched delimiters which are one of the bracket sets, ie { }, [ ], or ( ).
Normally I like to use the curly brackets, but in this case since I'm wrapping a hash string (which is itself delimited by 'curlies',) I chose to use square brackets. (I could have still used curlies, but decided it might look confusing.)
And it is normally good practice to wrap all
IO
operations within abegin ... rescue ... ensure
clause. (I didn't do it because my uitilty only runs once.. and won't be distro'd.)Example:
<span class="syntaxdefault">begin<br /> filename </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'monofonts.txt'<br /></span><span class="syntaxdefault"> io </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">open</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">filename</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'w'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">io</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> io</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">puts</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">font_hash</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">rescue IOError </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> e<br /> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Error writing file; #{filename}\n#{e.message}"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">ensure<br /> io</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">close unless io</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">closed</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">end</span>
~
-
@dan rathbun said:
I use the
%
string delimiters in the string defintion.
%q
is a single quoted string.
%Q
is a double quoted string.Aha! I had been trying to get string delimeters to work, and I had given up because I needed to use #{} notation inside my string, and I had only seen the %q example. I did not know that %Q acted as a duoble quotes. That will make things a little nicer. Thanks for all of that Dan and all,
Chris
-
@chris fullmer said:
@dan rathbun said:
I use the
%
string delimiters in the string defintion.
%q
is a single quoted string.
%Q
is a double quoted string...., and I had only seen the
%q
example. I did not know that%Q
acted as a double quotes.Actually
%
by itself is also a double-quoted string.
And also<
and>
can used as a bracket set, but I'm not sure I'd recommend it. (I'd prefer to save them for math expressions.)You can nest brackets and quotes in strings:
adverb = 'really' str = %Q{"Hey, nested brackets #{adverb} do work!"}
>> "Hey, nested brackets really do work!"
str.inspect
>> ""Hey, nested brackets really do work!"" -
How do you stop Sketchucation from downloading XML documents? It keeps doing this?
-
@jp2009 said:
How do you stop Sketchucation from downloading XML documents? It keeps doing this?
There's an issue with the recently updated PluginStore Extension when used in Chrome.
Disable that extension under Settings, until it is fixed...
Breaking news... v1.3 of that Extension was released today, it should fix the issue, so install that to update things, and those xml errors should be no more...
Advertisement