XML/XPath

From Rosetta Code
< XML
Revision as of 23:52, 21 August 2007 by rosettacode>KmgBi2

nino rota la strada skye sweetnam agriturismo siviglia orologi casio data bank sony cybershot w7 risultati motomondiale 2004 un paradiso senza biliardo eleni pca batterie per cellulari ericsson t39 fotocopiatrice scanner stampante a3 gabriel amano gioca jouer midi for men rivista giochi con gay e lesbiche copertina cd freeware urke videogioco on line dj console pixar good charlotte dvd sony walkman mp3 hp-compaq-ipaq 6345 quam mirabilis www playstation it diga di assuan cavalcata selvaggia donne nere penetrazioni anali oakley twenty fasciatoio vasca se ci credi anche tu karaoke file midi jaguar 3 2 i pugni in tasca office licenza edge cellulari bluetooth appunti diritto tributario blue video mutazioni generali cd - rw 8 cm floppy esterno usb canon ef fisheye camasutra foto dragostea din tei dj ross garlando 200 verona calcio prestito otranto lettore jpg moder times j five fotocamera cybershot sony t7 croazia hotel paradiso beach ancora vita mundo porno festivalbar blue 2004 pioneer vsx-c402-s quel giorno a rio gli orsi vanno in giappone wisin y yandel live logitech quickcam communicate data prova esami gru iv serie speciale fax 530 bottone enricke arbaud, joseph d- staffa accessori samsung audio video volvo v50 2 0 d summum sophia loren videocamera canon mv -790 try tradotta buczkowski, leopold cubo magico juegos de mario bros asus a8v e deluxe via k8t890 download yetisports 7 montoni optio pentax la macchina dei gelati sony cuffie 5 1 lettore mp3 radio 1g apera negozi vendita mobili afgani testi di canzoni it idropulitrice professionale 150 www laura engel it strumenti chirurgici ivg polar t-31 miazzina palmare fujitsu siemens behead kim sun andrews virginia libri travan tr5 pornio video omnia vacanza affitto como inside jupiter dragostea den tea haiduci gabry ponte el mundo de rastafari raz dwa trzy jassica biel charlotte gallery f leali logorrea traduttore spagnolo microsoft office per xp batajsk peg perego modular system pliko erotc pocket kingdom jvc th-s1 sanyo home theatre de noche en la ciudad piron, armand j. ruocco eric prytz call on me umberto poli minghi mietta software dj black pearl jam la vergine sotto il tetto nicholas e berg nidi darac antivirus panda titanium 2006 kyosho caliber hard disk mini freakys helico sharik sting feat cray david nicoletti serie 7 bmw ups e pdu www tigre it grottammare www nellyville com resultados de comipems hp 1150 valentino azzurro fujifilm finepix f610 decrypter sammichele ba kamsutra toe rings e la notte se ne va nova art explosion goldsmith, oliver programmi per funcard answer epson n1200 il pianeta vivente i mari d erba pereyra sony vgn s4hp volvo 2 3 divani letto ted bundy hp c4844ae pettenati case per hard disk esterno les deux alpes video boulevard of broken dreams ww virginio it poster vendita stampante multifunzione copiatrice pagine bianche avellino consulenza internet marketing tommi v la serenissima normas del imss para proyecto arq moderntalking ospedale vicenza dmc panasonic scarpe da mare e barca dat 40 external tape drive hp immagini rottweiller flinta delta fox la cumbia de los trapos nude fight porno alte prestazioni oktoberfest 2004 monaco gioia e rivoluzione pane e sale looney tunes - back in action nvidia geforce 6600 256mb istituto salvemini sicurezza baby porte dexther sloth spartito di adesso tu gabriella labate rivendite animali provincia di arezzo digital camera sony dsc registratore mini disc portatile fotografie erotiche mellin latte in polvere dafa vicario comunication orologio citizen google nel web locandine

Task
XML/XPath
You are encouraged to solve this task according to the task description, using any language you may know.

Perform the following three XPath queries on the XML Document below:

  • Retrieve the first "item" element
  • Perform an action on each "price" element (print it out)
  • Get an array of all the "name" elements

XML Document:

<inventory title="OmniCorp Store #45x10^3">
  
<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>
<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>
</inventory>

C#

 XmlReader XReader;

 // Either read the xml from a string ...
 XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>"));

 // ... or read it from the file system.
 XReader = XmlReader.Create("xmlfile.xml");

 // Create a XPathDocument object (which implements the IXPathNavigable interface)
 // which is optimized for XPath operation. (very fast).
 IXPathNavigable XDocument = new XPathDocument(XReader);

 // Create a Navigator to navigate through the document.
 XPathNavigator Nav = XDocument.CreateNavigator();
 Nav = Nav.SelectSingleNode("//item");

 // Move to the first element of the selection. (if available).
 if(Nav.MoveToFirst())
 {
   Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element.
 }

 // Get an iterator to loop over multiple selected nodes.
 XPathNodeIterator Iterator = XDocument.CreateNavigator().Select("//price");

 while (Iterator.MoveNext())
 {
   Console.WriteLine(Iterator.Current.Value);
 }

 Iterator = XDocument.CreateNavigator().Select("//name");

 // Use a generic list.
 List<string> NodesValues = new List<string>();

 while (Iterator.MoveNext())
 {
   NodesValues.Add(Iterator.Current.Value);
 }

 // Convert the generic list to an array and output the count of items.
 Console.WriteLine(NodesValues.ToArray().Length);

ColdFusion

 <cfsavecontent variable="xmlString">
 <inventory
 ...
 </inventory>
 </cfsavecontent>
 <cfset xml = xmlParse(xmlString)>