XML データベース
この高度なトピックは非常に単純な XML データベースの 2 種類を作成する方法を示します。読書 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=42XML データベースのこのタイプは、最大 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!!!");
}
}
このドキュメントは、オンライン翻訳を使用して英語から翻訳されました。我々 は、任意のミスを発見した場合をお詫び申し上げます。私たちの修正に役立つ場合は。翻訳エディターでは Hippani Animator ([ヘルプ] メニュー)。我々 は以上 100 フレーズを修正誰かに自由な免許証のキーを与えます。詳細については、お問い合わせください。