1
私の目的は、XML入力を行い、ノード内のテキストをXML DOM要素に置き換えてXML出力を生成することです。私のXML入力と期待される出力はhere, in this SO questionです。ここでJava:splitText()メソッドの分割ノード
は私の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()
のみテキストインターフェイスを使用することができるということである。textNode
変数である
textNode.splitText
ない。しかし、私は、XPathを持つノードからテキストを取り出すことを明示しています。
このコードを動作させるにはどうすればよいですか?
この場合、splitText
メソッドを使用するにはどうすればよいですか?
回答ありがとうございます。このコードにはもう少し問題がありますが、それは別の話です。 – Vitaliy