How to use JavaScript to operate XML data

Source: Internet
Author: User

<Script language = "JavaScript">
<! --
VaR Doc = new activexobject ("msxml2.domdocument"); // ie5.5 +, Createobject ("Microsoft. xmldom ")

// Load the document
// Doc. Load ("B. xml ");

// Create a file header
VaR P = Doc. createprocessinginstruction ("XML", "version = '1. 0' encoding = 'gb2312 '");

// Add a File Header
Doc. appendchild (P );

// Obtain the root contact during direct loading.
// Var root = doc.doc umentelement;

// Create a root contact in two ways
// Var root = Doc. createelement ("Students ");
VaR root = Doc. createnode (1, "Students ","");

// Create a child contact
VaR n = Doc. createnode (1, "ttyp ","");

// Specify the stator contact text
// N. Text = "this is a test ";

// Create a sun contact
VaR o = Doc. createelement ("sex ");
O. Text = "male"; // specify the text

// Create attributes
VaR r = Doc. createattribute ("ID ");
R. value = "test ";

// Add attributes
N. setattributenode (R );

// Create the second property
VaR R1 = Doc. createattribute ("class ");
R1.value = "TT ";

// Add attributes
N. setattributenode (R1 );

// Delete the second property
N. removeattribute ("class ");

// Add a sun contact
N. appendchild (O );

// Add text contacts
N. appendchild (Doc. createtextnode ("this is a text node ."));

// Add comments
N. appendchild (Doc. createcomment ("this is a comment/N "));

// Add a Child Contact
Root. appendchild (N );

// Copy contacts
VaR M = n. clonenode (true );

Root. appendchild (m );

// Delete the contact
Root. removechild (root. childnodes (0 ));

// Create a data segment
VaR c = Doc. createcdatasection ("this is a CDATA ");
C. Text = "Hi, CDATA ";
// Add Data Segment
Root. appendchild (C );

// Add the root contact
Doc. appendchild (Root );

// Search for contacts
VaR A = Doc. getelementsbytagname ("ttyp ");
// Var A = Doc. selectnodes ("// ttyp ");

// Display the attributes of the change contact
For (VAR I = 0; I <A. length; I ++)
{
Alert (A [I]. XML );
For (var j = 0; j <A [I]. Attributes. length; j ++)
{
Alert (A [I]. attributes [J]. Name );
}
}

// Save the XML file (the file must be stored on the server and FSO must be used on the client)
// Doc. Save ();

// View the root contact XML
If (N)
{
Alert (N. ownerdocument. XML );
}

// -->
</SCRIPT>
 
 
 
Example 1: Use xmldata island partition .htm
**************************************** ********
<XML id = "users">
<Usergroup>
<User>
<Userid> User 1 </userid>
<Name> name 1 </Name>
</User>
<User>
<Userid> User 2 </userid>
<Name> name 2 </Name>
</User>
<User>
<Userid> User 3 </userid>
<Name> name 3 </Name>
</User>
<User>
<Userid> User 4 </userid>
<Name> name 4 </Name>
</User>
<User>
<Userid> User 5 </userid>
<Name> name 5 </Name>
</User>
<User>
<Userid> User 6 </userid>
<Name> name 7 </Name>
</User>
<User>
<Userid> User 7 </userid>
<Name> name 7 </Name>
</User>
<User>
<Userid> User 8 </userid>
<Name> name 8 </Name>
</User>
<User>
<Userid> User 9 </userid>
<Name> name 9 </Name>
</User>
</Usergroup>
</XML>
<Table id = "datatable" datasrc = "# users" datapagesize = "3" width = "400" border = "1">
<Tr>
<TD> <span dataworks = "userid"> </span> </TD>
<TD> <span dataworks = "name"> </span> </TD>
</Tr>
</Table>
<Button onclick = "document. All. datatable. previouspage ()"> previous page </button>
<Button onclick = "document. All. datatable. nextpage ()"> next page </button>
**************************************** ********

Instance 2: xmldata island region 1.htm
**************************************** ********
<HTML>
<Head> <title> record set in data island in HTML </title> <Body bkcolor = # eeeeee text = blue bgcolor = "#00 FFFF">
<Table align = center width = "100%"> <tr> <TD align = "center">
<H5> <B> <font size = "4" color = "# ff0000"> edit and add XML data Island Records in HTML </font> </B> </H5>
</TD> </tr> </table>
<HR>
Hotel name: <input type = text datasrc = # thexmlisland data== name size = "76"> <br>
Address: <input type = text datasrc = # thexmlisland data== address size = "76"> <br>
Home page: <input type = text datasrc = # thexmlisland data== homepage size = "76"> <br>
Email: <input type = text datasrc = # thexmlisland data== e-mail size = "76"> <br>
Tel: <input type = text datasrc = # thexmlisland data== telephone size = "76"> <br>
Level: <input type = text datasrc = # thexmlisland dataworkflow = Grade size = "76"> <HR>
<Input id = "first" type = button value = "<first record" onclick = "thexmlisland. recordset. movefirst ()">
<Input id = "Prev" type = button value = "<previous record" onclick = "thexmlisland. recordset. moveprevious ()">
<Input id = "Next" type = button value = "next record>" onclick = "thexmlisland. recordset. movenext ()">
<Input id = "last" type = button value = "last record>" onclick = "thexmlisland. recordset. movelast ()"> & nbsp;
<Input id = "add" type = button value = "Add new record" onclick = "thexmlisland. recordset. addnew ()">

