如何使用 JavaScript 将 XML 文档中的节点解析为 HTML 页面?

发布于 2024-12-01 10:48:42 字数 2840 浏览 0 评论 0原文

我试图从 XML 提要中列出填字游戏的横向和纵向线索,但无法弄清楚如何深入到节点。这是 XML 的一个示例:

<across>
<a1 a="BLOC"
    c="Group of like-minded voters"
    n="1"
    cn="1" />
<a2 a="BATOR"
    c="Ulan ___, Mongolia"
    n="6"
    cn="5" />
<a3 a="OMEN"
    c="Black cat, supposedly"
    n="12"
    cn="10" />

...

<down>
<d1 a="BLIPS"
    c="Spots on a radar screen"
    n="1"
    cn="1" />
<d2 a="LIMIT"
    c="Word at the express checkout aisle"
    n="2"
    cn="2" />
<d3 a="OMANI"
    c="Man from Muscat"
    n="3"
    cn="3" />
<d4 a="CACKLE"
    c="Laugh like the Wicked Witch"
    n="4"
    cn="4" />

我在 javascript 中使用了以下方法:

document.getElementById('across_clues').innerText = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;

document.getElementById('down_clues').innerText = xmlhttp.responseXML.getElementsByTagName('down')[0].childNodes;

我在 HTML 中创建了两个 div(“#across_clues”和“#down_clues”),但这就是我的 HTML 页面上呈现的内容:

ACROSS

[object NodeList]

DOWN

[object NodeList]

我确信这对我来说是一个相当简单的错误,因为我对使用 javascript 解析 XML 很陌生,但是我错过了什么?我是否需要传递另一个调用来实际列出 XML 数据?请指教。

谢谢, 卡洛斯


更新的问题

好的,这就是我在第二次尝试解决这个问题时遇到的问题:

我想出了如何从各个节点提取数据......一次一个,但我可以'不知道如何一次列出所有节点数组。我不想列出 、 等中的每个值...只有“cn”和“c”作为每个子节点的对(与 相同)。

这是我修改后的代码:

var across = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;
var down = xmlhttp.responseXML.getElementsByTagName('down')[0].childNodes;

var a1Node = xmlhttp.responseXML.getElementsByTagName('a1')[0];
var a2Node = xmlhttp.responseXML.getElementsByTagName('a2')[0];

var d1Node = xmlhttp.responseXML.getElementsByTagName('d1')[0];
    var d1Node = xmlhttp.responseXML.getElementsByTagName('d2')[0];

    document.getElementById('across_clues').innerText = a1Node.getAttribute('cn');
document.getElementById('across_clues').innerText = a1Node.getAttribute('c');

document.getElementById('down_clues').innerText = d1Node.getAttribute('cn');
    document.getElementById('down_clues').innerText = d1Node.getAttribute('c');

结果:

只有一个属性呈现在 #accross_clues 中,而一个属性呈现在 #down_clues 中。每个 div 只显示列表中最后一个 .getAttribute() 调用...无论我列出了多少个。我也尝试了下面的功能,但仍然没有成功:

    function getAcross() {
    if (across) {
    var acrossNodes = across.childNodes;
    if (acrossNodes) {
    for (var i = 0; i < acrossNodes.length; i++) {
    var cnAttr = acrossNodes[i].getAttribute('cn');
    var cAttr = acrossNodes[i].getAttribute('c');
    document.getElementById('across_clues').innerText = cnAttr + '.' + cAttr;
      }
    }
  }
}

任何人都可以帮我解决这个问题吗?当谈到 XML 和 childNodes 时,我是一个严重的新手。非常感谢您的反馈。

谢谢, 卡洛斯

I'm trying to list the Across and Down clues of a crossword puzzle from an XML feed, but can't figure out how to drill into the nodes. Here's a piece sample of the XML:

<across>
<a1 a="BLOC"
    c="Group of like-minded voters"
    n="1"
    cn="1" />
<a2 a="BATOR"
    c="Ulan ___, Mongolia"
    n="6"
    cn="5" />
<a3 a="OMEN"
    c="Black cat, supposedly"
    n="12"
    cn="10" />

...

<down>
<d1 a="BLIPS"
    c="Spots on a radar screen"
    n="1"
    cn="1" />
<d2 a="LIMIT"
    c="Word at the express checkout aisle"
    n="2"
    cn="2" />
<d3 a="OMANI"
    c="Man from Muscat"
    n="3"
    cn="3" />
<d4 a="CACKLE"
    c="Laugh like the Wicked Witch"
    n="4"
    cn="4" />

I used the following method in my javascript:

document.getElementById('across_clues').innerText = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;

document.getElementById('down_clues').innerText = xmlhttp.responseXML.getElementsByTagName('down')[0].childNodes;

I made two divs in my HTML ("#across_clues" and "#down_clues"), but this is what is rendered on my HTML page:

ACROSS

[object NodeList]

DOWN

[object NodeList]

I'm sure this is a rather simple error on my part, as I am very new to parsing XML with javascript, but what am I missing? Is there another call that I need to pass in to actually list the XML data? Please advise.

Thanks,
Carlos


UPDATED QUESTION

OK, so here's what I ran into in my second attempt to solve this problem:

I figured out how to pull in data from individual nodes...one at a time, but I can't figure out how to list an array of nodes all at once. I don't want to list every value in , , etc...only 'cn' and 'c' as a pair for each childNode of (same thing with ).

Here's my revised code:

var across = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;
var down = xmlhttp.responseXML.getElementsByTagName('down')[0].childNodes;

var a1Node = xmlhttp.responseXML.getElementsByTagName('a1')[0];
var a2Node = xmlhttp.responseXML.getElementsByTagName('a2')[0];

var d1Node = xmlhttp.responseXML.getElementsByTagName('d1')[0];
    var d1Node = xmlhttp.responseXML.getElementsByTagName('d2')[0];

    document.getElementById('across_clues').innerText = a1Node.getAttribute('cn');
document.getElementById('across_clues').innerText = a1Node.getAttribute('c');

document.getElementById('down_clues').innerText = d1Node.getAttribute('cn');
    document.getElementById('down_clues').innerText = d1Node.getAttribute('c');

RESULT:

Only one attribute renders into #accross_clues and one attribute in #down_clues. Each div only displays the last .getAttribute() call on the list...no matter how many I list. I also tried the function below, but still no success:

    function getAcross() {
    if (across) {
    var acrossNodes = across.childNodes;
    if (acrossNodes) {
    for (var i = 0; i < acrossNodes.length; i++) {
    var cnAttr = acrossNodes[i].getAttribute('cn');
    var cAttr = acrossNodes[i].getAttribute('c');
    document.getElementById('across_clues').innerText = cnAttr + '.' + cAttr;
      }
    }
  }
}

Can anyone help me out with this? I am a serious novice when it comes to XML and childNodes. Your feedback is much appreciated.

Thanks,
Carlos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-12-08 10:48:42

在这里,您将其称为 childNodes,它本身是一个对象,您可以使用索引访问 childNodes,例如 childNodes[0]、childNodes[1],......
希望这对您有帮助

Here you are mentioning it as childNodes, this itself is an object and u have access the childNodes with the indexes, like childNodes[0], childNodes[1],..........
Hope this helps you

灯下孤影 2024-12-08 10:48:42

在您的代码中,您尝试将 XML DOM 节点集合(节点集合)分配给innerHTML 属性(字符串类型)。另外,标签 A1、A2 ... 未在 html 中定义:您期望得到什么?

在我看来,你可以从下一个开始(不使用外部库,仅使用 javascript):(

  • 不好的方式)使用 javascript 你可以从
    xmlhttp.responseXML.documentElement 并循环遍历所有 XML DOM
    树元素、属性、值并从中创建内容 HTML DOM
    节点树。您可以使用appendChild()追加这个根HTML DOM
    到您的文档 HTML DOM。
  • (正确的方法)第二种方法更合适。您需要 XSL 文档来使用 XSLTProcessor 对象的 importStylesheet()transformToFragment() 方法将 XML 文档转换为片段。您可以使用appendChild() 方法将此片段轻松附加到文档HTML DOM。 -

对于 IE8 及更低版本(对于未测试的 IE9),上述“正确方法”不起作用,因为未实现 XSLTProcessor。在 IE 中,您必须使用 transformNode(stylesheet)。这将创建 HTML 字符串,您可以将其分配给文档 HTML DOM 节点的innerHTML 属性。

更新: XSLTProcessor 无法在某些移动浏览器中实现。

In your code you try assing XML DOM nodes collection (collection of nodes) to innerHTML property (string type). Additionaly, tags A1, A2 ... is not defined in html: what do you expect to get?

In my opinion you can do one from next (without using extrenal libs, only with javascript):

  • (bad way) With using javascript you can go starting from
    xmlhttp.responseXML.documentElement and looping through all XML DOM
    tree elements, attributes, values and create from it content HTML DOM
    node tree. This root HTML DOM you can append with using appendChild()
    to your document HTML DOM.
  • (true way) Second way is more proper. You need XSL document to transform your XML document to fragment with using importStylesheet() and transformToFragment() methods of XSLTProcessor object. This fragment you can easily append to your document HTML DOM with appendChild() method. -

For IE8 and less (for IE9 not tested) this above "true way" is not works because XSLTProcessor is not implemented. In IE you must use transformNode(stylesheet). This create HTML string and you can assing it to innerHTML property of your document HTML DOM node.

Update: XSLTProcessor can be not implemented in some mobile browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文