0

我正在尝试使用 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>

我在这里面临几个问题。

  1. 我正在尝试提取 cmnt_message 类的值。一个是第一个块下的“发布者...”文本和 cmnt_text 下的文本内容。这是第一个由 part 发布的 xpath

/html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[1]/html:td[2]/text( )

这将返回“由 Macha 于 2011 年 3 月 6 日下午 1: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”,即直到 first 的值
。我试图在 cmnt_text 中获取整个文本。

  1. 另一个问题是我需要遍历 cmnt_message 并创建一个 Message 对象的集合,该对象由发布者和评论组成。不确定如何使用 Xpath 进行迭代。

    SAX2DOM sax2dom = 新 SAX2DOM(); p.setContentHandler(sax2dom); p.parse(new InputSource(urlXML.openStream())); 节点文档 = sax2dom.getDOM(); XObject 注释 = XPathAPI.eval(doc,commentPath);

但这只会让我第一次出现 cmnt_message 类。

任何指针将不胜感激。

  • 谢谢
4

2 回答 2

1

You have to either use XSL or iterate over the child nodes of /html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[2]/html:td yourself.

于 2011-03-10T07:22:14.713 回答
1

您想要的称为字符串值

如果您的 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 这样的混合内容模型并不好。

于 2011-03-10T18:10:13.687 回答