Hàm này phân tích cú pháp văn bản giữa bắt đầu và kết thúc dưới dạng HTML và trả về một danh sách đại diện cho cây phân tích cú pháp HTML. Nó cố gắng xử lý HTML trong thế giới thực bằng cách đối phó mạnh mẽ với các lỗi cú pháp
Url cơ sở đối số tùy chọn, nếu không phải là ____15, phải là một chuỗi chỉ định URL cơ sở cho các URL tương đối xuất hiện trong các liên kết
Nếu đối số tùy chọn discard-comments không phải là ____15, thì bất kỳ nhận xét cấp cao nhất nào cũng bị loại bỏ. (Đối số này đã lỗi thời và sẽ bị xóa trong các phiên bản Emacs trong tương lai. Để xóa nhận xét, hãy sử dụng chức năng tiện ích 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 trên dữ liệu trước khi bạn gọi chức năng phân tích cú pháp. )
Trong cây phân tích cú pháp, mỗi nút HTML được biểu thị bằng một danh sách trong đó phần tử đầu tiên là ký hiệu đại diện cho tên nút, phần tử thứ hai là danh sách các thuộc tính của nút và các phần tử còn lại là các nút con
Đô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(); 8Tuầ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(); 9Xâ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(); 0 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(); 1 nếu phù hợp dựa trên 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(); 2Tả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ỉ đó
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(); 1)
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(); 9
const xmlStr = '<q id="a"><span id="b">hey!</span></q>'; 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(); 1 đượ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(); 1. 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(); 0. 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(); 1
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(); 1, 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
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(); 2
Để 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;