使用 jQuery 解析数据

发布于 2024-09-28 13:35:28 字数 1417 浏览 2 评论 0原文

<?xml version="1.0" encoding="utf-8"?>
<blamatrixrix>
<name></name>
<columns number="2">
 <column id="title" num="0">Title</column>
 <column id="content" num="1">Content</column>
</columns>
<rows number="7"></rows>
<b>Description</b>
<b>Description text here</b>
<b>Some title 1</b>
<b>Some text blabla for Some title 1</b>
<b>Some title 2</b>
<b>Some text blabla for Some title 2</b>
<b>Some title 3</b>
<b>Some text blabla for Some title 3</b>
<b>Some title 4</b>
<b>Some text blabla for Some title 4</b>
<b>Some title 5</b>
<b>Some text blabla for Some title 5</b>
<b>Some title 6</b>
<b>Some text blabla for Some title 6</b>
</blamatrixrix>

我需要将该数据解析到表中,以便每个其他 content 都将进入 own ,并且每个其他内容都进入 own 。

最终看起来像这样:

<tr><td>Some title 1</td>  <td>Some title 2'</td> <td>Some title 3</td>  <td>Some title 4'</td></tr>
<tr><td>Some text 1</td><td>Some text 2</td><td>Some text 3</td><td>Some text 4</td></tr>

我应该如何开始做呢?谢谢。

编辑:我知道 XML 并不像它应该的那样,但我对此无能为力。我只需要在这样的情况下解析它。

<?xml version="1.0" encoding="utf-8"?>
<blamatrixrix>
<name></name>
<columns number="2">
 <column id="title" num="0">Title</column>
 <column id="content" num="1">Content</column>
</columns>
<rows number="7"></rows>
<b>Description</b>
<b>Description text here</b>
<b>Some title 1</b>
<b>Some text blabla for Some title 1</b>
<b>Some title 2</b>
<b>Some text blabla for Some title 2</b>
<b>Some title 3</b>
<b>Some text blabla for Some title 3</b>
<b>Some title 4</b>
<b>Some text blabla for Some title 4</b>
<b>Some title 5</b>
<b>Some text blabla for Some title 5</b>
<b>Some title 6</b>
<b>Some text blabla for Some title 6</b>
</blamatrixrix>

I would need to parse that data into table so that every other content would go into a own and every other in own .

Final would look like this:

<tr><td>Some title 1</td>  <td>Some title 2'</td> <td>Some title 3</td>  <td>Some title 4'</td></tr>
<tr><td>Some text 1</td><td>Some text 2</td><td>Some text 3</td><td>Some text 4</td></tr>

How should I start doing it? Thanks.

EDIT: I know the XML is not like it should but I cannot do nothing about it. I just need to parse it when its like that.

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

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

发布评论

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

评论(2

撩起发的微风 2024-10-05 13:35:28

对于初学者来说,您的 XML 无效:

<b>Description</c>

应该

<b>Description</b>

并且

<bDescription text here</b>

应该

<b>Description text here</b>

编辑:您还应该考虑将 XML 结构更改为更符合逻辑的内容,例如

<rows number="7">
    <row>
        <description>Description</description>
        <text>Description text here</text>
    </row>
    ...
</rows>

编辑#2:您也可以只使用 jQuery 来解析和编辑。遍历您的 XML,如下所示:

$(xml_string).children().each(function(c) {
    // do something
});

甚至更好:

parseXMLValue($(xml_string).find('noniterablenode').text());
...
$(xml_string).find('iterablenode').each(function(e) {
    // do something with your iterable elements
});

等等,其中 parseXMLValue() 将是您自己的函数,用于使用特定 XML 节点的值。

For starters, your XML is invalid:

<b>Description</c>

should be

<b>Description</b>

and

<bDescription text here</b>

should be

<b>Description text here</b>

Edit: you should also consider changing your XML structure to something more logical, like

<rows number="7">
    <row>
        <description>Description</description>
        <text>Description text here</text>
    </row>
    ...
</rows>

Edit #2: you can also just use jQuery to parse & traverse your XML, like so:

$(xml_string).children().each(function(c) {
    // do something
});

Or even better:

parseXMLValue($(xml_string).find('noniterablenode').text());
...
$(xml_string).find('iterablenode').each(function(e) {
    // do something with your iterable elements
});

etc, where parseXMLValue() would be your own function to use the value of a specific XML node.

初雪 2024-10-05 13:35:28

修复错误后,您可以执行如下操作:

$.ajax( 
  url: "path/to/file.xml", 
  type: "GET", 
  dataType:"xml", 
  success: function(xml){ 
    var cols = [];

    $(xml).find("col").each(function(){
      cols.push({
        id:$(this).attr('id'),
        text:$(this).text()
      });
    });

    $(xml).find("row").each(function(){
      var info = "<tr>";

      for(var i = 0; i < cols.length; i++) {
        info += "<td>" + $(this).find(cols[i].id).text() + "</td>";
      }

      info += "</tr>";

      $("#table").append(info);
    });
  },
  error : function(){ //some code}
});

这适用于以下 XML 文档。

<?xml version="1.0" encoding="utf-8"?>
<tableinfo>
  <columns>
    <col id="title">Title</column>
    <col id="content">Content</column>
  </columns>
  <rows>
    <row>
      <title>test</title>
      <content>text content</content>
    </row>
    <row>
      <title>test2</title>
      <content>more text content</content>
    </row>
  </rows>
</tableinfo>

Once the errors are fixed you could do something like the following:

$.ajax( 
  url: "path/to/file.xml", 
  type: "GET", 
  dataType:"xml", 
  success: function(xml){ 
    var cols = [];

    $(xml).find("col").each(function(){
      cols.push({
        id:$(this).attr('id'),
        text:$(this).text()
      });
    });

    $(xml).find("row").each(function(){
      var info = "<tr>";

      for(var i = 0; i < cols.length; i++) {
        info += "<td>" + $(this).find(cols[i].id).text() + "</td>";
      }

      info += "</tr>";

      $("#table").append(info);
    });
  },
  error : function(){ //some code}
});

This would work with the following XML document.

<?xml version="1.0" encoding="utf-8"?>
<tableinfo>
  <columns>
    <col id="title">Title</column>
    <col id="content">Content</column>
  </columns>
  <rows>
    <row>
      <title>test</title>
      <content>text content</content>
    </row>
    <row>
      <title>test2</title>
      <content>more text content</content>
    </row>
  </rows>
</tableinfo>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文