Javascript read J write XML file activeobject (used by msxml2.domdocument object)

Source: Internet
Author: User
Tags cdata
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 );
}
}

// Modify the node and use XPath to locate the node
VaR B = Doc. selectsinglenode ("// ttyp/sex ");
B. Text = "female ";

// Alert (Doc. XML );

// 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>

//////////////////////////////////////// ///////////////////
First, the content of the XML file (tree. XML) is as follows:
<? XML version = "1.0" encoding = "gb2312"?>
<Treeview>
<Tree id = "p1">
<Text> Shandong </text>
<Target> _ blank </Target>
<Title> province </title>
<Link> </link>
<Tree id = "p1-1">
<Text> Weihai </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
</Tree>
<Tree id = "p1-2">
<Text> Yantai City </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
<Node id = "p1-2-1">
<Text> Changyi village </text>
<Target> _ blank </Target>
<Title> Villages and Towns </title>
<Link>

Http://www.baidu.com/</Link>
</Node>
</Tree>
<Node id = "p1-3">
<Text> fuzhen </text>
<Target> _ blank </Target>
<Title> Villages and Towns </title>
<Link>Http://www.baidu.com/</Link>
</Node>
</Tree>

<Tree id = "p2">
<Text> Hebei </text>
<Target> _ blank </Target>
<Title> province </title>
<Link> </link>
<Tree id = "p2-1">
<Text> botou city </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
<Node id = "p2-1-1">
<Text> suburban river </text>
<Target> _ blank </Target>
<Title> Villages and Towns </title>
<Link>Http://www.baidu.com/</Link>
</Node>
</Tree>
<Tree id = "p2-2">
<Text> Shijiazhuang </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
</Tree>
</Tree>

<Tree id = "P3">
<Text> Zhejiang </text>
<Target> _ blank </Target>
<Title> province </title>
<Link> </link>
<Tree id = "p3-1">
<Text> Hangzhou </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
<Node id = "p3-1-1">
<Text> A town </text>
<Target> _ blank </Target>
<Title> Villages and Towns </title>
<Link>Http://www.baidu.com/</Link>
</Node>
</Tree>
<Tree id = "p3-2">
<Text> Wenzhou </text>
<Target> _ blank </Target>
<Title> city </title>
<Link> </link>
<Node id = "p3-2-1">
<Text> A town </text>
<Target> _ blank </Target>
<Title> Villages and Towns </title>
<Link>Http://www.baidu.com/</Link>
</Node>
</Tree>
</Tree>
</Treeview>
//////////////////////////////////////// //////////////
Then: JavaScript function implementation: (File Name: tree.htm)
<Script language = "JavaScript">
VaR html = "";
VaR Space = "";
VaR blank = "";
Function getsubject ()
{
VaR xmldoc;

If (window. activexobject)
{
// Get the object of the XML file for the operation
Xmldoc = new activexobject ('Microsoft. xmldom ');
Xmldoc. async = false;
Xmldoc. Load ("tree. xml ");
If (xmldoc = NULL)
{
Alert ('your browser does not support reading XML files, so this page prohibits your operations. We recommend you use ie5.0 or above to solve this problem! ');
Window. Location. href = '/index. aspx ';
Return;
}
}
// Parse the XML file and determine whether an error occurs.
If (xmldoc. parseerror. errorcode! = 0)
{
Alert (xmldoc. parseerror. Reason );
Return;
}
// Obtain the root contact
VaR nodes = xmldoc.doc umentelement. childnodes;
// Obtain the total number of child contacts under the root contact and cycle
For (VAR I = 0; I <nodes. length; I ++)
{
// If the contact name is Tree
If (nodes (I). nodename = "Tree ")
{
Readtree (nodes (I ));
}
// If the contact name is Node
Else if (nodes (I). nodename = "Node ")
{
Readnode (nodes (I ));
}
}
// Delete an object
Delete (xmldoc );
// Display HTML
Window. Show. innerhtml = HTML;
Return;
}
// Read Tree node
Function readtree (CI)
{
VaR nodes = CI. childnodes;
VaR menuhtml = space;
Menuhtml + = blank;
// Obtain the hyperlink
Menuhtml + = "<a href = '";
// If the connection attribute of the node is not empty, connect
If (CI. selectnodes ("Link") (0). Text! = "")
{
Menuhtml + = CI. selectnodes ("Link") (0). text;
}
// Otherwise, the link is empty.
Else
{
Menuhtml + = "#";
}
// Target
If (CI. selectnodes ("target") (0). Text! = "")
{
Menuhtml + = "target = '" + CI. selectnodes ("target") (0). text;
Menuhtml + = "'";
}
// Click the menu event and call the divshow (VID) function.
Menuhtml + = "onclick = javascript: divshow ('" + CI. getattribute ("ID") + "');";
// Obtain the node title
Menuhtml + = "Title = '";
Menuhtml + = CI. selectnodes ("title") (0). text;
// End
Menuhtml + = "'> ";
// Obtain the node body
Menuhtml + = CI. selectnodes ("text") (0). text;
Menuhtml + = "</a> <br>/N ";
// Add the menuhtml settings to the HTML string
HTML + = menuhtml;
// Obtain the attribute value of the node <Span
HTML + = "<Div id = '" + CI. getattribute ("ID") + "'style = 'display: none'>/N ";
For (VAR I = 0; I <nodes. length; I ++)
{
VaR tempimg = "";
Tempimg + = blank;
If (nodes (I). nodename = "Tree ")
{
Space + = tempimg;
Readtree (nodes (I ));
Space = "";
}
Else if (nodes (I). nodename = "Node ")
{
Space + = tempimg;
Readnode (nodes (I ));
}
}
HTML + = "</div>/N ";
Return;
}
// Read Node
Function readnode (CI)
{
VaR nodehtml = space;
Nodehtml + = blank;
// Set the hyperlink
Nodehtml + = "<a href = '";
// Obtain the connection address
Nodehtml + = CI. selectnodes ("Link") (0). text;
// Target
If (CI. selectnodes ("target") (0). Text! = "")
Nodehtml + = "'target = '" + CI. selectnodes ("target") (0). text;
// Obtain the node title
Nodehtml + = "'title = '";
Nodehtml + = CI. selectnodes ("title") (0). text;
// End
Nodehtml + = "'> ";
// Obtain the node body
Nodehtml + = CI. selectnodes ("text") (0). text;
Nodehtml + = "</a> <br>/N ";
HTML + = nodehtml;
// Html + = "<Div id = '" + CI. getattribute ("ID") + "'> ";
Space = "";
Return;
}
// Display or hide the operation object
Function divshow (VID)
{
If (document. All [vid]. style. Display = "NONE ")
{
Document. All [vid]. style. Display = "Block ";
}
Else
{
Document. All [vid]. style. Display = "NONE ";
}
Return;
}
</SCRIPT>

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> js_xml </title>

<Style type = "text/CSS">
<! --
Body
{
Margin-left: 0px;
Margin-top: 0px;
Margin-Right: 0px;
Margin-bottom: 0px;
Font-size: 9pt;
}
A
{
Text-Decoration: none;
Font-family: "";
Font-size: 9pt;
Color: #000000;
}
-->
</Style>

</Head>
<Body bgcolor = "# eeeeee" leftmargin = "0" topmargin = "0">
<Div id = show> </div>
</Body>
<SCRIPT>
Getsubject ()
</SCRIPT>
</Html>
//////////////////////////////////////// //////////////////

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.