May 25, 2004 at 11:58 pm
For my amusement and at the same time attempting to learn XML, I have
a written a simple dictionary webpage. The user enters a word and and
clicks on the find button. The webpage should search the XML file and
if found populates the webpage with the definition of the word. I
have written the webpage and the XML file. But I am stuck with the
searching part. I can not figure this out. How do I get the webpage
to search the XML file and populate the webpage?
Thanks for your response,
May 26, 2004 at 8:58 am
Hi Daniel,
Are you using asp.net or just an html page? If it's just html, I'm curious too then. If it's like asp.net, you should be able to treat xml as if it were just another data source.
The remainder assumes you're using asp.net; if not hopefully you find this interesting
You need three things; a dataset, the xml file (and schema doc), and like a datagrid or something (just for this example, named dgXML).
Dim MyDataSet as New DataSet()
MyDataSet.ReadXMLSchema(path to xml schema)
MyDataSet.ReadXML(path to xml)
dgXML.DataSource = MyDataSet.Tables("tablename from schema")
dgXML.DataBind()
-Ken
May 26, 2004 at 11:11 am
Thank you for the response. But I am using just HTML.
Thank you ,
May 27, 2004 at 7:55 am
Here's a code snippet that might get you started. This works in Internet Explorer, not sure about any other browsers. This contains an embedded XML "data island" and uses XPath to select a particular node in the XML document. In your case, rather than embedding the XML in the document, you could reference an external XML document. You'd need to adapt it so that it works with your XML and also so that it dynamically builds an XPath expression, based on user's choice, to pick out of the XML what you're loking for. That with some DHTML should get you what you want.
Not saying this is necessarily a good way to do this, just that it's one possible way.
Another method might be to use XSLT, but that might be a bit too heavy for this.
<html>
<head></head>
<body>
<xml id="xmlDoc">
<definitions>
<def>
<phrase>IANAL</phrase>
<definition>I am not a lawyer</definition>
</def>
<def>
<phrase>IIRC</phrase>
<definition>If I recall correctly</definition>
</def>
<def>
<phrase>IMOP</phrase>
<definition>In my opinion</definition>
</def>
</definitions>
</xml>
<script language="javascript">
var xmlSource = new ActiveXObject("MSXML2.DOMDocument.3.0");
xmlSource.async = false;
xmlSource.validateOnParse=false;
xmlSource.setProperty("SelectionLanguage", "XPath");
var xpathnode = document.all("xmlDoc").XMLDocument;
alert(xpathnode.selectSingleNode("//def[phrase='IANAL']/definition").text);
</script>
</body>
</html>
May 27, 2004 at 8:08 am
Oops, my bad, you don't need these lines, take them out:
var xmlSource = new ActiveXObject("MSXML2.DOMDocument.3.0");
xmlSource.async = false;
xmlSource.validateOnParse=false;
xmlSource.setProperty("SelectionLanguage", "XPath");
Sorry about that, I was doing a cut 'n paste.
May 27, 2004 at 8:28 am
OK, here's a "better" version that actually does something.
<html>
<head></head>
<body onload ="init()">
<xml id="xmlDoc">
<definitions>
<def>
<phrase>IANAL</phrase>
<definition>I am not a lawyer</definition>
</def>
<def>
<phrase>IIRC</phrase>
<definition>If I recall correctly</definition>
</def>
<def>
<phrase>IMOP</phrase>
<definition>In my opinion</definition>
</def>
</definitions>
</xml>
<label>Pick a phrase: </label>
<select id="phraseSelect" onchange="PickPhrase()"></select>
<br>
<textarea id="definition"></textarea>
<script language="javascript" defer>
var xpathNode = document.all("xmlDoc").XMLDocument;
function init()
{
// Populate drop-down with phrases
var phraseNodes = xpathNode.selectNodes("//def/phrase");
for (var x = 0; x < phraseNodes.length; x++)
{
thisNode = phraseNodes.item(x);
var newOpt = document.createElement("OPTION");
newOpt.value = thisNode.text;
newOpt.text = thisNode.text;
phraseSelect.options.add(newOpt);
}
phraseSelect.selectedIndex = -1
}
function PickPhrase()
{
// On pick of phrase, get definition for the phrase
phraseControl = event.srcElement;
phrase = phraseControl.options.item(phraseControl.selectedIndex).text;
xPath = "//def[phrase='" + phrase + "']/definition";
definition.innerText = xpathNode.selectSingleNode(xPath).text;
}
</script>
</body>
</html>
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply