Xml -ror

From Sandoz

(Difference between revisions)
Jump to: navigation, search
m (Protected "Xml -ror" ([edit=autoconfirmed] (indefinite) [move=autoconfirmed] (indefinite)))
 
(7 intermediate revisions not shown)
Line 1: Line 1:
http://www.germane-software.com/software/rexml/docs/tutorial.html
http://www.germane-software.com/software/rexml/docs/tutorial.html
-
Accessing Elements
+
require 'rexml/document'
-
==================
+
 
 +
1) Accessing Elements
 +
-----------------------
 +
doc = Document.new File.new("mydoc.xml")
doc = Document.new File.new("mydoc.xml")
 +
doc.elements.each("inventory/section") { |element| puts element.attributes["name"] }
doc.elements.each("inventory/section") { |element| puts element.attributes["name"] }
-
# -> health
+
 
-
# -> food
+
-> health
 +
 
 +
-> food
 +
 
doc.elements.each("*/section/item") { |element| puts element.attributes["upc"] }
doc.elements.each("*/section/item") { |element| puts element.attributes["upc"] }
-
# -> 123456789
+
 
-
# -> 445322344
+
-> 123456789
-
# -> 485672034
+
 
-
# -> 132957764
+
-> 445322344
 +
 
 +
-> 485672034
 +
 
 +
-> 132957764
 +
 
root = doc.root
root = doc.root
 +
puts root.attributes["title"]
puts root.attributes["title"]
-
# -> OmniCorp Store #45x10^3
+
 
 +
-> OmniCorp Store #45x10^3
 +
 
puts root.elements["section/item[@stock='44']"].attributes["upc"]
puts root.elements["section/item[@stock='44']"].attributes["upc"]
-
# -> 132957764
+
 
 +
-> 132957764
 +
 
puts root.elements["section"].attributes["name"]  
puts root.elements["section"].attributes["name"]  
-
# -> health (returns the first encountered matching element)  
+
 
 +
-> health (returns the first encountered matching element)  
 +
 
puts root.elements[1].attributes["name"]  
puts root.elements[1].attributes["name"]  
-
# -> health (returns the FIRST child element)  
+
 
 +
-> health (returns the FIRST child element)  
 +
 
root.detect {|node| node.kind_of? Element and node.attributes["name"] == "food" }
root.detect {|node| node.kind_of? Element and node.attributes["name"] == "food" }
 +
-
The source document
+
2) The source document
-
==================
+
---------------------
<inventory title="OmniCorp Store #45x10^3">
<inventory title="OmniCorp Store #45x10^3">
   <section name="health">
   <section name="health">
Line 53: Line 75:
Using XPath
Using XPath
-
==========
+
-----------------------
-
# The invisibility cream is the first <item>
+
The invisibility cream is the first <item>
-
invisibility = XPath.first( doc, "//item" )  
+
invisibility = XPath.first( doc, "//item" )  
-
# Prints out all of the prices
+
# Prints out all of the prices
-
XPath.each( doc, "//price") { |element| puts element.text }
+
XPath.each( doc, "//price") { |element| puts element.text }
-
# Gets an array of all of the "name" elements in the document.
+
Gets an array of all of the "name" elements in the document.
-
names = XPath.match( doc, "//name" )  
+
names = XPath.match( doc, "//name" )  
Using to_a()
Using to_a()
-
============
+
-----------------------
all_elements = doc.elements.to_a
all_elements = doc.elements.to_a
all_children = doc.to_a
all_children = doc.to_a
Line 69: Line 91:
Entity Replacement
Entity Replacement
-
===================
+
-----------------------
doc = Document.new '<!DOCTYPE foo [
doc = Document.new '<!DOCTYPE foo [
<!ENTITY ent "replace">
<!ENTITY ent "replace">
Line 77: Line 99:
Creating elements
Creating elements
-
=================
+
-----------------------
el = someelement.add_element "myel"  
el = someelement.add_element "myel"  
-
# creates an element named "myel", adds it to "someelement", and returns it  
+
creates an element named "myel", adds it to "someelement", and returns it  
el2 = el.add_element "another", {"id"=>"10"}  
el2 = el.add_element "another", {"id"=>"10"}  
-
# does the same, but also sets attribute "id" of el2 to "10"  
+
does the same, but also sets attribute "id" of el2 to "10"  
el3 = Element.new "blah"  
el3 = Element.new "blah"  
el1.elements << el3  
el1.elements << el3  
el3.attributes["myid"] = "sean"  
el3.attributes["myid"] = "sean"  
-
# creates el3 "blah", adds it to el1, then sets attribute "myid" to "sean"
+
creates el3 "blah", adds it to el1, then sets attribute "myid" to "sean"
Adding text
Adding text
-
=============
+
-----------------------
el1 = Element.new "myelement"  
el1 = Element.new "myelement"  
el1.text = "Hello world!"  
el1.text = "Hello world!"  
-
# -> <myelement>Hello world!</myelement>  
+
-> <myelement>Hello world!</myelement>  
el1.add_text "Hello dolly"  
el1.add_text "Hello dolly"  
-
# -> <myelement>Hello world!Hello dolly</element>  
+
  -> <myelement>Hello world!Hello dolly</element>  
