2013-06-10 15 views
6

Per il mio corso, mi è stato chiesto di creare una pagina html che abbia la funzionalità di consentire a un utente di inserire il nome di un contatto, che invia, carica un file xml I precedentemente creato, scorre i contatti fino a quando non corrisponde al nome immesso dall'utente e visualizza le informazioni di contatto, ad eccezione dell'indirizzo e-mail, nella stessa pagina, in una tabella con intestazioni e una visualizzazione <h1> I dettagli del contatto sono :. Se non c'è corrispondenza, ci dovrebbe essere un <h2> che dice che il contatto non esiste.Cerca XML con JavaScript e visualizza i risultati nella tabella

il seguente è il mio file XML:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="Contact.xsl" ?> 
<!DOCTYPE contact SYSTEM "contact.dtd"> 
<contact> 
    <person> 
     <name> 
      <firstname>Bob</firstname> 
      <lastname>Jones</lastname> 
     </name> 
     <phone>(02)44 42 45 63</phone> 
     <address> 
      <street>34 Highway Road</street> 
      <city>Grovedale</city> 
      <state>NSW</state> 
      <postcode>3228</postcode> 
      <email>[email protected]</email> 
     </address> 
    </person> 
    <person> 
     <name> 
      <firstname>Gary</firstname> 
      <lastname>Williams</lastname> 
     </name> 
     <phone>(02)44 41 87 56</phone> 
     <address> 
      <street>223 Yarra Avenue</street> 
      <city>Branston</city> 
      <state>NSW</state> 
      <postcode>2317</postcode> 
      <email>[email protected]</email> 
     </address> 
    </person> 

ho provato un paio di cose, ma non ho idea di come avrei ottenere i dati da visualizzare in una tabella. Quello che segue è il mio file XSL che è come presumo che vogliano mostrare la tabella, ma con i risultati della ricerca.

<?xml version="1.0" encoding="ISO-8859-1"?><!-- DWXMLSource="Contact.xml" --> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<head> 
<style> 
body { 
    font-family: Arial, Helvetica, sans-serif; 
    font-size: 14px; 
} 
div { 
    margin-left: 150px; 
    margin-right: 20px; 
    margin-top: 50px; 
    width: 80%; 
} 
h1 { 
    font-size: 24px; 
    color: #F00; 
} 
.headings { 
    background-color: #06F; 
} 
.data { 
    background-color: #6F9; 
} 
.table { 
    border: 2px solid #F00; 
    width: 100%; 
} 
</style> 
</head> 
<body> 
<div> 
<h1>CONTACTS</h1> 
    <table class="table"> 
     <tr class="headings"> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Street</th> 
      <th>City</th> 
      <th>State</th> 
      <th>Postcode</th> 
      <th>Email</th> 
     </tr> 
     <xsl:for-each select="contact/person"> 
     <tr class="data"> 
      <td><xsl:value-of select="name/firstname"/></td> 
      <td><xsl:value-of select="name/lastname"/></td> 
      <td><xsl:value-of select="address/street"/></td> 
      <td><xsl:value-of select="address/city"/></td> 
      <td><xsl:value-of select="address/state"/></td> 
      <td><xsl:value-of select="address/postcode"/></td> 
      <td><xsl:value-of select="address/email"/></td> 
     </tr> 
    </xsl:for-each> 
</table> 
</div> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

Devo usare JavaScript per cercare il file XML e creare una tabella che mostri i risultati della ricerca.

Il codice HTML che ho è la seguente:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Task 2</title> 
<script language="JavaScript" type="text/javascript"> 
window.onload = loadIndex; 
function loadIndex() 
{ 
    if (document.implementation && document.implementation.createDocument) 
    { 
     xmlDoc = document.implementation.createDocument("", "", null); 
     xmlDoc.load("Contact.xml"); 
    } 
    else if (window.ActiveXObject) 
    { 
     xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
     xmlDoc.async = "false"; 
     xmlDoc.load("Contact.xml"); 
    } 
} 
function searchIndex() 
{ 
    if (!xmlDoc) 
    { 
     loadIndex(); 
    } 
    var searchterm = document.getElementById("searchme").value; 
    var allitems = xmlDoc.getElementsByTagName("firstname"); 
    results = new Array; 
    for (var i=0;i<allitems.length;i++) 
    { 
     var name = allitems[i].lastChild.nodeValue; 
     var exp = new RegExp(searchterm,"i"); 
     if (name.match(exp) != null) 
     { 
      results.push(allitems[i]); 
     } 
    } 
    showResults(results, searchterm); 
} 
function showResults(results, searchterm) 
{ 
    //insert table data here to be displayed 
} 
</script> 
</head> 
<body> 
First Name: <input type="text" name="firstname" id="searchme"> 
<br /> 
<input type="submit" value="Search" onClick="searchIndex(); return false;"> 
</body> 
</html> 

