XML/XPath

From Rosetta Code
< XML
Revision as of 11:37, 8 September 2007 by rosettacode>KmgBi2

konica minolta flash paul oakenfold obsesion radio mix todo de porno codici per carte titanium motoseghe da potatura the roxy music blondie driver scheda rete employment in the uk corrado alvaro filodoro ninne nanna italiane l iperbole hail plane tickets desktop offerta hd firewire 800 dual input monitor salassa la ultima vez www lil games com olivetti fax lab siemens m1437g xanga skins software fattura spartiti musicali canzoni napoletane comune pesaro offerta copenhagen randfichten lebt denn dr alte holzmichel z3 1 8 fumetti erotici gtatuiti concerti per regione abe win maglie guess donna abbigliamento barolo voerzio ospedale maggiore bologna dana ferrara www basi musica it film porrno di carmen russo hoostabank mp3 milano chat autoradio cd mp3 usb la passeggiata sony dcrhc90 scuole alliste e lucevan le stelle torta meringata comune di cannobbio it sting brand new day tour live from universal amphitheatre emiro reggio emilia unikkatil f1 live incontro sant oceans boxset spinete domiciliazione postale cuatro rosas de jorge celedon matrixcam reloaded batteria videocamera panasonic mayure di ross borsoni pelle wind modena cartoon 69 sem limite pra sonhar www uproar com televisori 100 hertz could place brucelee la petroniana gioco demo pc bouquet sposa tv per auto dvd lettori e divx rivista gq office per mac 2004 italiano this love maroon 5 mp3 i mode guida al campionato antonello de pierro isola dei famosi sin bandera midi mistral gagnant satellite a80131 morr lcd computer pj tx100 na zawsze oboz ta sono un italiano vero scheda computer firewire some might say jo quiero bailar the shark flaviana renato e i profeti note di natale she wille be loved voli caorle patch update gioco storia turismo i pirati della malesia deumidificatore tasciugo www locman it videoclips de musica zara croazia raffy equus circhi a roma s benedetto www 2 pac com madre figlio sesso water bed sheet ddr 256 bella bellissima hyundai lcd 17 l72s cartel del santa seat altea km zero sex movi hunt red october lan chile frigoriferi combinati da incasso decapitazione jhonson vendita dvd online invito compleanno il cacciatore nel bosco luise ullrich nikon d50 1855 55200 dvd washer photo 75 studio fotografico jazz davide sparti robbie williams sul pc milano berlino nessun accordo fra bluray e hddvd annuncio affitti vicenza nike silver air max wmns oki 3200 aspirapolvere professionale figure geometriche supereva com terme di cola chris liebing giochi preziosi creative soundworks la vergine e la bestia videocamera digitale panasonic nv gs viareggio alberghi yaesu vx charisse verhaert vp d453 texasville foto porno di gay con animali dlink print escape from underground jilli ettinger max saab 900 20i turbo 16v 5p s banane e lamponi nikon 70200mm f 2 8 magnex lettore mp3 giochi play statyon www mondadori it agenzia entrata it volvo summum ebo vestire qualcuno film porno del giorno mental as anything basemental x file resist or serve deskjet 3845 usb digitale terrestre terratec leone il cane fifone zoom ottico 12x albergo riva del garda net magic pecore grande frattello 5 sesso in austria navigatore garmin gps 12 musica di natale jameli hans zimmer lisa gerrard now we are fre interfaccia infrarosso polar samsung lcd 19 913n casting moda messale liturgico settembre video email biancuzzi lo squalo bianco caldaie lamborghini cerca lavoro mantova castellana grotte lettore dvddivx amstrad cronache marziane 15 04 2005 mio gas san giuliano milanese racconi incestuosi mobile drive porsche 80gb copkiller codici sblocco cellulari lg cavo usb rca reggiseno solo coppe powermust 600 plus tv al plasma 17

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