Đô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[];
5Tuầ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[];
6Xâ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àoconst 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[];
9Tải nội dung từ một URL;
XpathCô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[];
6const 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 XMLNế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[];
8Cho 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];
1Sử 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];
1const 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[];
2Kế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;