Java:通过正则表达式字符串解析使用元素丰富xml
我有一项复杂的任务将docx文档转换为jatsxml。现在,我已经从docx和xslt中获取了一切可能的信息。下一步是解析xml文件,并通过将一些xml字符串(节点中的文本)更改为xml元素来更新它。我没有在这个论坛上的类似问题中找到这些信息。我的输入xml如下所示:Java:通过正则表达式字符串解析使用元素丰富xml,java,regex,xml,dom,xpath,Java,Regex,Xml,Dom,Xpath,我有一项复杂的任务将docx文档转换为jatsxml。现在,我已经从docx和xslt中获取了一切可能的信息。下一步是解析xml文件,并通过将一些xml字符串(节点中的文本)更改为xml元素来更新它。我没有在这个论坛上的类似问题中找到这些信息。我的输入xml如下所示: <article dtd-version="3.0" article-type="other"> <body> <sec> <title>mySuperTit
<article dtd-version="3.0" article-type="other">
<body>
<sec>
<title>mySuperTitle</title>
<p>
This is some scientific stuff [1]. Here is more complicated info. This text is even more bizarre [2,3].
</p>
<p>
Einstein formulas [4]. String theory [5,6]. Really don`t know what to write here[7,8].
</p>
</sec>
<sec>
<title>AnotherBoringTitle</title>
<p>
Another one section and obviously here is even more citations [9,10,11]
</p>
</sec>
</body>
</article>
我的超级头衔
这是一些科学的东西[1]。这里有更复杂的信息。这篇文章甚至更奇怪[2,3]。
爱因斯坦公式[4]。弦论[5,6]。真的不知道在这里写什么[7,8]。
另一个头衔
另一个章节,显然这里还有更多的引文[9,10,11]
理想情况下,我希望将所有的引用(在[]中是简单的数字)替换为xml元素。例如:
<article dtd-version="3.0" article-type="other">
<body>
<sec>
<title>mySuperTitle</title>
<p>
This is some scientific stuff [<xref ref-type="bibr" rid="bib1">1</xref>]. Here is more complicated info. This text is even more bizarre [<xref ref-type="bibr" rid="bib2">2</xref>,<xref ref-type="bibr" rid="bib3">3</xref>].
</p>
<p>
Einstein formulas [<xref ref-type="bibr" rid ="bib4">4</xref>]. String theory [<xref ref-type="bibr" rid ="bib5">5</xref>,<xref ref-type="bibr" rid ="bib6">6</xref>]. Really don`t know what to write here [<xref ref-type="bibr" rid ="bib7">7</xref>,<xref ref-type="bibr" rid ="bib8">8</xref>].
</p>
</sec>
<sec>
<title>AnotherBoringTitle</title>
<p>
Another one section and obviously here is even more citations [<xref ref-type="bibr" rid ="bib9">9</xref>,<xref ref-type="bibr" rid ="bib10">10</xref>,<xref ref-type="bibr" rid ="bib11">11</xref>]
</p>
</sec>
</body>
</article>
我的超级头衔
这是一些科学的东西[1]。这里有更复杂的信息。这篇文章甚至更奇怪[2,3]。
爱因斯坦公式[4]。弦论[5,6]。真的不知道在这里写什么[7,8]。
另一个头衔
另一个章节,显然这里还有更多的引文[9,10,11]
我在Java方面没有太多经验,但已经尝试使用DOM、Xpath和正则表达式来完成这项任务。问题是,当我解析文档并获取节点时,我必须从DOM获取它,转换为字符串,将字符串中的字符替换为数字,转换为元素并生成输出。我发现将这个字符串转换为元素(这需要创建新的documentBuilder,或者它如何调用)并替换DOM中适当的元素以输出新的xml是有问题的。这是一个简单的解决方案吗?或者我必须在这里写很多行代码?这可以使用DOM和regexex: 我假设您知道如何找到正确的
Text
节点
然后,您需要:
//get the split point:
int prevSplitOffset = 0;
Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(textNode.getData());
while (m.find()) {
// get the text and split it:
Text number = textNode.splitText(m.start(1) - prevSplitOffset);
textNode = number.splitText(m.group(1).length());
// Replace the number with a new DOM node:
Element xref = document.createElement("xref");
xref.setAttribute("rid", "bib" + m.group(1));
xref.setAttribute("ref-type", "bibr");
number.getParentNode().replaceChild(xref, number);
xref.appendChild(number);
prevSplitOffset = m.end(1);
}
这可以使用DOM和regexex: 我假设您知道如何找到正确的
Text
节点
然后,您需要:
//get the split point:
int prevSplitOffset = 0;
Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(textNode.getData());
while (m.find()) {
// get the text and split it:
Text number = textNode.splitText(m.start(1) - prevSplitOffset);
textNode = number.splitText(m.group(1).length());
// Replace the number with a new DOM node:
Element xref = document.createElement("xref");
xref.setAttribute("rid", "bib" + m.group(1));
xref.setAttribute("ref-type", "bibr");
number.getParentNode().replaceChild(xref, number);
xref.appendChild(number);
prevSplitOffset = m.end(1);
}
谢谢你的回答。我正在使用xpath查找所需的节点。明天我将检查这段代码,但我假设这就是我要查找的。请确保根据您的环境调整变量名;-)请更改编号.getParentNode().replaceChild(编号,外部参照);到number.getParentNode().replaceChild(外部参照,编号);第二个问题是,它只在一个文本节点中找到第一个引用。如果有第二个-抛出错误:索引\u大小\u错误:索引或大小为负数,或大于允许值。我修复了它。第一次拆分后,需要调整偏移量。谢谢回答。我正在使用xpath查找所需的节点。明天我将检查这段代码,但我假设这就是我要查找的。请确保根据您的环境调整变量名;-)请更改编号.getParentNode().replaceChild(编号,外部参照);到number.getParentNode().replaceChild(外部参照,编号);第二个问题是,它只在一个文本节点中找到第一个引用。如果有第二个-抛出错误:索引\u大小\u错误:索引或大小为负数,或大于允许值。我修复了它。第一次拆分后,需要调整偏移。