XML/XPath

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

tutti videocamere un ora sola ti vorrei giorgia photo shop milano costa rica televisore lcd 27 scavi cosa sono le nuvole accessori culle punto van 1 9 d sesso la prima volta yves saint laurent breath easy mp3 foto selen nuda siria siae chema teresa raquin viedo porno googlr com mah jong solitario gesu lava piu bianco le avventure di charlie this love tradotto orion network communication srl renault modus km zero ray-ban 3184 decoder digitali e terrestri galluppi grigne nikon 80 200 d film rosso tre colori video red hot chili peppers famiglio celebrity porn villaggio puglia casus belli fujitsu siemens amilo a7640 house party 1990 www live score com usb pen drive 2 gb lcd 20 1600 1200 fermasoldi argento tipos de scanner nuttea voli san francisco pornosnob panasonic plasma tv offerte lavoro stlisti a lecce sbloccare videofonini tre chelsea charm palmare navigatore ipaq sms gratis free processore intel 478 hurgada hilton servant orchestra video volume muscolare integratori wallpaper harry potter 10 roxette. ballad - p hits. the complete video collection limosano possibili tracce caratteri ereditari bad boys ii movin crusin candeejay web cam gratis mansioni area sicurezza aziendale ofx 560 hyundai hqp421sr ticket one sex pictures alfa romeo 156 1.6 tv philips 42 pollici al plasma hp psc-1315 concorso danza eugenio polyakov camara a gas italia1 it ophelie winter nordica dobermann philips lcd tv ljubav marsh scheda madre amd fsb 800 mhz gba sp accessori console wwwagenziaentrate it www load a game it miguel enriquez maria morena streptococco ufficio forte dei marmo ante legno piante sempreverdi sea monkeys maglie ai ferri cavo s video 10 mt elenco abbonati telecom internazionali indesit wisl86 inthe shadows alta stagione ultrabeat durata documento di trasporto assicurazione portogruaro ati radeon 9800 pro all in wonder san vito dei normanni batterie nokia 6600 juventus film dvd cruscotto audi a3 matur hana melonova sosia drgon one more night wireless compaq panorama calendari linkin park numb kiss dvd 558 hainan piosenka renato zero l equilibrista progetto diffusori dimensioni legno touareg tdi r5 comi umberta bergamo pagine bianche padova tv sat usb nitchevo ucacaksin midi nuovi cavasin agriturismo casalecchio di reno hotel nevea router access point modem hector lavoe professione... giocattolo ferraris ferruccio figli srl g magazine dibujo en porno www sci championship manager 03 04 tips tricks t street parade istituto oncologico europeo milano scarica she will be loved massala toner aculaser c1900 poker di sangue processori intel xeon 2 8 ghz 2 8 driver pentax bluetooth handsfree decapitato usa hotel maxivillage sinai garden sharm www cr decorazioni com batteria per videocamera panasonic jolly hotel roma cartier must bobbit colorare satkeys officinale chiles com mx codice d istallazione di the sims2 tv lcd 22 stefano benni fano contatto fujiko proton metal mr12 router 4 porte notebook thoshiba ericsson hbh 602 marani friends anno 4 episodi 24 la famiglia per freud kitty roberto cecchini mia martini bolero web tv bandiere araldiche albergo alassio sony dsc p93a video lettore dvd divx portatile con tv casino tressure herceptin iv 1 fl 150 mg vita di ghandi navigazione satellitare lavori sessuali tripla corona mp3 innovix preteen denis verdini esposizioni lugano affitto ufficio aquila correggio toshiba satellite m40x 122 latino 9 agroalimentare jvc th-a75r burn this house hyry, antti assicurazione mantova altalene chicco linfodrenaggio vodder prezzi della fiat 1500 costantino vitaliano la scelta la vera storia di eva peron

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