manipulating XML with JavaScript

Source: Internet
Author: User
Tags add format cdata comments modify string version xpath
Javascript|xml

<script language= "JavaScript" >
<!--
var doc = new ActiveXObject ("Msxml2.domdocument"); Ie5.5+,createobject ("Microsoft.XMLDOM")


Loading documents
Doc.load ("B.xml");

To create a file header
var p = doc.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' gb2312 '");

Add File Headers
Doc.appendchild (P);

To get the root contact when loading directly
var root = doc.documentelement;

Create root contacts in two ways
var root = doc.createelement ("Students");
var root = Doc.createnode (1, "Students", "");

Creating Child contacts
var n = doc.createnode (1, "Ttyp", "");

Specify Child Contact text
N.text = "This is a test";

Create Sun Contact
var o = doc.createelement ("Sex");
O.text = "male"; Specify its text

Creating properties
var r = doc.createattribute ("id");
r.value= "Test";

Add properties
N.setattributenode (R);

Create a second property
var r1 = Doc.createattribute ("class");
R1.value= "TT";

Add properties
N.setattributenode (R1);

Delete Second Property
N.removeattribute ("class");

Add Sun Contact
N.appendchild (o);

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

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

Add Child contacts
Root.appendchild (n);

Copy Contacts
var m = N.clonenode (true);

Root.appendchild (m);

Delete a contact
Root.removechild (root.childnodes (0));

Create a data segment
var c = doc.createcdatasection ("This is a CDATA");
C.text = "Hi,cdata";
Adding data segments
Root.appendchild (c);

Add Root Contact
Doc.appendchild (root);

Find Contacts
var a = Doc.getelementsbytagname ("Ttyp");
var a = Doc.selectnodes ("//ttyp");

Show the properties of the modified 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);
}
}

modifying nodes, using XPath to locate nodes
var B = Doc.selectsinglenode ("//ttyp/sex");
B.text = "female";

alert (doc.xml);

XML save (required on server, client with FSO)
Doc.save ();

View the root contact XML
if (n)
{
alert (n.ownerdocument.xml);
}

-->
</script>
Posted on 2004-11-30 15:55 Ttyp Reading (2178) Comments (13) Edit favorites to 365Key categories: scripts

Comments
# Re: Manipulating XML 2005-04-08 14:01 Feig with JavaScript
Thanks to the landlord, I used to do C/s, the JavaScript things know too little.


# Re: manipulating XML 2005-07-05 12:16 Koy with JavaScript
I used to do C/s, and now I have to learn JavaScript.


# Help 2005-08-29 11:30 Hawk
I am using JS to load the XML fragment (from the database read a section of the XML rules of the string) read the node, add no problem, but finally did not know how to save the modified XML fragment into the database! Help!!


# Re: manipulating XML 2005-08-29 12:40 Ttyp with JavaScript
Doc.save (Yourpath);

You can see a lot of XML operations in code collection


# Re: manipulating XML 2005-08-29 13:19 Hawk with JavaScript
I am saving a fragment of XML, not an XML file, is to put the saved XML fragment as a string into the database, code collection does not have to save the operation of the XML fragment Ah!


# Re: manipulating XML 2005-08-29 13:26 Ttyp with JavaScript
What is the fragment? All the content under a contact, Node.xml is its content.


# Re: manipulating XML 2005-08-29 17:25 Hawk with JavaScript
It is
<?xml version= "1.0" encoding= "gb2312" ><xml><group id= "My Friends" ><friend>1001</friend> <friend>1002</friend><friend>1003</friend></group><group Id= "My colleagues" >< Friend>1005</friend></group></xml>

This is placed in the database to get a field, with the Loadxml () method can be used with loading XML files, including lookup, modify, add nodes and display, but find no changes to save and then put back to the database method


# Re: manipulating XML 2005-08-29 19:05 Ttyp with JavaScript
Are you going to save it directly in the database via JS? That's not going to work. Dom does not operate the database function, can only save as a file, you use Doc.xml to get content, and then how do you read how to write back?


# Re: manipulating XML 2005-09-17 01:55 Xiao3 with JavaScript
Please ask the landlord, I use the ASP page to invoke the following code: (JavaScript)
<script language=javascript>
var objxml = new ActiveXObject ("MSXML2. DOMDocument ");
Objxml.async = false;
Objxml.load ("Data1.xml");
Alert ("D:" +objxml.xml);
</script>
If the first sentence of the loaded Data1.xml file is: <?xml version= "1.0" encoding= "Utf-8"?>
Alert is empty.
And if the first sentence is modified into <?xml version= "1.0" encoding= "gb2312"?>
The contents of alert are data1.xml.
How do you load XML files correctly in JavaScript while keeping <?xml version= "1.0" encoding= "Utf-8"?>? Now it's time to change the code to gb2312.
Thank you.


# Re: manipulating XML 2005-09-17 08:06 Ttyp with JavaScript
The encoding format of the XML file is modified to UTF-8 try (open with Notepad, save as, there is a selected encoding format below)


# Re: manipulating XML 2005-09-18 02:07 Xiao3 with JavaScript
Thank you Landlord!
This can be load in, in this way, I do not know whether there are other methods

Thank you.


# Re: manipulating XML 2005-10-19 20:26 gphy with JavaScript
The landlord does not seem to give a way to modify the node

Solving


# Re: manipulating XML 2005-10-19 22:55 Ttyp with JavaScript
Plus the modified part.
modifying nodes, using XPath to locate nodes
var B = Doc.selectsinglenode ("//ttyp/sex");
B.text = "female";



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.