dom4j 中的元素偏移和覆盖文本

发布于 2024-12-12 00:04:52 字数 816 浏览 0 评论 0原文

我们需要处理一个包含 PCDATA 元素的 XML 文件,如下所示:

<corpus id="c01">
  <text id="t01>
    <sentence id="s01">Mary <instance id="i01">had</instance> a <instance id="i02">lamb</instance>.</sentence>
    <sentence id="s02">...</sentence>
    ...
  </text>
  ...
</corpus>

对于每个对于每个,我们需要填充一个包含句子 ID 和句子涵盖的全文的数据结构。然后,对于每个,我们需要填充一个数据结构,其中包含实例 ID 及其在句子中的开始和结束位置。 (我们不关心空白是否被标准化。)

因此对于上面的例子,我们基本上需要以下内容:

s.id = "s01"
s.text = "Mary had a lamb."
i1.id = "i01"
i1.start = 6
i1.end = 8
i2.id = "i02"
i2.start = 12
i2.end = 15

有没有办法用 dom4j 做到这一点? Element.getText() 方法会跳过子元素的文本,并且我没有看到任何给出元素在另一个元素中的偏移量的方法。如果 dom4j 不适合此任务,那么什么是更好的工具?

We need to process an XML file which contains PCDATA elements like the following:

<corpus id="c01">
  <text id="t01>
    <sentence id="s01">Mary <instance id="i01">had</instance> a <instance id="i02">lamb</instance>.</sentence>
    <sentence id="s02">...</sentence>
    ...
  </text>
  ...
</corpus>

For each <sentence> of each <text>, we need to populate a data structure containing the sentence ID and the full text covered by the sentence. Then for each <instance>, we need to populate a data structure containing the instance ID and its start and end position within the sentence. (We don't care whether or not the white space is normalized.)

So for the above example, we basically need the following:

s.id = "s01"
s.text = "Mary had a lamb."
i1.id = "i01"
i1.start = 6
i1.end = 8
i2.id = "i02"
i2.start = 12
i2.end = 15

Is there any way of doing this with dom4j? The Element.getText() method skips over the text of child elements, and I don't see any methods which give the offset of an element within another. If dom4j isn't appropriate for this task, what's a better tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-12-19 00:04:52

这当然是可行的,但需要做一些工作。您可以创建一个访问者来跟踪它在树中的位置,并随着它的进展累积文本和实例偏移量。然而,该解决方案也可以直接由 SAX 处理程序实现,这样会快得多。

这应该给出一些开始:

public class Main extends DefaultHandler {

StringBuilder buf = new StringBuilder();
boolean collecting = false;
int ic = 0;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (localName.equals("sentence")) {
        System.out.printf("s.id=%s\n", attributes.getValue("id"));
        collecting = true;
        buf.setLength(0);
        ic = 0;
    } else if (localName.equals("instance")) {
        ++ic;
        System.out.printf("i%d.id=%s\n", ic, attributes.getValue("id"));
        System.out.printf("i%d.start=%s\n", ic, buf.length());
    }

}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (localName.equals("sentence")) {
        collecting = false;
        System.out.printf("s.text=%s\n", buf.toString());
    } else if (localName.equals("instance")) {
        System.out.printf("i%d.end=%s\n", ic, buf.length());
    }
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (collecting) {
        buf.append(ch, start, length);
    }
}

public static void main(String[] args) throws Exception {

    SAXParserFactory f = SAXParserFactory.newInstance();
    f.setNamespaceAware(true);
    f.newSAXParser().parse(Main.class.getResourceAsStream("data.xml"),
            new Main());
}
}

It is certainly doable, but requires a little work. You could create a Visitor that keeps track of where it is in the tree and accumulates the text and instance offsets as it progresses. However, that solution could just as well be implemented by a SAX handler directly, which would be a lot faster.

This should give something to start with:

public class Main extends DefaultHandler {

StringBuilder buf = new StringBuilder();
boolean collecting = false;
int ic = 0;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (localName.equals("sentence")) {
        System.out.printf("s.id=%s\n", attributes.getValue("id"));
        collecting = true;
        buf.setLength(0);
        ic = 0;
    } else if (localName.equals("instance")) {
        ++ic;
        System.out.printf("i%d.id=%s\n", ic, attributes.getValue("id"));
        System.out.printf("i%d.start=%s\n", ic, buf.length());
    }

}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (localName.equals("sentence")) {
        collecting = false;
        System.out.printf("s.text=%s\n", buf.toString());
    } else if (localName.equals("instance")) {
        System.out.printf("i%d.end=%s\n", ic, buf.length());
    }
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (collecting) {
        buf.append(ch, start, length);
    }
}

public static void main(String[] args) throws Exception {

    SAXParserFactory f = SAXParserFactory.newInstance();
    f.setNamespaceAware(true);
    f.newSAXParser().parse(Main.class.getResourceAsStream("data.xml"),
            new Main());
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文