Contenuto

Creazione di applicazioni telefono Pubblicazione su WordPress

Databases XML


Questo argomento avanzato viene illustrato come creare due tipi di basi di dati XML molto semplice.

Lettura XML è dritto in avanti. Tuttavia, è Impossibile salvare XML direttamente dall'HTML o Hippani Animator a causa di restrizioni di sicurezza. Per interagire con il server web è necessaria una pagina server. Abbiamo utilizzato pagine ASPX di server scritte in c#. Altre lingue e pagine server potrebbero essere utilizzati invece. ASPX e c# sono oltre la portata di questa documentazione. Per personalizzare ed espandere su questi esempi, approfondimenti possono essere richiesti.

È necessario un server web di Microsoft Windows (IIS) per ospitare i file ASPX. Inoltre devi impostare le autorizzazioni del server web per creare e scrivere file.


Singolo Record dei databases

Questo semplice database memorizza una lista di valori. Ogni valore ha un nome.

Per esempio:

HighScore=10000

PlayerName=Zara

Location=France

Creare un file ASPX sul server web denominato XMLDatabase.aspx. Utilizzare il seguente codice. Un file ASPX è semplicemente un file di testo con l'estensione .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());
%>


La pagina ASPX server gestisce la manipolazione di file XML.

Se esiste il file XML, ASPX carica i dati XML in un dizionario. Quindi controlla per vedere se tutti i valori sono stati ricevuti. Se c'è un nuovo valore, aggiungerla al dizionario, aggiornare il valore corrente, se già esistente. Crea quindi un nuovo file XML basato su dizionario. Se ci fosse un nuovo valore aggiunto, viene salvato il nuovo file XML. Infine, il XML viene restituito.

È possibile testare la pagina aprendola in qualsiasi browser web.

Caricamento della pagina ASPX e includendo un nome e un valore viene inviati dati.

Per esempio:

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

I dati saranno archivio in un file XML che assomiglia a questo:


<Data>
<HighScore>10000</HighScore>
<PlayerName>Zara</PlayerName>
<Location>France</Location>
</Data>

Questo è un esempio di come inviare i dati alla pagina ASPX 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);
}
}

Ricevuti dati semplicemente caricando il file ASPX.

Per esempio:
http://www.MyWebServer.com/XMLDatabase.aspx

Questo è un esempio di come ottenere i dati dalla pagina ASPX 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!!!");
}
}

Databases con Record multipli

Questo database più complesso memorizza una lista di record. Ogni record contiene un elenco di valori. Ogni valore ha un nome.

Per esempio:

Record di 1:

FirstName=Andrew

Age=23

Record di 2:

FirstName=Andrea

Age=42

Questo tipo di database XML funzionerà efficacemente per fino a 100 record. I più grandi database avrebbe bisogno di più file XML o essere collegati a un database adeguato ad esempio SQL.

Creare un altro file ASPX sul server web denominato XMLDatabase2.aspx. Utilizzare il seguente codice.


<%@ 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());
%>


Questa server la pagina ASPX è più complessa. Ancora una volta, gestisce la manipolazione di file XML.

Se esiste il file XML, ASPX carica i dati XML in un dizionario. Quindi controlla per vedere se tutti i valori sono stati ricevuti. Se c'è un nuovo valore e un indice, aggiungerlo al record nel dizionario, aggiornare il valore corrente, se già esistente. Crea quindi un nuovo file XML basato su dizionario. Se ci fosse un nuovo valore aggiunto, viene salvato il nuovo file XML. Infine, il XML viene restituito.

È possibile testare la pagina aprendola in qualsiasi browser web.

Caricando la pagina ASPX e compreso un indice, il nome e il valore viene inviati dati.

Per esempio:

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

I dati saranno archivio in un file XML che assomiglia a questo:


<Data>
<Record Index="1">
<FirstName>Andrew</FirstName>
<Age>23</Age>
</Record>
<Record Index="2">
<FirstName>Andrea</FirstName>
<Age>42</Age>
</Record>
</Data>

Questo è un esempio di come inviare i dati alla pagina ASPX 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);
}
}


Ricevuti dati semplicemente caricando il file ASPX.

Per esempio:
http://www.MyWebServer.com/XMLDatabase.aspx

Questo è un esempio di come ottenere i dati dalla pagina ASPX 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!!!");
}
}

Creazione di applicazioni telefono Pubblicazione su WordPress
Questa documentazione è stato tradotto dall'inglese, utilizzando un traduttore online. Ci scusiamo se trovate eventuali errori. Se volete aiutarci a fare le correzioni. C'è un editor di traduzione in Hippani Animator (nel menu aiuto). Noi diamo le chiavi di licenza gratuita a chiunque corregge più di 100 frasi. Si prega di contattarci per maggiori dettagli.