Hướng dẫn display xml in html using javascript - hiển thị xml trong html bằng javascript

6

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Như câu hỏi nói, nó chỉ thoát khỏi bộ nhớ của tôi cách hiển thị XML trong JavaScript, tôi muốn hiển thị một số nguồn XML trong một div trên một trang, nằm bên cạnh kết quả được xử lý của XML trong một div khác.

Không thể nhớ nếu có tương đương với lối thoát của JavaScript để chuyển đổi các thực thể trên máy khách

Lưu ý: Các tệp XML được phục vụ như là từ máy chủ, vì vậy tôi cần một giải pháp phía máy khách: the xml files are served as is from the server, so I need a client side solution

Lưu ý: Vấn đề chính là XML không hiển thị chính xác trong hầu hết các trình duyệt, tất cả các dấu ngoặc và thuộc tính biến mất, để lại cho bạn văn bản không giống XML: the main problem is XML doesn't render correctly in most browsers, all the brackets and attributes disappear, leaving you with text that doesn't look like xml

Hỏi ngày 8 tháng 12 năm 2008 lúc 11:29Dec 8, 2008 at 11:29

Hướng dẫn display xml in html using javascript - hiển thị xml trong html bằng javascript

Robert Gouldrobert GouldRobert Gould

67.5K61 Huy hiệu vàng185 Huy hiệu bạc271 Huy hiệu Đồng61 gold badges185 silver badges271 bronze badges

Tìm nạp XML của bạn bằng AJAX và chỉ cần đặt kết quả vào textarea. Phong cách Textarea bất kỳ cách nào bạn muốn.

Đã trả lời ngày 8 tháng 12 năm 2008 lúc 14:14Dec 8, 2008 at 14:14

1

Nếu bạn muốn trình duyệt hiển thị XML của bạn là XML, thì nó phải có trong tài liệu của riêng nó, như iframe hoặc

function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
0.
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
1 sẽ không làm công việc!

Bên cạnh đó, trong tiêu đề

function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
2 của yêu cầu phục vụ iframe, bạn nên gửi
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
4, vì vậy trình duyệt có thể hiểu rằng nội dung này là XML.

Nhưng, nếu bạn thực sự cần phải nhìn thấy nó trong một div, bạn sẽ phải xây dựng cho mình một chức năng kết xuất XML

function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
7. Bạn có thể sử dụng thẻ
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
8
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
9 cho điều đó, nếu chuỗi XML của bạn đã được định dạng (ngắt dòng và tab). Trong trường hợp đó, bạn sẽ phải thay thế
#targetDiv {
  font-family: fixed;
  white-space: pre;
}
1 thành
#targetDiv {
  font-family: fixed;
  white-space: pre;
}
2 trong chuỗi XML.

Kết luận:

#targetDiv {
  font-family: fixed;
  white-space: pre;
}
4 Không thể hiển thị XML
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
8 trong cùng một tài liệu. Nếu bạn cần nó, bạn chỉ cần giải quyết nó.
#targetDiv {
  font-family: fixed;
  white-space: pre;
}
4 can't render XML and
function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);
8 in the same document. If you need it, you just have to workaround it.

Đã trả lời ngày 8 tháng 12 năm 2008 lúc 11:57Dec 8, 2008 at 11:57

Daniel Silveiradaniel SilveiraDaniel Silveira

39,8k35 huy hiệu vàng98 Huy hiệu bạc120 Huy hiệu đồng35 gold badges98 silver badges120 bronze badges

2

Đây là một chức năng cho bạn:


Đã trả lời ngày 8 tháng 12 năm 2008 lúc 11:58Dec 8, 2008 at 11:58

Andrew G. Johnsonandrew G. JohnsonAndrew G. Johnson

26.2k30 Huy hiệu vàng88 Huy hiệu bạc134 Huy hiệu đồng30 gold badges88 silver badges134 bronze badges

3

Khi bạn không muốn sử dụng toàn bộ khung JavaScript chỉ cho điều này ...

function insertLiteral(literalString, targetElement)
{
    var textNode = document.createTextNode(literalString);
    targetElement.appendChild(textNode)
    return textNode;
}

// test it (for the example, I assume #targetDiv is there but empty)
var xmlString = "";
var targetElement = document.getElementById("targetDiv");
var xmlTextNode = insertLiteral(xmlString, targetElement);

#targetDiv {
  font-family: fixed;
  white-space: pre;
}
7 có thể được tạo kiểu bằng CSS:

#targetDiv {
  font-family: fixed;
  white-space: pre;
}

Đã trả lời ngày 8 tháng 12 năm 2008 lúc 13:08Dec 8, 2008 at 13:08

TomalaktomalakTomalak

326K66 Huy hiệu vàng516 Huy hiệu bạc619 Huy hiệu Đồng66 gold badges516 silver badges619 bronze badges

1

Vấn đề là bạn phải thoát khỏi các nhân vật mà trình phân tích cú pháp HTML sẽ giải thích là đánh dấu:, và trong một số trường hợp "và '

Core JavaScript không cung cấp điều này cho bạn, nhưng nhiều thư viện bổ trợ làm.

Ví dụ: nguyên mẫu có: http://www.prototypejs.org/api/string/escapehtml

Đã trả lời ngày 8 tháng 12 năm 2008 lúc 12:32Dec 8, 2008 at 12:32

Kdgregorykdgregorykdgregory

38K10 Huy hiệu vàng75 Huy hiệu bạc101 Huy hiệu đồng10 gold badges75 silver badges101 bronze badges

Một kỹ thuật sẽ là sử dụng hai phần tử iframe với thuộc tính SRC được đặt thành tệp XML tương ứng có sẵn từ máy chủ (giả sử nó được hiển thị thông qua thư mục ảo):