XML podatkovnih baz
Te napredne teme prikazuje, kako ustvariti dve vrsti zelo preproste zbirke podatkov XML.
Branje XML je naravnost naprej. Vendar je nemogoče za shranjevanje XML neposredno iz HTML ali Hippani Animator zaradi varnostnih omejitev. Stran strežnika je potrebno interakcijo s spletnim strežnikom. Uporabili smo strežniške strani ASPX, napisana v C#. Drugi strežnik strani in jeziki se lahko uporabijo namesto tega. ASPX in C#, ki so izven področja uporabe te dokumentacije. Prilagoditi in razširiti na te primere, lahko zahteva nadaljnje branje.
Boste potrebovali Microsoft Windows spletnega strežnika (IIS) za gostitelja datotek ASPX. Prav tako boste morali nastaviti spletni strežnik dovoljenja za ustvarjanje in pisanje datotek.
Posamezen zapis zbirk podatkov
Ta preprost zbirke podatkov Shrani seznam vrednosti. Vsaka vrednost je ime.
Na primer:
HighScore=10000
PlayerName=Zara
Location=France
Ustvarjanje ASPX datoteke na spletni strežnik imenuje XMLDatabase.aspx. Uporabite naslednjo kodo. ASPX datoteke je preprosto besedilno datoteko s pripono .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());
%>
Strani ASPX strežnik ročaji manipulacijo datoteko XML.
Če XML datoteka obstaja, potem je ASPX naloži podatke XML v slovar. Nato to preveri, če so bile prejete vsak nove vrednosti. Če obstaja novo vrednost, dodati v slovar, posodobi trenutno vrednost, če že obstaja. Nato ustvari nov XML pila osnova naprej slovar. Če je bilo dodano novo vrednost, nov XML datoteka shranjena. Končno, se je vrnil XML.
Testirate lahko stran odprete v kateri koli spletni brskalnik.
Podatke pošlje nakladanje ASPX strani, in vključno z imenom in vrednostjo.
Na primer:
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
Podatki bodo shrambe v datoteko XML, ki izgleda takole:
<Data>
<HighScore>10000</HighScore>
<PlayerName>Zara</PlayerName>
<Location>France</Location>
</Data>
To je primer, kako poslati podatke na strani ASPX v 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);
}
}
Podatke prejme preprosto nalaganje datoteke ASPX.
Na primer:
http://www.MyWebServer.com/XMLDatabase.aspx
To je primer, kako dobiti podatkov iz strani ASPX v 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!!!");
}
}
Več zapisov podatkovnih baz
Bolj zapletene zbirke podatkov Shrani seznam zapisov. Vsak zapis vsebuje seznam vrednosti. Vsaka vrednost je ime.
Na primer:
1. zapis:
FirstName=Andrew
Age=23
2. zapis:
FirstName=Andrea
Age=42
Ta vrsta zbirke podatkov XML bo učinkovito delo za do 100 zapisov. Večje zbirk podatkov bodisi potrebujejo več datotek XML ali priključi pravilno baze kot so SQL.
Ustvarite drugo ASPX datoteko na svoj spletni strežnik, ki se imenuje XMLDatabase2.aspx. Uporabite naslednjo kodo.
<%@ 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());
%>
Ta ASPX strani strežnika je bolj zapletena. Again, ročaji manipulacijo datoteko XML.
Če XML datoteka obstaja, potem je ASPX naloži podatke XML v slovar. Nato to preveri, če so bile prejete vsak nove vrednosti. Če je novo vrednost in stvarno kazalo, dodajte zapis v slovarju, posodobi trenutno vrednost, če že obstaja. Nato ustvari nov XML pila osnova naprej slovar. Če je bilo dodano novo vrednost, nov XML datoteka shranjena. Končno, se je vrnil XML.
Testirate lahko stran odprete v kateri koli spletni brskalnik.
Podatke pošlje nakladanje ASPX strani in indeks, ime in vrednost.
Na primer:
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
Podatki bodo shrambe v datoteko XML, ki izgleda takole:
<Data>
<Record Index="1">
<FirstName>Andrew</FirstName>
<Age>23</Age>
</Record>
<Record Index="2">
<FirstName>Andrea</FirstName>
<Age>42</Age>
</Record>
</Data>
To je primer, kako poslati podatke na strani ASPX v 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);
}
}
Podatke prejme preprosto nalaganje datoteke ASPX.
Na primer:
http://www.MyWebServer.com/XMLDatabase.aspx
To je primer, kako dobiti podatkov iz strani ASPX v 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!!!");
}
}
Ta dokumentacija je bila prevedena iz angleščini, uporabo online prevajalec. Opravičujemo se, če najdete kakšne napake. Če želite pomoč nas naredi popravke. Je prevod urednik v Hippani Animator (v meniju pomoč). Smo izžarevati prost licenca sklepnik vsakomur, ki popravlja več kot 100 stavkov. Prosimo, kontaktirajte nas za več podrobnosti.