Javascript: xml reading and writing into xml implementation code _ javascript skills

Source: Internet
Author: User
Javascriptxml reading and writing xml implementation code

Add data:

Data display:

ClassModel. js source code ::

The Code is as follows:


ClassModel =
{
Create: function ()
{
Return function ()
{
This. construct. apply (this, arguments );
}
}
}
Extend = function (desc, src)
{
For (var c in src)
{
Desc [c] = src [c];
}
Return desc;
}
Object. prototype. extend = function (src)
{
Return Extend. apply (this, [this, src]);
}


AddData. js source code ::

The Code is as follows:


Var insert = ClassModel. create ();
Var doc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
Doc. load ("books. xml ");
Var books;
Insert. prototype =
{
Construct: function (config)
{
This. id = config. id;
This. name = config. name;
This. author = config. author;
This. price = config. price;
This. publisher = config. publisher;
This. count = config. count;
This. insertData ();
},
InsertData: function ()
{

Var book = doc. createElement ("book ");

Book. setAttribute ("id", this. id );
Var name = doc. createElement ("name ");
Var nameValue = doc. createTextNode (this. name );
Name. appendChild (nameValue );
Book. appendChild (name );
Var author = doc. createElement ("author ");
Var authorValue = doc. createTextNode (this. author );
Author. appendChild (authorValue );
Book. appendChild (author );
Var price = doc. createElement ("price ");
Var priceValue = doc. createTextNode (this. price );
Price. appendChild (priceValue );
Book. appendChild (price );

Var count = doc. createElement ("count ");
Var countValue = doc. createTextNode (this. count );
Count. appendChild (countValue );
Book. appendChild (count );
Var publisher = doc. createElement ("publisher ");
Var publisherValue = doc. createTextNode (this. publisher );
Publisher. appendChild (publisherValue );
Book. appendChild (publisher );



If(doc.doc umentElement = null)
{

Books = doc. createElement ("books ");
Books. appendChild (book );
Doc. appendChild (books );
}
Else
{
Books = doc.doc umentElement;
Books. appendChild (book );

}
Doc. save ("books. xml ");
}
}


Window. js source code ::

The Code is as follows:


Var windows = ClassModel. create ();
Windows. prototype =
{
Construct: function (jsonObject)
{
This. title = jsonObject. title;
This. width = jsonObject. width;
This. height = jsonObject. height;
This. titleColor = jsonObject. titleColor;
This. backgroundColor = jsonObject. backgroundColor;
This. LwHeight = (document. body. clientHeight-this. width)/2; // put p in the middle of the screen
This. LwWidth = (document. body. clientWidth-this. height)/2; // put p in the middle of the screen
This. content = jsonObject. content;
Var loginWindow = this. createLoginBody ();
Var title = this. createLoginTitle ();
LoginWindow. appendChild (title );
Var cont = this. createContent ();
LoginWindow. appendChild (cont );
Document. body. appendChild (loginWindow );
},
CreateLoginBody: function () // create a login box, that is, the entire box
{
Var loginWindow = document. createElement ("p ");
LoginWindow. id = "dialog ";
With (loginWindow. style)
{
Border = "1px solid white ";
Position = "absolute ";
Width = this. width + "px ";
Height = this. height + "px ";
Top = this. LwHeight + "px ";
Left = this. LwWidth + "px ";
BackgroundColor = this. backgroundColor;
}
Return loginWindow;
},
CreateLoginTitle: function () // create a black title
{
Var title = document. createElement ("p ");
Var table = document. createElement ("table ");
Var tbody = document. createElement ("tbody ");
Var tr = document. createElement ("tr ");
Var td_1 = document. createElement ("td ");
Var td_2 = document. createElement ("td ");
Var close = document. createElement ("");
Close. onclick = function ()
{
Document. body. removeChild (title. parentNode );
}
Close. innerHTML = "X ";
Td_1.innerHTML = this. title;
With (title. style)
{
Width = "100% ";
Height = this. height/10 + "px ";
BackgroundColor = this. titleColor;
}
With (table. style)
{
Color = "white ";
FontSize = "12pt ";
Width = "100% ";
BackgroundColor = this. titleColor;
Color = "red ";
}
Td_2.style.textAlign = "right ";
Td_2.appendChild (close );
Tr. appendChild (td_1 );
Tr. appendChild (td_2 );
Tbody. appendChild (tr );
Table. appendChild (tbody );
Title. appendChild (table );
Return title;
},
CreateContent: function ()
{
Var p = document. createElement ("p ");
If (typeof (this. content) = 'string ')
{
P. innerHTML = this. content;
} Else
{
P. appendChild (this. content );
}
With (p. style)
{
PaddingLeft = "80px ";
PaddingTop = "50px ";
Float = "left ";
Width = "100% ";
}
Return p;
}
}


Book_infor.js source code ::

The Code is as follows:


