XML/XPath: Difference between revisions

Content added Content deleted
(Properly ordering the languages)
Line 34: Line 34:
</inventory>
</inventory>


==[[Ruby]]==
==[[C#]]==
XmlReader XReader;
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html)
// Either read the xml from a string ...
require "rexml/document"
XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>"));
include REXML
// ... or read it from the file system.
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string)
XReader = XmlReader.Create("xmlfile.xml");
doc = Document.new(
// Create a XPathDocument object (which implements the IXPathNavigable interface)
%q@<inventory title="OmniCorp Store #45x10^3">
// which is optimized for XPath operation. (very fast).
...
IXPathNavigable XDocument = new XPathDocument(XReader);
</inventory>
// Create a Navigator to navigate through the document.
@
XPathNavigator Nav = XDocument.CreateNavigator();
)
Nav = Nav.SelectSingleNode("//item");
# The invisibility cream is the first <item>
// Move to the first element of the selection. (if available).
invisibility = XPath.first( doc, "//item" )
if(Nav.MoveToFirst())
# Prints out all of the prices
Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element.
XPath.each( doc, "//price") { |element| puts element.text }
// Get an iterator to loop over multiple selected nodes.
# Gets an array of all of the "name" elements in the document.
XPathNodeIterator Iterator = XDocument.CreateNavigator().Select("//price");
names = XPath.match( doc, "//name" )
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);


==[[PHP]]==
==[[PHP]]==
Line 84: Line 93:
}
}


==[[C#]]==
==[[Ruby]]==
#Example taken from the REXML tutorial (http://www.germane-software.com/software/rexml/docs/tutorial.html)
XmlReader XReader;
require "rexml/document"
// Either read the xml from a string ...
include REXML
XReader = XmlReader.Create(new StringReader("<inventory title=... </inventory>"));
#create the REXML Document from the string (%q is Ruby's multiline string, everything between the two @-characters is the string)
// ... or read it from the file system.
doc = Document.new(
XReader = XmlReader.Create("xmlfile.xml");
%q@<inventory title="OmniCorp Store #45x10^3">
// Create a XPathDocument object (which implements the IXPathNavigable interface)
...
// which is optimized for XPath operation. (very fast).
</inventory>
IXPathNavigable XDocument = new XPathDocument(XReader);
@
// Create a Navigator to navigate through the document.
)
XPathNavigator Nav = XDocument.CreateNavigator();
# The invisibility cream is the first <item>
Nav = Nav.SelectSingleNode("//item");
invisibility = XPath.first( doc, "//item" )
// Move to the first element of the selection. (if available).
# Prints out all of the prices
if(Nav.MoveToFirst())
XPath.each( doc, "//price") { |element| puts element.text }
Console.WriteLine(Nav.OuterXml); // The outer xml of the first item element.
# Gets an array of all of the "name" elements in the document.
// Get an iterator to loop over multiple selected nodes.
names = XPath.match( doc, "//name" )
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);