Difference between revisions of "Ruby:Read XML"

From Progzoo
Jump to: navigation, search
(New page: <question lang="rb" className="Demo" copyFile="bbc.xml"> <div>The file http://progzoo.net/bbc.xml is as shown: <pre style="width:80ex;border-style:solid;border-width:thin;"> <world> ...)
 
Line 17: Line 17:
 
node = REXML::XPath.first(doc,"//country[@id='fr']/@pop")
 
node = REXML::XPath.first(doc,"//country[@id='fr']/@pop")
 
print node.value
 
print node.value
</prog></prog>
+
</prog>
 
<p>
 
<p>
 
You load the XML file via the DocumentBuilderFactory, DocumentBuilder
 
You load the XML file via the DocumentBuilderFactory, DocumentBuilder

Revision as of 17:27, 31 December 2007

The file http://progzoo.net/bbc.xml is as shown:
<world>
 <region id='Europe'>
  <country id='de' name='Germany' pop='82000000'/>
  <country id='fr' name='France'  pop='60000000'/>
 </region>
 <region id='Asia'>
  <country id='cn' name='China' pop='1300000000'/>
 </region>
</world>

You load the XML file via the DocumentBuilderFactory, DocumentBuilder and parser. You then obtain a list of all country nodes. The first node represents Germany, it must be cast as an element before you can access the pop attribute.

The value returned should be 82 million.

[Font] [Default] [Show] [Resize] [History] [Profile]