admin管理员组文章数量:1023242
I'd like to add new elements to a XML file from a user input. I've tried a lot possible ways but none of them worked for me :(
How can I add a new node/element/item to a existing local XML file using JavaScript or jQuery?
This is my current state:
function addElement() {
var xml;
if (window.activexobject) {
xml = new activexobject("microsoft.xmlhttp");
} else {
xml = new xmlhttprequest();
}
xml.onreadystatechange = function () {
if (xml.readystate == 4 && xml.status == 200) {
var resp = xml.responsexml;
var user = xml.responsexml.createelement("User");
var name = xml.responsexml.createelement("name");
name.appendchild(xml.responsexml.createtextnode("sof_user"));
var admin = xml.responsexml.createelement("admin");
admin.appendchild(xml.responsexml.createtextnode("false"));
user.appendchild(name);
user.appendchild(admin);
xml.responsexml.documentelement.appendchild(user);
}
}
xml.open("get", "users.xml", true);
xml.send(null);
}
the XML should look like this:
<Users>
<User>
<name>testuser</name>
<admin>true</admin>
</User>
<User>
<name>sof_user</name>
<admin>false</admin>
</User>
....
</Users>
I'd like to add new elements to a XML file from a user input. I've tried a lot possible ways but none of them worked for me :(
How can I add a new node/element/item to a existing local XML file using JavaScript or jQuery?
This is my current state:
function addElement() {
var xml;
if (window.activexobject) {
xml = new activexobject("microsoft.xmlhttp");
} else {
xml = new xmlhttprequest();
}
xml.onreadystatechange = function () {
if (xml.readystate == 4 && xml.status == 200) {
var resp = xml.responsexml;
var user = xml.responsexml.createelement("User");
var name = xml.responsexml.createelement("name");
name.appendchild(xml.responsexml.createtextnode("sof_user"));
var admin = xml.responsexml.createelement("admin");
admin.appendchild(xml.responsexml.createtextnode("false"));
user.appendchild(name);
user.appendchild(admin);
xml.responsexml.documentelement.appendchild(user);
}
}
xml.open("get", "users.xml", true);
xml.send(null);
}
the XML should look like this:
<Users>
<User>
<name>testuser</name>
<admin>true</admin>
</User>
<User>
<name>sof_user</name>
<admin>false</admin>
</User>
....
</Users>
Share
Improve this question
edited Jun 2, 2015 at 15:34
ArmandoS63
asked Jun 2, 2015 at 14:46
ArmandoS63ArmandoS63
7031 gold badge6 silver badges21 bronze badges
2
- Are you asking how to save the file to the local hard drive from a browser? – Dan Field Commented Jun 2, 2015 at 15:23
- @DanField Yes. And how to add a new token – ArmandoS63 Commented Jun 2, 2015 at 15:29
2 Answers
Reset to default 3You should be able to build up your XML something like this:
usersNode = document.createElement("Users");
userNode = document.createElement("User");
nameNode = document.createElement("name");
nameNode.innerText = "testuser";
adminNode = document.createElement("admin");
adminNode.innerText = "true";
userNode.appendChild(nameNode);
userNode.appendChild(adminNode);
usersNode.appendChild(userNode);
userNode = document.createElement("User");
nameNode = document.createElement("name");
nameNode.innerText = "sof_user";
adminNode = document.createElement("admin");
adminNode.innerText = "false";
userNode.appendChild(nameNode);
userNode.appendChild(adminNode);
usersNode.appendChild(userNode);
console.log(usersNode.innerHTML);
I think that should give you what you want.
If you don't have immediate access to the document, you can also get to it from an existing element:
someElement.ownerDocument.createElement('User');
I'd like to add new elements to a XML file from a user input. I've tried a lot possible ways but none of them worked for me :(
How can I add a new node/element/item to a existing local XML file using JavaScript or jQuery?
This is my current state:
function addElement() {
var xml;
if (window.activexobject) {
xml = new activexobject("microsoft.xmlhttp");
} else {
xml = new xmlhttprequest();
}
xml.onreadystatechange = function () {
if (xml.readystate == 4 && xml.status == 200) {
var resp = xml.responsexml;
var user = xml.responsexml.createelement("User");
var name = xml.responsexml.createelement("name");
name.appendchild(xml.responsexml.createtextnode("sof_user"));
var admin = xml.responsexml.createelement("admin");
admin.appendchild(xml.responsexml.createtextnode("false"));
user.appendchild(name);
user.appendchild(admin);
xml.responsexml.documentelement.appendchild(user);
}
}
xml.open("get", "users.xml", true);
xml.send(null);
}
the XML should look like this:
<Users>
<User>
<name>testuser</name>
<admin>true</admin>
</User>
<User>
<name>sof_user</name>
<admin>false</admin>
</User>
....
</Users>
I'd like to add new elements to a XML file from a user input. I've tried a lot possible ways but none of them worked for me :(
How can I add a new node/element/item to a existing local XML file using JavaScript or jQuery?
This is my current state:
function addElement() {
var xml;
if (window.activexobject) {
xml = new activexobject("microsoft.xmlhttp");
} else {
xml = new xmlhttprequest();
}
xml.onreadystatechange = function () {
if (xml.readystate == 4 && xml.status == 200) {
var resp = xml.responsexml;
var user = xml.responsexml.createelement("User");
var name = xml.responsexml.createelement("name");
name.appendchild(xml.responsexml.createtextnode("sof_user"));
var admin = xml.responsexml.createelement("admin");
admin.appendchild(xml.responsexml.createtextnode("false"));
user.appendchild(name);
user.appendchild(admin);
xml.responsexml.documentelement.appendchild(user);
}
}
xml.open("get", "users.xml", true);
xml.send(null);
}
the XML should look like this:
<Users>
<User>
<name>testuser</name>
<admin>true</admin>
</User>
<User>
<name>sof_user</name>
<admin>false</admin>
</User>
....
</Users>
Share
Improve this question
edited Jun 2, 2015 at 15:34
ArmandoS63
asked Jun 2, 2015 at 14:46
ArmandoS63ArmandoS63
7031 gold badge6 silver badges21 bronze badges
2
- Are you asking how to save the file to the local hard drive from a browser? – Dan Field Commented Jun 2, 2015 at 15:23
- @DanField Yes. And how to add a new token – ArmandoS63 Commented Jun 2, 2015 at 15:29
2 Answers
Reset to default 3You should be able to build up your XML something like this:
usersNode = document.createElement("Users");
userNode = document.createElement("User");
nameNode = document.createElement("name");
nameNode.innerText = "testuser";
adminNode = document.createElement("admin");
adminNode.innerText = "true";
userNode.appendChild(nameNode);
userNode.appendChild(adminNode);
usersNode.appendChild(userNode);
userNode = document.createElement("User");
nameNode = document.createElement("name");
nameNode.innerText = "sof_user";
adminNode = document.createElement("admin");
adminNode.innerText = "false";
userNode.appendChild(nameNode);
userNode.appendChild(adminNode);
usersNode.appendChild(userNode);
console.log(usersNode.innerHTML);
I think that should give you what you want.
If you don't have immediate access to the document, you can also get to it from an existing element:
someElement.ownerDocument.createElement('User');
本文标签: javascriptAdd new node to local XML file with JS or jQueryStack Overflow
版权声明:本文标题:javascript - Add new node to local XML file with JS or jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598748a2158330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论