%@ page
import = "org.w3c.dom.*,org.apache.xerces.parsers.DOMParser,org.xml.sax.*"
%>
Address Book
CONTENT OF THIS PART¦¦TABLE FORM¦¦HOMEPAGE¦¦
WORKPAGE¦¦
TECFA
Address Book of Contacts
Here are some of the contacts which I treasure much (not exactly of course).
<%
// URL
String URL = "http://tecfa.unige.ch/staf/staf-e/sun/staf2x/xml/try.xml";
// Entry element (fiche)
String ROOT_ELEMENT_TAG = "Entry";
// DONNEES
// charge le document et cree le document xml ou exit si cela ne marche pas
// Note: Il est OBLIGATOIRE d'implementer ce try (avec xml.jar de Sun en tout cas!)
try {
DOMParser parser = new DOMParser();
parser.parse(URL);
Document TheDocument = parser.getDocument();
// Say Hello
out.println("Contacts structured by " + ROOT_ELEMENT_TAG+" :
");
// List of entries (fiches)
NodeList entries = TheDocument.getDocumentElement().getElementsByTagName(ROOT_ELEMENT_TAG);
int nEntries = entries.getLength();
// Deal Entry by Entry
for(int i=0; i < nEntries; i++) {
Element currentEntry = (Element) entries.item(i);
NodeList entryItems = currentEntry.getChildNodes();
int nEntryItems = entryItems.getLength();
out.println("- Contact number " + i + "/"
+ nEntries + " :");
out.println("
");
Element lastItem = null;
// Deal with each Item for the current Entry
for (int j=0; j < nEntryItems; j++) {
// test if the item is real element
if (entryItems.item(j) instanceof Element) {
Element item = (Element)entryItems.item(j);
if (lastItem!=null) {
if (lastItem.getTagName()!=item.getTagName()) {
out.println("- " + item.getTagName() + ":
");
}
}
else
{currentEntry.getChildNodes();
out.println("- " + item.getTagName() + "
");
}
out.println("- ");
NodeList itemStringNodes = item.getChildNodes();
String itemValue = new String();
for(int k=0; k");
lastItem = item;
}
}
out.println("
");
out.println("
");
}
out.println("
SUN April 2000");
}
catch (SAXParseException e) {
out.println("The File is not well formed.");
out.println(e.getMessage()
+ " at line " + e.getLineNumber()
+ ", column " + e.getColumnNumber());
}
catch (SAXException e) {
out.println("SAX exception found");
out.println(e.getMessage());
// e.printStackTrace(out);
}
catch (Exception e) {
out.println (e.getMessage());
// e.printStackTrace(out);
out.println ("On arrete tout de suite, reparez l'erreur SVP
");
return;
}
%>
Source