Ruby & Ruby on rails : Rexml 파싱 :: 2009/02/16 00:00

[참고:Rails for Java Developers]
xml 파싱 중, 푸시파싱과 풀파싱.  둘다 순서대로 한방향으로 이동하며 처리되는 로직이다.
트리파싱은 전체 문서를 불러들이고 무작위로 접근. 복잡한 문서활용때 효과증가.

푸시파싱//
require 'rexml/parsers/sax2parser'
include REXML::Parsers

 targets = []
 xml ='<data><type name="a">test1</type>
             <type name="b">test2</type>
          <type name="c">test3</type></data>'
 parser = SAX2Parser.new(xml)
 parser.listen(:start_element, %w{type}) do |u,l,q,atts|  
    targets << {:name=>atts['name']}
 end
 parser.parse
 
 puts targets
=>namea
   nameb
   namec

##여기서 %w{}는 배열 생성자

풀파싱//
require 'rexml/parsers/pullparser'
include REXML::Parsers
 targets = []
 xml ='<data><type name="a">test1</type>
             <type name="b">test2</type>
          <type name="c">test3</type></data>'
 parser = PullParser.new(xml)
  parser.each do |e|
   if e.start_element? and e[0] == 'type'
     targets << {:name=>e[1]['name']}
  end
  end
 
 puts targets
=>namea
   nameb
   namec

트리파싱//
require 'rexml/document'
include REXML

targets = []
 xml ='<data><type name="a">test1</type>
             <type name="b">test2</type>
          <type name="c">test3</type></data>'
  Document.new(xml).elements.each("//type") do |e|
    targets << {:name=>e.attributes["name"]}
  end
 
 puts targets
=>namea
   nameb
   namec


빌더로 xml 생성
require 'rubygems'
require 'builder'

xml = Builder::XmlMarkup.new :type=>STDOUT, :indent=>1
xml.data do
  xml.type "test1", :name=>"a"
  xml.type "test2", :name=>"b"
  xml.type "test3", :name=>"c"
end

puts xml.to_ary
=><data>
    <type name="a">test1</type>
     <type name="b">test2</type>
     <type name="c">test3</type>
   </data>
   <to_ary/>

##레일스에 기본적으로 내장되어있는 줄 알고 있었는데, 새로 설치해야 했다. 그럴 경우 gem install builder로 설치
rails 1.x 버전은 require_gem, 2.x 버전은 그냥 require로...

[기타정보]
rexml 공식 // http://www.germane-software.com/software/rexml/
rexml api reference //
http://docs.huihoo.com/rdoc/ruby/stdlib/libdoc/rexml/rdoc/index.html
builder reference // http://builder.rubyforge.org/

2009/02/16 00:00 2009/02/16 00:00
Trackback Address :: http://www.dreamzinn.pe.kr/tc/trackback/12
  • Valium.

    Tracked from Liquid valium. | 2010/06/25 00:36 | DEL

    Valium side effects. Xanax valium. Buy valium c.o.d.. Liquid valium. Side effects of valium. Valium.

  • Cialis.

    Tracked from Cialis. | 2010/06/27 15:50 | DEL

    Cialis st. Taking cialis. Cialis.

  • Levitra.

    Tracked from Levitra. | 2010/06/30 22:51 | DEL

    Why would he have levitra.. Buy levitra paypal. Which is more effective viagra cialas levitra. Levitra. Buy levitra online. Using levitra for eczema..

  • Ibuprofen.

    Tracked from Ibuprofen dosage. | 2010/07/01 09:59 | DEL

    Ibuprofen liver damage. Ibuprofen. Ibuprofen and overdose. Perrigo ibuprofen recall.

  • Hydrocodone.

    Tracked from Hydrocodone. | 2010/08/04 07:19 | DEL

    Hydrocodone. Hydrocodone apap. Buy hydrocodone online. Hydrocodone guaifenesin. Buy hydrocodone online without a prescription. Buy hydrocodone without a prescription.

  • Buy soma.

    Tracked from Buy soma online without rx. | 2010/08/07 08:07 | DEL

    Buy soma online. Buy soma without prescription. Buy soma bloghoster. Buy soma watson brand online 150 tablets. Http pills.viptemplates.com p buy soma. Buy watson brand soma. Cyclobenzaprine buy prescription soma zanaflex.

  • Lorazepam.

    Tracked from Lorazepam addiction. | 2010/08/12 07:20 | DEL

    Lorazepam dosage. Lorazepam and propylene glycol. Effect lorazepam side. Lorazepam r. Lorazepam.

  • Buy hydrocodone.

    Tracked from Buy hydrocodone. | 2010/08/16 07:32 | DEL

    Buy hydrocodone without prescription. Buy hydrocodone. Buy hydrocodone without a prescription. Buy hydrocodone illegally.

  • Hydrocodone.

    Tracked from Watson hydrocodone. | 2010/08/17 08:29 | DEL

    Easy way to buy hydrocodone online. Taking low dose of hydrocodone while pregnant. Hydrocodone. Hydrocodone detection period. Buy hydrocodone. Hydrocodone guaifenesin syreth.

  • Buy xanax online.

    Tracked from Buy xanax online. | 2010/08/21 05:25 | DEL

    Buy xanax online. Xanax buy xanax online discoint xanax cheap. Buy no xanax online prescription all on one site.

Name
Password
Homepage
Secret
< PREV |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  ...  11  |  NEXT >