Java:使用splitText()方法拆分节点

时间:2017-01-20 19:00:18

标签: java regex xml xpath

我的目标是进行XML输入,将节点中的一些文本替换为XML DOM元素并生成XML输出。我的XML输入和预期输出可以找到here, in this SO question

这是我的java代码:

private static void textTransformCitations(Document document) 
{       
    XPath xPath =  XPathFactory.newInstance().newXPath();
    String expression = "/article/body/sec/p/text()";
    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); i++) 
    {
        Node textNode = nodeList.item(i);
        Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(textNode.getNodeValue());
        while (m.find()) 
        {
          Text number = textNode.splitText(m.start(1));
          textNode = number.splitText(m.group(1).length());
          Element xref = document.createElement("xref");
          xref.setAttribute("rid", "bib" + m.group(1));
          xref.setAttribute("ref-type", "bibr");
          number.getParentNode().replaceChild(number, xref);
          xref.appendChild(number);
        }
    }
}   // Added by edit!

显然问题是splitText()只能用于Text接口:

  

textNode.splitText

textNode变量不是。但我已明确声明要从具有XPath的节点中检索文本。

如何使此代码正常工作? 在这种情况下,如何使用splitText方法?

1 个答案:

答案 0 :(得分:0)

将语句Node textNode = nodeList.item(i);更改为Text textNode = (Text)nodelist.item(i);