使用 jQuery 解析数据
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者来说,您的 XML 无效:
应该
并且
应该
编辑:您还应该考虑将 XML 结构更改为更符合逻辑的内容,例如
编辑#2:您也可以只使用 jQuery 来解析和编辑。遍历您的 XML,如下所示:
甚至更好:
等等,其中
parseXMLValue()
将是您自己的函数,用于使用特定 XML 节点的值。For starters, your XML is invalid:
should be
and
should be
Edit: you should also consider changing your XML structure to something more logical, like
Edit #2: you can also just use jQuery to parse & traverse your XML, like so:
Or even better:
etc, where
parseXMLValue()
would be your own function to use the value of a specific XML node.修复错误后,您可以执行如下操作:
这适用于以下 XML 文档。
Once the errors are fixed you could do something like the following:
This would work with the following XML document.