<XML id = "thexmlisland">
<Hotellist>
<Hotel>
<Name> Sihai hotel </Name>
<Address> No. 1, haisoul road </address>
<Homepage> www.sihaoapps.com.cn <E-mail> master@sihaohotel.com.cn </e-mail>
<Telephone> (0989) 8888888 </telephone>
<Grade> five-star </grade>
</Hotel>
<Hotel>
<Name> Wuhu hotel </Name>
<Address> No. 99 Dongping road </address>
<Homepage> www.wuhu.com.cn <E-mail> web@wuhu.com.cn </e-mail>
<Telephone> (0979) 1111666 </telephone>
<Grade> four-star </grade>
</Hotel>
<Hotel>
<Name> grand Desert Hotel </Name>
<Address> No. 168 Liuxiang road </address>
<Homepage> www.dashamow..com.cn <E-mail> master@dashamohotel.com.cn </e-mail>
<Telephone> (0989) 87878788 </telephone>
<Grade> five-star </grade>
</Hotel>
<Hotel>
<Name> "eyebrow bird" Hotel </Name>
<Address> No. 2, pianxiang Road, xuehai </address>
<Homepage> www.throstleapps.com.cn <E-mail> chuliuxiang@throstlehotel.com.cn </e-mail>
<Telephone> (099) 9886666 </telephone>
<Grade> five-star </grade>
</Hotel>
</Hotellist>
</XML>
</Body>
</Html>
**************************************** ********
 
 
Instance 3: xml_linked table ticket .htm
**************************************** ********

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
Http://www.w3.org/TR/html4/loose.dtd>
<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Script language = "javascript1.2">
Function choosestate ()
{
VaR source;
VaR sourcename = "Citys. xml ";
VaR source = new activexobject ('Microsoft. xmldom '); // create an MSXML parser instance
Source. async = false;
Source. Load (sourcename); // load the XML document
Root = source.doc umentelement; // sets the document element as the root node element.
Sortfield = root. selectnodes ("// @ name"); // search all nodes whose attributes contain name
For (VAR I = 0; I <sortfield. length; ++ I) // Add the province name to the drop-down list.
{
VaR ooption = Document. createelement ('option ');
Ooption. Text = "" + sortfield [I]. Text + "";
Ooption. value = sortfield [I]. text;
Form1.selstate. Options. Add (ooption );
}
Choosecity ();
}

// Custom function: choosecity
// (Read the name of the target city in the XML data based on the currently selected province name and add it to the selcity drop-down list)
Function choosecity ()
{
X = form1.selstate. selectedindex; // read the current option in the province drop-down list.
Y = form1.selstate. Options [X]. value;
Sortfield = root. selectnodes ("// State [@ name = '" + Y + "']/City"); // search all city nodes under the State node with the property value of name equal to the parameter Y
For (VAR I = form1.selcity. Options. Length-1; I> = 0; -- I) // undo the original list item
{
Form1.selcity. Options. Remove (I)
}
For (VAR I = 0; I <sortfield. length; ++ I) // Add the city name to the drop-down list.
{
VaR ooption = Document. createelement ('option ');
Ooption. Text = "" + sortfield [I]. Text + "";
Ooption. value = sortfield [I]. text;
Form1.selcity. Options. Add (ooption );
}
}
</SCRIPT>
</Head>

<Body onload = "choosestate ()">
<Form action = "" method = "Post" id = "form1" name = "form1">
<Select name = "selstate" id = "selstate" onchange = "choosecity ()">
</SELECT>
<Select name = "selcity" id = "selcity">
</SELECT>
</Form>
</Body>
</Html>

Appendix Citys. xml
<? XML version = "1.0" encoding = "gb2312"?>
<China>
<State ID = "1" name = "Jiangxi">
<City> Jiujiang </city>
<City> Nanchang </city>
<City> Lushan </city>
<City> Jingdezhen </city>
</State>
<State ID = "2" name = "Beijing">
<City> Beijing West </city>
<City> juyongguan </city>
<City> Tsinghua Park </city>
<City> Zhoukoudian </city>
</State>
<State ID = "3" name = "Fujian">
<City> Fuzhou </city>
<City> Xiamen </city>
<City> Zhangzhou </city>
</State>
<State ID = "4" name = "Gansu">
<City> Lanzhou </city>
<City> lomen </city>
<City> Jiayuguan </city>
</State>
<State ID = "5" name = "Guangdong">
<City> Guangzhou </city>
<City> Shenzhen </city>
<City> Dongguan </city>
<City> Shi Pai </city>
</State>
<State ID = "6" name = "Anhui">
<City> Hefei </city>
<City> Huangshan </city>
<City> Kowloon gang </city>
<City> ma'anshan </city>
</State>
</China>

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.