FIREFO XML Read and write to realize JS code _javascript skill
Source: Internet
Author: User
read an XML file to a string Step One: Convert the XML file into a DOM structure
1
var xmldoc = document.implementation.createDocument ("", "test", null);
Xmldoc.load ("D:\\develop\\bookmarks.xml");
2
var req = new XMLHttpRequest ();
Req.open ("Get", "Chrome://passwdmaker/content/people.xml", false);
Req.send (NULL);
var dom = Req.responsexml;
DOM is the DOM structure object
Step two: Convert the DOM structure into an XML string
var serializer = components.classes["@mozilla. Org/xmlextras/xmlserializer;1"].createinstance ( Components.interfaces.nsIDOMSerializer);
var str = serializer.serializetostring (DOM);
STR is the string of XML content
two writes an XML string as an XML file STR is an XML string
var parser = new Domparser ();
var dom = parser.parsefromstring (str, "text/xml");
var serializer = components.classes["@mozilla. Org/xmlextras/xmlserializer;1"].createinstance ( Components.interfaces.nsIDOMSerializer);
var fostream = components.classes["@mozilla. Org/network/file-output-stream;1"].createinstance ( Components.interfaces.nsIFileOutputStream);
var file = components.classes["@mozilla. Org/file/local;1"].createinstance (Components.interfaces.nsILocalFile);
File.initwithpath ("D:\\develop\\myxmlfile.xml")/position maintained
Fostream.init (file, 0x02 | 0x08 | 0x20, 0664, 0); Write, create, truncate
Serializer.serializetostream (Dom.documentelement, Fostream, ""); Rememeber, Dom is the DOM tree
Fostream.close ();
Attachment:
1 Creating a DOM structure
Generate Document Object
var xmldoc = document.implementation.createDocument ("", "", null);
To create the header of an XML file
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
var head = xmldoc.createprocessinginstruction ("xml", "Version=\" 1.0\ "encoding=\" utf-8\ "standalone=\");
Xmldoc.appendchild (head);
Create root node
var nodest = xmldoc.createelement ("NodeSet");
Xmldoc.appendchild (nodest);
Creating child nodes
var elem1 = doc.createelement ("name");
Elem1.textcontent = "John";
Nodest.appendchild (ELEM1);
var elem2 = doc.createelement ("name");
Elem2.textcontent = "Dick";
Nodest.appendchild (ELEM2);
The results created are as follows
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><nodeset><name> John > Dick </name></nodeset>
The resulting XMLDOM structure is not automatically indented when it is converted to an XML string, but can be parsed with XML objects.
var serializer = components.classes["@mozilla. Org/xmlextras/xmlserializer;1"].createinstance ( Components.interfaces.nsIDOMSerializer);
Parsing domxml structures to XML strings
XML header must be removed for XML object resolution success
Remove XML headers
var XmlDeclaration =/^<\?xml version[^>]+?>/;
var str = new XML (serializer.serializetostring (xmldoc). Replace (XmlDeclaration, ')). Toxmlstring ();
Haha, now str is an XML string that can be automatically indented. But if your XML has to be coded successfully, precede str with an XML header, and don't forget it.
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.