Var doc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
Doc. load ("books. xml ");
Var query = ClassModel. create ();
Var v = 0;
Query. prototype =
{
Construct: function ()
{
This. bookInfor ();
},
BookInfor: function ()
{
Var p = document. createElement ("p ");
Var root = doc.doc umentElement;
If (root = null)
{
P. innerHTML = "no data ";
Document. body. appendChild (p );
} Else
{


With (p. style)
{
MarginLeft = "200px ";
Overflow = "auto ";
Border = "0px solid white ";
Width = "605px ";

}
Var table = document. createElement ("table ");
Table. cellSpacing = "0 ";
With (table. style)
{
FontSize = "12pt ";
Color = "white ";
Border = "0px ";
Width = "600px ";

}
Var tbody = document. createElement ("tbody ");
Var trHead = document. createElement ("tr ");
With (trHead. style)
{
Height = "20px ";
BackgroundColor = "Transparent ";

}
Var tname = document. createElement ("td ");
Var tauthor = document. createElement ("td ");
Var tprice = document. createElement ("td ");
Var tCount = document. createElement ("td ");
Var tpublisher = document. createElement ("td ");

Tname. innerHTML = "name ";
Tauthor. innerHTML = "author ";
Tprice. innerHTML = "price ";
TCount. innerHTML = "inventory ";
Tpublisher. innerHTML = "publisher ";
Tname. style. borderBottom = "1px solid ";
Tauthor. style. borderBottom = "1px solid ";
Tprice. style. borderBottom = "1px solid ";
TCount. style. borderBottom = "1px solid ";
Tpublisher. style. borderBottom = "1px solid ";

Tname. style. width = "20% ";
Tauthor. style. width = "20% ";
Tprice. style. width = "20% ";
TCount. style. width = "20% ";
Tpublisher. style. width = "20% ";
TrHead. appendChild (tname );
TrHead. appendChild (tauthor );
TrHead. appendChild (tprice );
TrHead. appendChild (tCount );
TrHead. appendChild (tpublisher );

Tbody. appendChild (trHead );



For (var c = 0; c <root. getElementsByTagName ("book"). length; c ++)
{
Var roots = root. getElementsByTagName ("book") [c];
Var id = roots. getAttribute ("id ");
Var name = roots. getElementsByTagName ("name") [0]. childNodes [0]. nodeValue;
Var author = roots. getElementsByTagName ("author") [0]. childNodes [0]. nodeValue;
Var price = roots. getElementsByTagName ("price") [0]. childNodes [0]. nodeValue;
Var count = roots. getElementsByTagName ("count") [0]. childNodes [0]. nodeValue;
Var publisher = roots. getElementsByTagName ("publisher") [0]. childNodes [0]. nodeValue;
Var tr = document. createElement ("tr ");

With (tr. style)
{
BackgroundColor = "Transparent ";
}

Var tdName = document. createElement ("td ");
Var tdAuthor = document. createElement ("td ");
Var tdPrice = document. createElement ("td ");
Var tdCount = document. createElement ("td ");
Var tdPublisher = document. createElement ("td ");


TdName. innerHTML = name;
TdAuthor. innerHTML = author;
TdPrice. innerHTML = price;
TdCount. innerHTML = count;
TdPublisher. innerHTML = publisher;


TdName. id = "tdName" + c;
TdAuthor. id = "tdAuthor" + c;
TdPrice. id = "tdPrice" + c;
TdCount. id = "tdCount" + c;
TdPublisher. id = "tdPublisher" + c;
Tr. appendChild (tdName );
Tr. appendChild (tdAuthor );
Tr. appendChild (tdPrice );
Tr. appendChild (tdCount );
Tr. appendChild (tdPublisher );
Tbody. appendChild (tr );

TdName. onmouseover = function (){
Document. body. style. cursor = "pointer ";
Document. getElementById (this. id). style. backgroundColor = "darkred ";
}
TdName. onmouseout = function (){
Document. body. style. cursor = "";
Document. getElementById (this. id). style. backgroundColor = "";
}
TdAuthor. onmouseover = function (){
Document. body. style. cursor = "pointer ";
Document. getElementById (this. id). style. backgroundColor = "darkred ";
}
TdAuthor. onmouseout = function (){
Document. body. style. cursor = "";
Document. getElementById (this. id). style. backgroundColor = "";
}
TdPrice. onmouseover = function (){
Document. body. style. cursor = "pointer ";
Document. getElementById (this. id). style. backgroundColor = "darkred ";
}
TdPrice. onmouseout = function (){
Document. body. style. cursor = "";
Document. getElementById (this. id). style. backgroundColor = "";
}
TdCount. onmouseover = function (){
Document. body. style. cursor = "pointer ";
Document. getElementById (this. id). style. backgroundColor = "darkred ";
}
TdCount. onmouseout = function (){
Document. body. style. cursor = "";
Document. getElementById (this. id). style. backgroundColor = "";
}
TdPublisher. onmouseover = function (){
Document. body. style. cursor = "pointer ";
Document. getElementById (this. id). style. backgroundColor = "darkred ";
}
TdPublisher. onmouseout = function (){
Document. body. style. cursor = "";
Document. getElementById (this. id). style. backgroundColor = "";
}
}

Table. appendChild (tbody );
P. appendChild (table );

Document. body. appendChild (p );


}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.