创建手机应用程序 在上发布 WordPress

XML 数据库

此高级的主题演示如何创建两种非常简单的 XML 数据库。阅读 XML 是笔直向前。然而,它是不可能直接从 HTML 保存 XML 或 Hippani Animator 由于安全限制。服务器页面需要与 web 服务器进行交互。我们已经习惯用 C# 编写的 ASPX 服务器页面。其他服务器页面和语言可供使用。ASPX 和 C# 都超出了本文档的范围。若要自定义并扩展这些例子,可能需要进一步阅读。您将需要 Microsoft Windows web 服务器 (IIS) 主办的 ASPX 文件。您还需要设置 web 服务器权限,以创建和写入文件。


单个记录数据库

这个简单的数据库存储的值的列表。每个值有一个名称。举个例子:HighScore=10000

PlayerName=Zara

Location=France创建一个称为 web 服务器上的 ASPX 文件 XMLDatabase.aspx。使用下面的代码。ASPX 文件是简单的文本文件,扩展名为 .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());
%>

ASPX 服务器页面处理 XML 文件的操作。如果 XML 文件存在,然后 ASPX 就会将 XML 数据加载到一本字典。然后,它检查以查看是否已收到任何新的值。如果有一个新的值,将它添加到字典中,更新的当前值,如果它已经存在。然后,它创建一个新的 XML 文件,基于字典。如果没有添加一个新值,然后保存新的 XML 文件。最后,返回的 XML。您可以通过在任何 web 浏览器中打开测试页。数据发送的 ASPX 页的加载和包括名称和值。举个例子: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数据将存储在一个 XML 文件,看起来像这样:


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

这是一个例子如何将数据发送到的 ASPX 页 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);
}
}

通过简单地加载 ASPX 文件接收数据。举个例子:http://www.MyWebServer.com/XMLDatabase.aspx这是一个例子如何从 ASPX 页中获取数据 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!!!");
}
}

多个记录数据库

这更复杂的数据库存储的记录的列表。每个记录包含的值的列表。每个值有一个名称。举个例子:记录 1:FirstName=Andrew

Age=23记录 2:FirstName=Andrea

Age=42这种类型的 XML 数据库将为达 100 条记录有效地工作。更大的数据库也需要多个 XML 文件或连接到正确的数据库,如 SQL。创建称为 web 服务器上的另一个 ASPX 文件 XMLDatabase2.aspx。使用下面的代码。


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

此服务器 ASPX 页面会更加复杂。再次,它可以处理 XML 文件的操作。如果 XML 文件存在,然后 ASPX 就会将 XML 数据加载到一本字典。然后,它检查以查看是否已收到任何新的值。如果有一个新的值和索引,将它添加到字典中记录,更新当前值,如果它已经存在。然后,它创建一个新的 XML 文件,基于字典。如果没有添加一个新值,然后保存新的 XML 文件。最后,返回的 XML。您可以通过在任何 web 浏览器中打开测试页。数据发送的 ASPX 页的加载和包括索引、 名称和值。举个例子: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数据将存储在一个 XML 文件,看起来像这样:


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

这是一个例子如何将数据发送到的 ASPX 页 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);
}
}

通过简单地加载 ASPX 文件接收数据。举个例子:http://www.MyWebServer.com/XMLDatabase.aspx这是一个例子如何从 ASPX 页中获取数据 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!!!");
}
}

创建手机应用程序 在上发布 WordPress
本文档是从使用在线翻译的英语翻译。如果您发现任何错误,我们深表歉意。如果你想要帮助我们改正错误。还有一个翻译编辑器在 Hippani Animator (在帮助菜单上)。我们给出免费许可证密钥谁纠正了超过 100 个短语。请更多详细信息,联系我们。