Tạo và lưu tệp xml trong javascript

Đôi khi, bạn có thể cần phân tích cú pháp nội dung XML và chuyển đổi nó thành cây DOM hoặc ngược lại, tuần tự hóa cây DOM hiện có thành XML. Trong bài viết này, chúng ta sẽ xem xét các đối tượng do nền tảng web cung cấp để thực hiện các tác vụ thông thường là tuần tự hóa và phân tích cú pháp XML một cách dễ dàng

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
5

Tuần tự hóa các cây DOM, chuyển đổi chúng thành các chuỗi chứa XML

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
6

Xây dựng một cây DOM bằng cách phân tích cú pháp một chuỗi chứa XML, trả về một giá trị

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
7 hoặc
const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8 tùy theo dữ liệu đầu vào

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
9

Tải nội dung từ một URL;

Xpath

Công nghệ tạo chuỗi chứa địa chỉ cho các phần cụ thể của tài liệu XML và định vị các nút XML dựa trên các địa chỉ đó

Tạo một tài liệu XML

Sử dụng một trong các phương pháp sau để tạo tài liệu XML (là một ví dụ của

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8)

Phân tích chuỗi thành cây DOM

Ví dụ này chuyển đổi một đoạn XML trong một chuỗi thành một cây DOM bằng cách sử dụng một

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
6

const xmlStr = 'hey!';
const parser = new DOMParser();
const doc = parser.parseFromString(xmlStr, "application/xml");
// print the name of the root element or error message
const errorNode = doc.querySelector("parsererror");
if (errorNode) {
  console.log("error while parsing");
} else {
  console.log(doc.documentElement.nodeName);
}

Phân tích tài nguyên có thể định địa chỉ URL thành cây DOM

Sử dụng XMLHttpRequest

Đây là mã mẫu đọc và phân tích tệp XML có thể định địa chỉ URL thành cây DOM

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();

Giá trị trong trường

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
4 của đối tượng
const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
3 là một
const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8 được xây dựng bằng cách phân tích cú pháp XML

Nếu tài liệu là HTML, mã được hiển thị ở trên sẽ trả về một

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8. Nếu tài liệu là XML, đối tượng kết quả thực sự là một
const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
7. Hai loại về cơ bản là giống nhau;

Ghi chú. Trên thực tế, cũng có một giao diện

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8, nhưng nó không nhất thiết phải là một loại độc lập. Trong một số trình duyệt, nó là bí danh, trong khi ở những trình duyệt khác, nó là bí danh cho giao diện
const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8

Nối tiếp một tài liệu XML

Cho một

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
8, bạn có thể sắp xếp lại cây DOM của tài liệu thành XML bằng cách sử dụng phương pháp
const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
1

Sử dụng các phương pháp sau để tuần tự hóa nội dung của tài liệu XML mà bạn đã tạo trong phần trước

Tuần tự hóa cây DOM thành chuỗi

Đầu tiên, tạo cây DOM như được mô tả trong Cách tạo cây DOM. Ngoài ra, sử dụng cây DOM thu được từ

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
9

Để tuần tự hóa cây DOM

const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
3 thành văn bản XML, hãy gọi
const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
1

const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);

Tuần tự hóa các tài liệu HTML

Nếu DOM bạn có là một tài liệu HTML, bạn có thể sắp xếp theo thứ tự bằng cách sử dụng

const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
5, nhưng có một tùy chọn đơn giản hơn. chỉ cần sử dụng thuộc tính
const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
6 (nếu bạn chỉ muốn các phần tử con của nút được chỉ định) hoặc thuộc tính
const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
7 nếu bạn muốn nút và tất cả các phần tử con của nó

const xhr = new XMLHttpRequest();

xhr.onload = () => {
  dump(xhr.responseXML.documentElement.nodeName);
}

xhr.onerror = () => {
  dump("Error while getting XML.");
}

xhr.open("GET", "example.xml");
xhr.responseType = "document";
xhr.send();
2

Kết quả là,

const serializer = new XMLSerializer();
const xmlStr = serializer.serializeToString(doc);
8 là một chuỗi chứa HTML của nội dung tài liệu;

Làm cách nào để tạo Tệp XML trong JS?

Tạo đối tượng XML DOM mới . var xmlDoc = document. triển khai. createDocument(null, "books");

Làm cách nào để thêm dữ liệu vào XML bằng JavaScript?

Thêm văn bản vào nút văn bản - insertData() . Phương thức insertData() có hai tham số. offset - Vị trí bắt đầu chèn ký tự (bắt đầu từ 0) chuỗi - Chuỗi cần chèn.

Có thể sử dụng JavaScript với XML không?

Với XML, dữ liệu có thể được lưu trữ trong các tệp XML riêng biệt. Với một vài dòng mã JavaScript, bạn có thể đọc tệp XML và cập nhật nội dung dữ liệu của bất kỳ trang HTML nào .

XML trong JavaScript là gì?

XML là viết tắt của eXtensible Markup Language . XML là một ngôn ngữ đánh dấu giống như HTML. XML được thiết kế để lưu trữ và vận chuyển dữ liệu. XML được thiết kế để tự mô tả.