XML/XPath

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

numeri cellulari sesso porne impiegato post vendita (regione trentino alto adige trento provincia) aladino 2 riassunto libro dieci piccoli indiani kit gps tom lorenzo il magnifico cyrus mono x mallawi cara de chango southwest airlines sexy katia pedrotti chat firenze bigiotteria etnica canon 100 macro ilahii buldok dimmi dimmi tatangelo vibrazioni prodotte da infrastrutture di trasporto in fase di costruzione e di esercizio hp 4370 autofellatio anti legge no ormoni volo aereo bologna palermo i want internet in english www abcarcade com www cronache rapimenti it km 0 fiat panda barash asher navigatore gps autoradio tv dvd philips www dgtv it san carlos (uruguay) ocean congelatore verticale lenau nikolaus anal toys midi country vacatures hip hop immortals we got your kids pearcing sexi jdo spa gottmann jean splav e dimme che non vuoi morire www unile it scarica canzoni di natale piastra bistecchiere www nane sexi it vacanze in liguria surf la grande rinuncia vantec tornado term rewriting system hd 160 mb gps bluetooth tomtom navigator 3 dlink wireless usb viamichelin com silp cucina chamberlin william henry crrek ventspils corsi d informatica tally 9312 piegatura alluminio ciao norma jean tim mms foto dei morti in iraq fantuzzi it opel vetra tuning saeco vienna toner xerox docuprint p8ex residence queen rimini gente come noi hbv batteria amilo d7820 volo aereo brindisi bologna canon eos 350d canon kataweb mappe telefono di singoli che cercano coppie vestire i personnaggi akg k141 vino tego caldero n saggio breve famiglia italiana albero verde dolgan asus a620 palmari olimpia s mackay hp 2840 auto noleggio valencia carla gantus la canzone dei gabbiani houteman here i came here i go honda transalp 2000 frullatore e mixer dan seals brio telecom cordless creativity spa jhonny angel testo tchat profumi creed testo hoobastank romagn mia carbone barbecue nokia 6210 batteria rezzori gregor von emma warton modem adsl dlink banca popolare sondrio mp3 wma hifi www cerchi in lega fotocamera digitale nikon 5600 latin lover cesare cremonesi www italiasalute it leaving on a jet plane antenna per tv auto scanjet 4600 te amar selznick david o www terigi it mafia nocd download pornoshop lingerie kosov distributori diffusori b ghosthunter assicurazione motorino egitto sharm el sheikh hilton fatturazione gay che si fanno seghe jak zapomniec free mp3 mpx 200 il sorriso della vita ipaq compaq hp plotter 70 hotel lucoli aq papa wojtyla francobolli mape concettuali www mercedes lu rover defender 90 fotos proibidas xxx www wanadoo dk fratelli feltrinelli spa spartiti zecchino d oro oratoria casa mutuo appartamento vendita boxster 987 racconti erotici adolescenti ipod video musical lantern forno a incasso clipart nozze d argento monika brodka ten chocolate choco choco aspirapolvere robot cesare evora cordless desktop mx bluetooth socialist hymn seggiolino bicicletta www casalinghe com polsat auto film kurt cobain ingrosso bigiotteria boulling la luce dell est lucio battisti annuncio affitti reggio calabria jet stream culiadas margarita (astronomia) sopranos info italy klm com monitor sony hs75b cartelle termiche zoo berlino viaggio slovenia consiglio dimagrire seleco venice amd 3200 adriana volpe 2004 bicep muscle teen webcam video porno clip o zone dragostea tin dei ddr2 667mhz lnb mti philips 530 dizionario spagnolo online ragazza arles l ufologia decapitazione di uno statunitense maxtor sata 80 gb hard disk e floppy log e attivazioni cavo usb tastiera mouse orali copia cd falditas hey baby bruce channel porno mum pinne da pesca bantumi yety sport 6 elenco asili nido privati in ancona

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)>