XML 데이터베이스
이 고급 항목에서는 두 종류의 매우 간단한 XML 데이터베이스를 만드는 방법을 보여 줍니다.
읽기 XML은 똑바로 앞으로. 그러나, 그것은 HTML에서 직접 XML을 저장할 수 또는 Hippani Animator 보안 제한으로 인해. 서버 페이지는 웹 서버와 상호 작용 해야 합니다. 우리는 C#으로 작성 된 ASPX 서버 페이지를 사용 했습니다. 다른 서버 페이지 및 언어 대신 사용 될 수 있습니다. ASPX 및 C#는이 설명서의 범위를 벗어납니다. 사용자 지정 하 고이 예제를 확장, 추가 읽기 필요할 수 있습니다.
마이크로 소프트 윈도우 웹 서버 (IIS) 호스트 ASPX 파일에 필요 합니다. 쓰기 파일을 만들고 웹 서버 사용 권한을 설정 해야 합니다.
단일 레코드 데이터베이스
이 간단한 데이터베이스 값의 목록이 저장 됩니다. 각 값에는 이름이 있습니다.
예를 들어:
HighScore=10000
PlayerName=Zara
Location=France
라는 웹 서버에서 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이 반환 됩니다.
어떤 웹 브라우저 든 지에서 그것을 열어 페이지를 테스트할 수 있습니다.
데이터는 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과 같은 적절 한 데이터베이스에 연결 합니다.
라는 웹 서버에서 다른 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이 반환 됩니다.
어떤 웹 브라우저 든 지에서 그것을 열어 페이지를 테스트할 수 있습니다.
데이터는 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 이상의 문구를 수정 하는 사람에 게 무료 라이센스 키를 알려주지. 자세한 내용은 문의 하시기 바랍니다.