Cách tìm kiếm dữ liệu trong file XML bằng 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ỉ đó

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]

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];
}

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

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 thức
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

Đầ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];

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 để truy cập dữ liệu XML trong JavaScript?

Với DOM, bạn có thể truy cập mọi nút trong tài liệu XML. .
Bằng cách sử dụng phương thức getElementsByTagName[]
Bằng cách lặp qua [duyệt qua] cây nút
Bằng cách điều hướng cây nút, sử dụng các mối quan hệ nút

Làm cách nào để lấy dữ liệu cụ thể từ tệp XML?

Bạn có thể sử dụng hoạt động Deserialize XML để trích xuất dữ liệu từ XML .

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 .

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

Phương thức insertData[] chèn dữ liệu vào nút văn bản hiện có . 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.

Chủ Đề