Home  >  Article  >  Web Front-end  >  firefo xml reading and writing to implement js code_javascript skills

firefo xml reading and writing to implement js code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:51:301341browse
Read the xml file into a string
Step 1: 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 2 : Convert DOM structure into 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
2. Write the xml string into an xml file
// str is the 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");// Maintained location
foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
serializer.serializeToStream(dom.documentElement, foStream, ""); // rememeber, dom is the DOM tree
foStream.close();
Attachments:
1 Create DOM structure
// Generate document object
var xmldoc = document.implementation.createDocument("","",null );
// Create the header of the xml file
//
var head = xmldoc.createProcessingInstruction("xml","version="1.0" encoding="UTF-8" standalone=" yes"");
xmldoc.appendChild(head);
// Create ROOT node
var nodest = xmldoc.createElement("nodeset");
xmldoc.appendChild(nodest);
//Create child node
var elem1 = doc.createElement("name");
elem1.textContent = "Zhang San";
nodest.appendChild(elem1);
var elem2 = doc .createElement("name");
elem2.textContent = "李思";
nodest.appendChild(elem2);
// The created result is as follows
Zhang SanLi Si
The xmlDOM structure generated in this way will not be automatically indented when it is converted into an xml string through serialization, but it can be parsed through XML objects.
var serializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
// Parse DOMxml structure into xml string
// To successfully parse the XML object, the xml header must be removed
// Remove the xml header
var xmlDeclaration = /^] ?>/;
var str = new XML( serializer.serializeToString(xmldoc). replace(xmlDeclaration, '') ).toXMLString();
Haha, now str is an xml string that can be automatically indented. However, if you want your xml to be successfully encoded, you must add the xml header in front of str. Don't forget it.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn