我正在尝试使用 xalan XPathAPI 解析 xhtml 文件。我被以下要求困住了。这是 xhtml 的片段
<table border="0" cellspacing="0" cellpadding="0" class="cmnt_message">
<tr>
<td width="33" align="right">
<span class="cmnt_baloon"><!-- Image --></span>
</td>
<td width="767" class="red pad_l_10">
Posted by Macha on Mar 06, 2011 at 01:02 PM
</td>
</tr>
<tr>
<td colspan="2" class="cmnt_text">
@rmaytee<br />
<br />
#2<br />
<br />
In 2011 it is possible to switch to old mat/map browser<br />
<br />
Just look around<br />
<br />
<a target="_blank" href=
"http://area.autodesk.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/material-editor/">area.autodesk.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/material-editor/</a><br />
<br />
<br />
<br />
</td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" class="cmnt_message">
<tr>
<td width="33" align="right">
<span class="cmnt_baloon"><!-- Image --></span>
</td>
<td width="767" class="red pad_l_10">
Posted by rmaytee on Mar 02, 2011 at 06:04 PM
</td>
</tr>
<tr>
<td colspan="2" class="cmnt_text">
2 things:<br />
<br />
1- Please bring back "use object center as start snap point" in the snap settings. We have voiced our opinion about this, now please show us you care. <a target="_blank" href=
"http://www.the-area.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/use-object-center-as-start-snap-point">www.the-area.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/use-object-center-as-start-snap-point</a><br />
<br />
2- Make the Material/Map Browser the way it used to be. It is SO SLOW. At least make an option to switch to a "classic Material/Map Browser" or something.
</td>
</tr>
</table>
我在这里面临几个问题。
- 我正在尝试提取 cmnt_message 类的值。一个是第一个 block 下的“Posted By...”文本和 cmnt_text 下的文本内容。这是第一个部分发布的 xpath
/html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[1]/html:td[2]/文本()
这会返回“由 Macha 于 2011 年 3 月 6 日下午 01:02 发布”,这就是我想要的。但是当我尝试使用以下 xpath 表达式获取 cmnt_text
/html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[2]/html:td/text( )
我得到“@rmaytee”,即第一个
之前的值。我正在尝试获取 cmnt_text 中的整个文本。
另一个问题是我需要迭代 cmnt_message 并创建一个由发布者和评论组成的 Message 对象集合。 不确定如何使用 Xpath 进行迭代。
SAX2DOM sax2dom = new SAX2DOM(); p.setContentHandler(sax2dom); p.parse(new InputSource(urlXML.openStream())); 节点 doc = sax2dom.getDOM(); XObject comment = XPathAPI.eval(doc,commentPath);
但这仅返回 cmnt_message 类的第一次出现。
任何指针都将受到高度赞赏。
- 谢谢
最佳答案
你想要的叫string value .
如果您的 XPath 引擎支持字符串数据类型作为结果,您可以使用:
string(
/html:html
/html:body
//html:div[@class='content_d']
/html:table[@class='cmnt_message']
/html:tr[1]
/html:td[2]
)
或者选择 td
元素并使用正确的 DOM 方法。
对于像 XHTML 这样的混合内容模型使用文本节点并不好。
关于java - Xalan XPathAPI遍历问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5256572/