8. public class XPathExample {
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr
= xpath.compile("//book[author='Neal Stephenson']/title/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}
9. // Boucle sur les différentes stations
for($i=0;$i<sizeof($liste_station);$i++)
{
// Ouverture flux
$dom = new DOMDocument();
if (!$dom->load($url.$liste_station[$i])) {
echo "Impossible de charger le flux XML";
}
$itemList = $dom->getElementsByTagName("station");
foreach ($itemList as $item) {
// Nom station
$name = $item->getElementsByTagName('name');
if ($name->length >0) {
$var = $name->item(0);
}
// Derniere remontée d'info
$update = $item->getElementsByTagName('lastupdate');
if ($update->length > 0) {
$var = $update->item(0);
$date_update = isoTimeToStamp($var->nodeValue);
$updated = $date - $date_update;
}
10. Solutions Ruby
• ReXML: spécialisé pour le XML (standard
mais lent)
• Hpricot: d’abord un parseur HTML (rapide)
• Nokogiri: la compétition (rapide)