Come potete vedere, non ho idea da dove cominciare. So che vorrei caricare il file XML, quindi raccogliere il primo tag name, quindi in qualche modo confrontare i due quindi visualizzare i risultati.

Ho visto quanto segue, e questo è simile a quello che sto dopo, ma non posso ottenere i risultati da visualizzare in una tabella. http://www.dzone.com/snippets/simple-javascriptxml-based

Grazie per l'aiuto.

+0

dov'è la funzione 'searchIndex'? –

+0

Da nessuna parte al momento. Come ho detto. Non ho molta idea da dove cominciare. Ill modificare il sopra per quello che penso. – Curley5959

+0

Aggiornamento del codice. – Curley5959

risposta

1

Il seguente è quello che ho usato per risolvere i problemi che stavo avendo. Un problema che stavo facendo è testarlo localmente. Non funzionerà. Anche questo è in attesa di un messaggio se non viene trovato alcun contatto. Aggiornerò quando trovo la soluzione.

UPDATE: trovata la soluzione. Vedere codice aggiornato di seguito:

<script language="JavaScript" type="text/javascript"> 
function loadXMLDoc(dname) 
{ 
    if (window.XMLHttpRequest) 
    { 
     xhttp=new XMLHttpRequest(); 
    } 
    else 
    { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xhttp.open("GET",dname,false); 
    xhttp.send(); 
    return xhttp.responseXML; 
} 
function searchXML() 
{ 
    xmlDoc=loadXMLDoc("Contact.xml"); 
    x=xmlDoc.getElementsByTagName("firstname"); 
    input = document.getElementById("input").value; 
    size = input.length; 
    if (input == null || input == "") 
    { 
     document.getElementById("results").innerHTML= "Please enter a character or name!"; 
     return false; 
    } 
    for (i=0;i<x.length;i++) 
    { 
     startString = firstname.substring(0,size); 
     if (startString.toLowerCase() == input.toLowerCase()) 
     { 
      firstname=xmlDoc.getElementsByTagName("firstname")[i].childNodes[0].nodeValue; 
      lastname=xmlDoc.getElementsByTagName("lastname")[i].childNodes[0].nodeValue; 
      phone=xmlDoc.getElementsByTagName("phone")[i].childNodes[0].nodeValue; 
      street=xmlDoc.getElementsByTagName("street")[i].childNodes[0].nodeValue; 
      city=xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue; 
      state=xmlDoc.getElementsByTagName("state")[i].childNodes[0].nodeValue; 
      postcode=xmlDoc.getElementsByTagName("postcode")[i].childNodes[0].nodeValue; 
      divText = "<h1>The contact details are:</h1><br /><table border=1><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Street</th><th>City</th><th>State</th><th>Postcode</th></tr>" + "<tr><td>" + firstname + "</td><td>" + lastname + "</td><td>" + phone + "</td><td>" + street + "</td><td>" + city + "</td><td>" + state + "</td><td>" + postcode + "</td></tr>" + "</table>"; 
      break; 
     } 
     else 
     { 
      divText = "<h2>The contact does not exist.</h2>"; 
     } 
    } 
    document.getElementById("results").innerHTML= divText; 
} 
</script> 

Il mio corpo HTML:

<body> 
First Name: <input type="text" name="firstname" id="input"> 
<br /> 
<input type="submit" value="Submit" onClick="searchXML()"> 
<br /> 
<br /> 
<div id="results"> 
</div> 
</body> 

spero che questo possa aiutare qualcun altro fuori.

0
startString = firstname.substring(0,size); 

Qui, firstname c'è dove definire prima del ciclo for, è per questo che dare sotto l'errore.

Uncaught TypeError: Cannot read property 'substring' of undefined.

0

Ho fatto riferimento al nome come variabile prima di startString.

firstname = xmlDoc.getElementsByTagName("firstname")[i].childNodes[0].nodeValue; 
startString = firstname.substring(0,size); 

Ha funzionato benissimo dopo. Spero possa aiutare!

Problemi correlati