如何从XML表中获取表数据? [英] How to get table data from html table in xml?
问题描述
我有一个xml文件,它有一些html内容,如粗体,段落和表格。我写了shell脚本来解析除表格以外的所有html标签。我正在使用XML(R包)解析数据。
<根>
<标题>这是虚拟xml文件< / Title>
<内容>此表格汇总了BMC格式的数据。
< div class =abctable>
< tbody>
< tr>
< th width =50%> ABC< / th>
< th width =50%>体重状态< / th>
< / tr>
< tr>
< td>是18.5< / td>
< td> arew< / td>
< / tr>
< tr>
< td> 18.5& amp; mdash; 24.9< / TD>
< td> rweq< / td>
< / tr>
< tr>
< td> 25.0& amp; mdash; 29.9< / TD>
< td> qewrte< / td>
< / tr>
< tr>
< td> 30.0和hwerqer< / td>
< td> rwqe< / td>
< / tr>
< tr>
< td> 40.0 rweq rweq< / td>
< td> rqwe reqw< / td>
< / tr>
< / tbody>
< / table>
< / div>
< / Content>
< Section>等等等等等等< / Section>
< / Root>
如何解析xml中这个表的内容?
$ b $在 XML $ 中有一个叫做 readHTMLTable
的函数。
以下是使用以下xml文件执行此操作的方法:
< Root>
<标题>这是虚拟xml文件< / Title>
<内容>
此表格汇总了BMC格式的数据。
< div class =abctable>
< tbody>
< tr>
< th width =50%> ABC< / th>< th width =50%> Weight status< / th>
< / tr>
< tr>
< td>是18.5< / td>
< td> arew< / td>
< / tr>
< tr>
< td> 18.5& amp; mdash; 24.9< / TD>
< td> rweq< / td>
< / tr>
< tr>
< td> 25.0& amp; mdash; 29.9< / TD>
< td> qewrte< / td>
< / tr>
< tr>
< td> 30.0和hwerqer< / td>
< td> rwqe< / td>
< / tr>
< tr>
< td> 40.0 rweq rweq< / td>
< td> rqwe reqw< / td>
< / tr>
< / tbody>
< / table>
< / Content>
< / div>
< Section>等等等等等等< / Section>
< / Root>
如果保存在名为 /tmp/data.xml
那么你可以使用下面的代码:
doc< - htmlParse(/ tmp / data。 xml)
tableNodes< - getNodeSet(doc,// table)
tb< - readHTMLTable(tableNodes [[1]])
/ pre>
哪五种:
R> tb
V1 V2
1 ABC体重状态
2是18.5 arew
3 18.5& mdash; 24.9美元
4 25.0& mdash; 29.9 qewrte
5 30.0 and hwerqer rwqe
6 40.0 rweq rweq rqwe reqw
I have one xml file which has some html content like bold, paragraph and tables. I have written shell script to parse all html tags except tables. I'm using XML (R package) to parse the data.
<Root>
<Title> This is dummy xml file </Title>
<Content> This table summarises data in BMC format.
<div class="abctable">
<table border="1" cellspacing="0" cellpadding="0" width="100%" class="coder">
<tbody>
<tr>
<th width="50%">ABC</th>
<th width="50%">Weight status</th>
</tr>
<tr>
<td>are 18.5</td>
<td>arew</td>
</tr>
<tr>
<td>18.5 &mdash; 24.9</td>
<td>rweq</td>
</tr>
<tr>
<td>25.0 &mdash; 29.9</td>
<td>qewrte</td>
</tr>
<tr>
<td>30.0 and hwerqer</td>
<td>rwqe</td>
</tr>
<tr>
<td>40.0 rweq rweq</td>
<td>rqwe reqw</td>
</tr>
</tbody>
</table>
</div>
</Content>
<Section>blah blah blah</Section>
</Root>
How to parse the content of this table which in present in xml?
解决方案 Well there is a function called readHTMLTable
in the XML
package, that seems to do just what you need ?
Here is a way to do it with the following xml file :
<Root>
<Title> This is dummy xml file </Title>
<Content>
This table summarises data in BMC format.
<div class="abctable">
<table border="1" cellspacing="0" cellpadding="0" width="100%" class="coder">
<tbody>
<tr>
<th width="50%">ABC</th><th width="50%">Weight status</th>
</tr>
<tr>
<td>are 18.5</td>
<td>arew</td>
</tr>
<tr>
<td>18.5 &mdash; 24.9</td>
<td>rweq</td>
</tr>
<tr>
<td>25.0 &mdash; 29.9</td>
<td>qewrte</td>
</tr>
<tr>
<td>30.0 and hwerqer</td>
<td>rwqe</td>
</tr>
<tr>
<td>40.0 rweq rweq</td>
<td>rqwe reqw</td>
</tr>
</tbody>
</table>
</Content>
</div>
<Section>blah blah blah</Section>
</Root>
If this is saved in a file called /tmp/data.xml
then you can use the following code :
doc <- htmlParse("/tmp/data.xml")
tableNodes <- getNodeSet(doc, "//table")
tb <- readHTMLTable(tableNodes[[1]])
Which fives :
R> tb
V1 V2
1 ABC Weight status
2 are 18.5 arew
3 18.5 — 24.9 rweq
4 25.0 — 29.9 qewrte
5 30.0 and hwerqer rwqe
6 40.0 rweq rweq rqwe reqw
这篇关于如何从XML表中获取表数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!