el1.add Text.new("Goodbye")  
el1.add Text.new("Goodbye")  
-
# -> <myelement>Hello world!Hello dollyGoodbye</element>  
+
-> <myelement>Hello world!Hello dollyGoodbye</element>  
el1 << Text.new(" cruel world")  
el1 << Text.new(" cruel world")  
-
# -> <myelement>Hello world!Hello dollyGoodbye cruel world</element>
+
-> <myelement>Hello world!Hello dollyGoodbye cruel world</element>
Encoded Output
Encoded Output
-
==============
+
-----------------------
e = Element.new "<a/>"
e = Element.new "<a/>"
e.text = "f\xfcr"  # ISO-8859-1 '??'
e.text = "f\xfcr"  # ISO-8859-1 '??'
Line 109: Line 131:
Inserts
Inserts
-
=============
+
-----------------------
doc = Document.new "<a><one/><three/></a>"  
doc = Document.new "<a><one/><three/></a>"  
doc.root[1,0] = Element.new "two"  
doc.root[1,0] = Element.new "two"  
-
# -> <a><one/><two/><three/></a>  
+
-> <a><one/><two/><three/></a>  
three = doc.elements["a/three"]  
three = doc.elements["a/three"]  
doc.root.insert_after three, Element.new "four"  
doc.root.insert_after three, Element.new "four"  
-
# -> <a><one/><two/><three/><four/></a>  
+
  -> <a><one/><two/><three/><four/></a>  
-
# A convenience method allows you to insert before/after an XPath:  
+
  A convenience method allows you to insert before/after an XPath:  
doc.root.insert_after( "//one", Element.new("one-five") )  
doc.root.insert_after( "//one", Element.new("one-five") )  
-
# -> <a><one/><one-five/><two/><three/><four/></a>  
+
  -> <a><one/><one-five/><two/><three/><four/></a>  
-
# Another convenience method allows you to insert after/before an element:  
+
  Another convenience method allows you to insert after/before an element:  
four = doc.elements["//four"]  
four = doc.elements["//four"]  
four.previous_sibling = Element.new("three-five")  
four.previous_sibling = Element.new("three-five")  
-
# -> <a><one/><one-five/><two/><three/><three-five/><four/></a>
+
  -> <a><one/><one-five/><two/><three/><three-five/><four/></a>
Automatic raw text handling
Automatic raw text handling
-
===========================
+
-----------------------
doc = REXML::Document.new( source, { :raw => %w{ tag1 tag2 tag3 } }
doc = REXML::Document.new( source, { :raw => %w{ tag1 tag2 tag3 } }
Raw documents
Raw documents
-
================
+
-----------------------
doc = REXML::Document.new( source, { :raw => :all })
doc = REXML::Document.new( source, { :raw => :all })
Stream parsing
Stream parsing
-
================
+
-----------------------
list = MyListener.new  
list = MyListener.new  
source = File.new "mydoc.xml"  
source = File.new "mydoc.xml"  
REXML::Document.parse_stream(source, list)
REXML::Document.parse_stream(source, list)

Latest revision as of 00:49, 2 February 2011

http://www.germane-software.com/software/rexml/docs/tutorial.html

require 'rexml/document'

1) Accessing Elements


doc = Document.new File.new("mydoc.xml")

doc.elements.each("inventory/section") { |element| puts element.attributes["name"] }

-> health
-> food

doc.elements.each("*/section/item") { |element| puts element.attributes["upc"] }

-> 123456789
-> 445322344
-> 485672034
-> 132957764

root = doc.root

puts root.attributes["title"]

-> OmniCorp Store #45x10^3

puts root.elements["section/item[@stock='44']"].attributes["upc"]

-> 132957764

puts root.elements["section"].attributes["name"]

-> health (returns the first encountered matching element) 

puts root.elements[1].attributes["name"]

-> health (returns the FIRST child element) 

root.detect {|node| node.kind_of? Element and node.attributes["name"] == "food" }


2) The source document


