从XML文本中提取数据

时间:2017-02-06 07:08:44

标签: java xml xpath sax

我有很多XML数据,如下所示:

<contextfile concordance=brown>
<context filename=br-a02 paras=yes>
<p pnum=1>
<s snum=1>
<wf cmd=done pos=NN lemma=committee wnsn=1 lexsn=1:14:00::>Committee</wf>
<wf cmd=done pos=NN lemma=approval wnsn=1 lexsn=1:04:02::>approval</wf>
<wf cmd=ignore pos=IN>of</wf>
<wf cmd=done rdf=person pos=NNP lemma=person wnsn=1 lexsn=1:03:00:: pn=person>Gov._Price_Daniel</wf>
<wf cmd=ignore pos=POS>'s</wf>
<punc>``</punc>
<wf cmd=done pos=JJ lemma=abandoned wnsn=1 lexsn=5:00:00:uninhabited:00>abandoned</wf>
<wf cmd=done pos=NN lemma=property wnsn=1 lexsn=1:21:00::>property</wf>
<punc>''</punc>
<wf cmd=done pos=NN lemma=act wnsn=1 lexsn=1:10:01::>act</wf>
<wf cmd=done pos=VB lemma=seem wnsn=1 lexsn=2:39:00::>seemed</wf>
<wf cmd=done pos=JJ lemma=certain wnsn=4 lexsn=3:00:03::>certain</wf>
<wf cmd=done pos=NN lemma=thursday wnsn=1 lexsn=1:28:00::>Thursday</wf>
<wf cmd=ignore pos=IN>despite</wf>
<wf cmd=ignore pos=DT>the</wf>
<wf cmd=done pos=JJ lemma=adamant wnsn=1 lexsn=5:00:00:inflexible:02>adamant</wf>
<wf cmd=done pos=NN lemma=protest wnsn=1 lexsn=1:10:00::>protests</wf>
<wf cmd=ignore pos=IN>of</wf>
<wf cmd=done pos=NN lemma=texas wnsn=1 lexsn=1:15:00::>Texas</wf>
<wf cmd=done pos=NN lemma=banker wnsn=1 lexsn=1:18:00::>bankers</wf>
<punc>.</punc>
</s>
</p>

由此我需要在</wf>之前提取单词以获得输出:

Committee approval of Gov. Price Daniel's `` abandoned property '' act seemed certain Thursday despite the adamant protests of Texas bankers .

之前我从未使用过xml文本,所以我有点无能为力。

我尝试使用我在网上找到的一些示例xml代码来提取这个但是有一个错误说:开放引用是预期属性&#34;一致&#34;与元素类型相关联&#34; contextfile&#34;。 我要解析的所有文件都以:

开头
<contextfile concordance=brown>
<context filename=br-a02 paras=yes>

但文件中的后续数据以:

开头
<p pnum=2>
<s snum=2>

........
</s>
</p>

1 个答案:

答案 0 :(得分:0)

这似乎有效:

//s[@snum]/string-join(wf | punc, " ")

我在http://xpather.com/Va3jRPr4(xpath在线测试人员)上使用您的示例&#34; p&#34;标签的内容两次。您可以向下拖动水平条以查看整个结果。