gpt4 book ai didi

javascript - 读取 xml 文件,修改值/添加元素/属性并保存 xml 如何?

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:30 24 4
gpt4 key购买 nike

我想使用 javascript 从磁盘读取 xml 文件,修改值/添加元素/属性并将 xml 保存回磁盘。

有人知道我可以在这里找到适用于 IE 和 Firefox 的示例吗?我已经找到要阅读的示例,现在改变值才是问题所在。

谢谢

最佳答案

假设您正在尝试从浏览器而不是 node.js 读取和写入磁盘,第一步是使用 file 类型的 input 标签来访问文件系统。

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="file" id="input" accept="text/xml">
<script src="script.js"></script>
</body>

一旦选择了文件,我们就想从元素中提取 blob。执行此操作的好时机是在更改事件期间。

const input = document.querySelector('#input');

input.addEventListener('change', () => {
const file = input.files.item(0);
});

将 blob 解析为元素树的方法不止一种。这里我利用了浏览器解析 HTTP 请求中的 xml 文档这一事实。

function blobToDocument(blob, callback) {
const url = URL.createObjectURL(blob);
const request = new XMLHttpRequest();
request.open('Get', url);
request.responseType = 'document';
request.addEventListener('load', () => {
callback(request.response);
});
request.send();
}

解析 blob 后,我们可以像操作 DOM 树一样操作它。

function editDocument(document) {
const element = document.createElement('editor');
element.textContent = 'JavaScript';
document.firstChild.appendChild(element);
return document;
}

为了将文件保存到磁盘,我们需要反转解析过程,将元素树转换回字符串。

function documentToString(document) {
const serializer = new XMLSerializer();
return serializer.serializeToString(document);
}

唯一剩下的就是将文件发送回磁盘。为实现这一点,我们可以在包含我们修改过的文件的链接上触发点击事件。

function download(string, mime) {
const blob = new Blob([string], { type: mime });
const a = document.createElement('a');
const url = URL.createObjectURL(blob);
a.href = url;
a.download = 'document.xml';
a.click();
}

完整代码如下

const input = document.querySelector('#input');

input.addEventListener('change', () => {
const file = input.files.item(0);
blobToDocument(file, (xmlDocument) => {
editDocument(xmlDocument);
download(documentToString(xmlDocument), "text/xml");
});
});

function blobToDocument(blob, callback) {
const url = URL.createObjectURL(blob);
const request = new XMLHttpRequest();
request.open('Get', url);
request.responseType = 'document';
request.addEventListener('load', () => {
callback(request.response);
});
request.send();
}

function editDocument(document) {
const element = document.createElement('editor');
element.textContent = 'JavaScript';
document.firstChild.appendChild(element);
return document;
}

function documentToString(document) {
const serializer = new XMLSerializer();
return serializer.serializeToString(document);
}

function download(string, mime) {
const blob = new Blob([string], { type: mime });
const a = document.createElement('a');
const url = URL.createObjectURL(blob);
a.href = url;
a.download = 'document.xml';
a.click();
}
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="file" id="input" accept="text/xml">
<script src="script.js"></script>
</body>

关于javascript - 读取 xml 文件,修改值/添加元素/属性并保存 xml 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2194810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com