<inventory title="OmniCorp Store #45x10^3">

 <section name="health">
   <item upc="123456789" stock="12">
     <name>Invisibility Cream</name>
     <price>14.50</price>
     <description>Makes you invisible</description>
   </item>
   <item upc="445322344" stock="18">
     <name>Levitation Salve</name>
     <price>23.99</price>
     <description>Levitate yourself for up to 3 hours per application</description>
   </item>
 </section>
 <section name="food">
   <item upc="485672034" stock="653">
     <name>Blork and Freen Instameal</name>
     <price>4.95</price>
     <description>A tasty meal in a tablet; just add water</description>
   </item>
   <item upc="132957764" stock="44">
     <name>Grob winglets</name>
     <price>3.56</price>
     <description>Tender winglets of Grob. Just add water</description>
   </item>
 </section>

</inventory>

Using XPath


The invisibility cream is the first <item>
invisibility = XPath.first( doc, "//item" ) 
# Prints out all of the prices
XPath.each( doc, "//price") { |element| puts element.text }
Gets an array of all of the "name" elements in the document.
names = XPath.match( doc, "//name" ) 

Using to_a()


all_elements = doc.elements.to_a all_children = doc.to_a all_upc_strings = doc.elements.to_a( "//item/attribute::upc" ) all_name_elements = doc.elements.to_a( "//name" )

Entity Replacement


doc = Document.new '<!DOCTYPE foo [ <!ENTITY ent "replace"> ]><a>&ent;</a>' doc.root.text #-> "replace"


Creating elements


el = someelement.add_element "myel"

creates an element named "myel", adds it to "someelement", and returns it 

el2 = el.add_element "another", {"id"=>"10"}

does the same, but also sets attribute "id" of el2 to "10" 

el3 = Element.new "blah" el1.elements << el3 el3.attributes["myid"] = "sean"

creates el3 "blah", adds it to el1, then sets attribute "myid" to "sean"

Adding text


el1 = Element.new "myelement" el1.text = "Hello world!"

-> <myelement>Hello world!</myelement> 

el1.add_text "Hello dolly"

 -> <myelement>Hello world!Hello dolly</element> 

el1.add Text.new("Goodbye")

-> <myelement>Hello world!Hello dollyGoodbye</element> 

el1 << Text.new(" cruel world")

-> <myelement>Hello world!Hello dollyGoodbye cruel world</element>


Encoded Output


e = Element.new "<a/>" e.text = "f\xfcr" # ISO-8859-1 '??' o = e.write( Output.new( o, "ISO-8859-1" ) )


Inserts


doc = Document.new "<a><one/><three/></a>" doc.root[1,0] = Element.new "two"

-> <a><one/><two/><three/></a> 

three = doc.elements["a/three"] doc.root.insert_after three, Element.new "four"

 -> <a><one/><two/><three/><four/></a> 
 A convenience method allows you to insert before/after an XPath: 

doc.root.insert_after( "//one", Element.new("one-five") )

 -> <a><one/><one-five/><two/><three/><four/></a> 
 Another convenience method allows you to insert after/before an element: 

four = doc.elements["//four"] four.previous_sibling = Element.new("three-five")

 -> <a><one/><one-five/><two/><three/><three-five/><four/></a>

Automatic raw text handling


doc = REXML::Document.new( source, { :raw => %w{ tag1 tag2 tag3 } }


Raw documents


doc = REXML::Document.new( source, { :raw => :all })

Stream parsing


list = MyListener.new source = File.new "mydoc.xml" REXML::Document.parse_stream(source, list)

Personal tools