XML Databases
This advanced topic demonstrates how to create two kinds of very simple XML databases.
Reading XML is straight forward. However, it is impossible to save XML directly from HTML or Hippani Animator due to security restrictions. A server page is required to interact with the web server. We've used ASPX server pages written in C#. Other server pages and languages could be used instead. ASPX and C# are beyond the scope of this documentation. To customize and expand on these examples, further reading may be required.
You will need a Microsoft Windows web server (IIS) to host the ASPX files. You will also need to set web server permissions to create and write files.
Single Record Databases
This simple database stores a list of values. Each value has a name.
For example:
HighScore=10000
PlayerName=Zara
Location=France
Create an ASPX file on your web server called XMLDatabase.aspx. Use the following code. An ASPX file is simply a text file with the extension .aspx.
<%@ Page Language="C#" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Collections.Generic" %>
<%
string DataFilename=Server.MapPath("Data.xml");
Dictionary<string,string> MyData=new Dictionary<string,string>();
bool DataChanged=false;
// Load the xml data into a dictionary
if(File.Exists(DataFilename)){
XmlDocument Doc=new XmlDocument();
Doc.Load(DataFilename);
XmlNode N=Doc.DocumentElement.FirstChild;
while(N!=null){ //Iterate through the nodes
if(!MyData.ContainsKey(N.Name)){ //If the dictionary does not contain the node name, add it and the text it contains.
MyData.Add(N.Name,N.InnerText);
}
N=N.NextSibling;
}
}
//Add any new Name and Value values sent to the page.
string Name=Context.Request["Name"];
string Value=Context.Request["Value"];
if(!String.IsNullOrEmpty(Name)){
if(!MyData.ContainsKey(Name)){ //If the dictionary does not contain the name, add it and the value.
MyData.Add(Name,Value);
}else{ //Replace the value if the name already exists in the dictionary.
MyData[Name]=Value;
}
DataChanged=true;
}
//Generate a new xml file.
StringBuilder NewXML=new StringBuilder();
NewXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
NewXML.Append("<Data>\r\n");
Dictionary<string,string>.Enumerator Enum=MyData.GetEnumerator();
while(Enum.MoveNext()){
NewXML.Append("<"+Enum.Current.Key+">"+HttpUtility.HtmlEncode(Enum.Current.Value)+"</"+Enum.Current.Key+">\r\n");
}
NewXML.Append("</Data>\r\n");
//Save the data file if it has been changed.
if(DataChanged){
StreamWriter SW = new StreamWriter(DataFilename);
SW.WriteLine(NewXML.ToString());
SW.Close();
}
//Return the xml file.
Context.Response.ContentType = "text/xml";
Response.Write(NewXML.ToString());
%>
The ASPX server page handles the manipulation of an XML file.
If the XML file exists, then the ASPX loads the XML data into a dictionary. Then it checks to see if any new values have been received. If there is a new value, add it to the dictionary, update the current value if it already exists. It then creates a new XML file based on the dictionary. If there was a new value added, then the new XML file is saved. Finally, the XML is returned.
You can test the page by opening it in any web browser.
Data is sent by loading the ASPX page and including a name and value.
For example:
http://www.MyWebServer.com/XMLDatabase.aspx?Name=HighScore&Value=10000
http://www.MyWebServer.com/XMLDatabase.aspx?Name=PlayerName&Value=Zara
http://www.MyWebServer.com/XMLDatabase.aspx?Name=Location&Value=France
The data will be store in an XML file that looks like this:
<Data>
<HighScore>10000</HighScore>
<PlayerName>Zara</PlayerName>
<Location>France</Location>
</Data>
This is an example of how to send data to the ASPX page in Hippani Animator.
var Name="HighScore";
var Value="10000";
OpenXMLUrl("http://www.MyWebServer.com/XMLDatabase.aspx?Name="+escape(Name)+"&Value="+escape(Value),Result);
function Result(XML){
if(XML.status==200){
alert("Saved.");
}else{
alert("Error Code "+XML.status);
}
}
Data is received by simply loading the ASPX file.
For example:
http://www.MyWebServer.com/XMLDatabase.aspx
This is an example of how to get data from the ASPX page in Hippani Animator.
OpenXMLUrl("http://www.MyWebServer.com/XMLDatabase.aspx",Result);
function Result(XML){
if(XML.status==200){
var Node=XML.documentElement.firstChild;
var List="List:\r\n";
while(Node!=null){
List+=Node.nodeName+"="+Node.firstChild.nodeValue+"\r\n";
Node=Node.nextSibling;
}
alert(List);
}else{
alert(XML.status+" Error!!!");
}
}
Multiple Record Databases
This more complex database stores a list of records. Each record contains a list of values. Each value has a name.
For example:
Record 1:
FirstName=Andrew
Age=23
Record 2:
FirstName=Andrea
Age=42
This type of XML database will work effectively for up to 100 records. Larger databases would either need multiple XML files or be connected to a proper database such as SQL.
Create another ASPX file on your web server called XMLDatabase2.aspx. Use the following code.
<%@ Page Language="C#" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Collections.Generic" %>
<%
string DataFilename=Server.MapPath("Data2.xml");
Dictionary<int,Dictionary<string,string>> MyData=new Dictionary<int,Dictionary<string,string>>();
bool DataChanged=false;
Dictionary<string,string> Record=null;
int Index=0;
// Load the xml data into a dictionary
if(File.Exists(DataFilename)){
XmlDocument Doc=new XmlDocument();
Doc.Load(DataFilename);
XmlNode N=Doc.DocumentElement.FirstChild,M=null;
while(N!=null){ //Iterate through the record nodes
Index=Convert.ToInt32(N.Attributes["Index"].Value); //Get the index value of the record.
Record=new Dictionary<string,string>();
M=N.FirstChild;
while(M!=null){ //Iterate through the value nodes
if(!Record.ContainsKey(M.Name)){ //If the record does not contain the node name, add it and the text it contains.
Record.Add(M.Name,M.InnerText);
}
M=M.NextSibling;
}
if(!MyData.ContainsKey(Index)){ //If the dictionary does not contain the record index, add it.
MyData.Add(Index,Record);
}
N=N.NextSibling;
}
}
//Add any new Name and Value values sent to the page. Use the Index value to locate the correct record.
string Name=Context.Request["Name"];
string Value=Context.Request["Value"];
Index=Convert.ToInt32(Context.Request["Index"]);
if(!String.IsNullOrEmpty(Name)&&Index>0){
if(MyData.ContainsKey(Index)){ //If the dictionary contains the record index.
Record=MyData[Index];
}else{ //If the dictionary does not contain the record. Create a new empty record and add it.
Record=new Dictionary<string,string>();
MyData.Add(Index,Record);
}
if(!Record.ContainsKey(Name)){ //If the record does not contain the name, add it and the value.
Record.Add(Name,Value);
}else{ //Replace the value if the name already exists in the record.
Record[Name]=Value;
}
DataChanged=true;
}
//Generate a new xml file.
StringBuilder NewXML=new StringBuilder();
NewXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
NewXML.Append("<Data>\r\n");
Dictionary<int,Dictionary<string,string>>.Enumerator Enum=MyData.GetEnumerator();
Dictionary<string,string>.Enumerator EnumRecord;
while(Enum.MoveNext()){
NewXML.Append("<Record Index=\""+Enum.Current.Key+"\">\r\n");
EnumRecord=Enum.Current.Value.GetEnumerator();
while(EnumRecord.MoveNext()){
NewXML.Append("<"+EnumRecord.Current.Key+">"+HttpUtility.HtmlEncode(EnumRecord.Current.Value)+"</"+EnumRecord.Current.Key+">\r\n");
}
NewXML.Append("</Record>\r\n");
}
NewXML.Append("</Data>\r\n");
//Save the data file if it has been changed.
if(DataChanged){
StreamWriter SW = new StreamWriter(DataFilename);
SW.WriteLine(NewXML.ToString());
SW.Close();
}
//Return the xml file.
Context.Response.ContentType = "text/xml";
Response.Write(NewXML.ToString());
%>
This ASPX server page is more complex. Again, it handles the manipulation of an XML file.
If the XML file exists, then the ASPX loads the XML data into a dictionary. Then it checks to see if any new values have been received. If there is a new value and an index, add it to the record in the dictionary, update the current value if it already exists. It then creates a new XML file based on the dictionary. If there was a new value added, then the new XML file is saved. Finally, the XML is returned.
You can test the page by opening it in any web browser.
Data is sent by loading the ASPX page and including an index, name and value.
For example:
http://www.MyWebServer.com/XMLDatabase2.aspx?Index=1&Name=FirstName&Value=Andrew
http://www.MyWebServer.com/XMLDatabase2.aspx?Index=1&Name=Age&Value=23
http://www.MyWebServer.com/XMLDatabase2.aspx?Index=2&Name=FirstName&Value=Andrea
http://www.MyWebServer.com/XMLDatabase2.aspx?Index=2&Name=Age&Value=42
The data will be store in an XML file that looks like this:
<Data>
<Record Index="1">
<FirstName>Andrew</FirstName>
<Age>23</Age>
</Record>
<Record Index="2">
<FirstName>Andrea</FirstName>
<Age>42</Age>
</Record>
</Data>
This is an example of how to send data to the ASPX page in Hippani Animator.
var Index=1;
var Name="FirstName";
var Value="Andrew";
OpenXMLUrl("http://www.MyWebServer.com/XMLDatabase2.aspx?Index="+Index+"&Name="+escape(Name)+"&Value="+escape(Value),Result);
function Result(XML){
if(XML.status==200){
alert("Saved.");
}else{
alert("Error Code "+XML.status);
}
}
Data is received by simply loading the ASPX file.
For example:
http://www.MyWebServer.com/XMLDatabase.aspx
This is an example of how to get data from the ASPX page in Hippani Animator.
OpenXMLUrl("http://www.MyWebServer.com/XMLDatabase2.aspx",Result);
function Result(XML){
if(XML.status==200){
var Record=XML.documentElement.firstChild,Node=null;
var List="List:\r\n";
while(Record!=null){
List+="Record: "+Record.attributes.getNamedItem("Index").value+"\r\n";
Node=Record.firstChild;
while(Node!=null){
List+=" "+Node.nodeName+"="+Node.firstChild.nodeValue+"\r\n";
Node=Node.nextSibling;
}
Record=Record.nextSibling;
}
alert(List);
}else{
alert(XML.status+" Error!!!");
}
}