XML operations using JavaScript

Source: Internet
Author: User
< Script Language = " Javascript " >
<! --
VaR Doc =   New Activexobject ( " Msxml2.domdocument " ); // Ie5.5 +, Createobject ("Microsoft. xmldom ")

// document loading
// Doc. load ("B. XML);

// create a file header
var P = Doc. createprocessinginstruction ( " XML " , " Version = '1. 0 'encoding= 'gb2312' " );

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

// Used to obtain the root contact during direct loading.
// VaR root = doc.doc umentelement;

// Two ways to create a root contact
// VaR root = Doc. createelement ("Students ");
VaR Root = Doc. createnode ( 1 , " Students " , "" );

//Create a Child Contact
VaRN=Doc. createnode (1,"Ttyp","");

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

// Create a sun contact
VaR O = Doc. createelement ( " Sex " );
O. Text =   " Male " ; // Specify text

// Create attributes
VaR R = Doc. createattribute ( " ID " );
R. Value = " Test " ;

//Add attribute
N. setattributenode (R );

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

// Add attribute
N. setattributenode (R1 );

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

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

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

// Add Comment
N. appendchild (Doc. createcomment ( " This is a comment \ n " ));

// ADD child contacts
Root. appendchild (N );

// Replication contacts
VaR M = N. clonenode ( True );

Root. appendchild (m );

//Delete contacts
Root. removechild (root. childnodes (0));

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

// Add root contacts
Doc. appendchild (Root );

// Find contacts
VaR A = Doc. getelementsbytagname ( " Ttyp " );
// VaR A = Doc. selectnodes ("// ttyp ");

// Display attributes of change contacts
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 nodes and use XPath to locate nodes
VaRB=Doc. selectsinglenode ("// Ttyp/sex");
B. Text= "Female";

// Alert (Doc. XML );

// Save XML (on the server side and FSO on the client side)
// Doc. Save ();

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

//-->
</Script>

Related Article

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.