修改 XML 标签的文本内容 [英] Modify the text content of XML tag
问题描述
如何从标签的文本内容中为每个单词插入一个新标签?
How can I insert a new tag for each word from the text content of a tag?
如果我有一个像:
<root>
<el> Text content for tag
</el>
</root>
我希望输出为:
<root>
<el> <new>Text</new> <new>content</new> <new>for</new> <new>tag</new>
</el>
</root>
有什么想法吗?
推荐答案
您之前已经问过这个问题的一部分:在 XML 文件中添加新节点
You already asked part of this question before here: Add new node in XML file
基于此,我将使用一个与您在 那个 问题中使用的示例类似的示例,该示例比这个更复杂一些,因为元素不包含纯文本,但可以具有混合内容(元素和文本).
Based on that, I will use an example similar on the one you used in that question, which is a bit more complex than this one because the elements didn't contain plain text, but could have mixed content (elements and text).
我在那里使用的 XML 是您之前发布的:
The XML I am using there is the one you posted before:
<nodes>
<RegDef>This <i>text</i> have i node.</RegDef>
<RegDef>This text doesn't have i atribute.</RegDef>
</nodes>
请参阅上一个问题.在那个问题中,我调用了一个我称之为 wrapWordsInContents()
的方法,该方法返回一个新元素,其单词包含在 <w>
元素中.该新元素用于替换旧元素.这是那个方法:
Refer to the previous question. In that question I call a method which I called wrapWordsInContents()
which returns a new element with its words wrapped inside <w>
elements. That new element is used to replace the old one. This is that method:
public static Element wrapWordsInContents(Element node, Document document) {
NodeList children = node.getChildNodes();
int size = children.getLength();
Element newElement = document.createElement(node.getTagName());
for(int i = 0; i < size; i++) {
if (children.item(i).getNodeType() == Document.ELEMENT_NODE) {
newElement.appendChild(wrapWordsInContents((Element)(children.item(i)), document));
} else { // text node
String text = children.item(i).getTextContent().trim();
if(text.isEmpty()) {
continue;
}
String[] words = text.split("\\s");
for(String word : words) {
Element w = document.createElement("w");
Node textNode = document.createTextNode(word);
w.appendChild(textNode);
newElement.appendChild(w);
}
}
}
return newElement;
}
请注意,它会递归地处理任何子元素,并使用
标记将它在其中找到的任何单词包装起来.如果您想使用
,只需将 "w"
替换为 "new"
.
Note that it recursively processes any child elements, wrapping any words it finds inside them with the <w>
tag. If you want to use <new>
, just replace "w"
for "new"
.
如果您使用此方法运行上一个问题中的代码,您将获得一个新文档,该文档将生成一个 XML,序列化后将生成此输出:
If you run the code in the previous question with this method, you will get a new document which will generate a XML that when serialized will produce this output:
<nodes>
<RegDef><w>This</w><i><w>text</w></i><w>have</w><w>i</w><w>node.</w></RegDef>
<RegDef><w>This</w><w>text</w><w>doesn't</w><w>have</w><w>i</w><w>atribute.</w></RegDef>
</nodes>
对于您在此问题中发布的代码示例,您将使用:
For the code example you posted in this question, you would use:
NodeList elNodes = document.getElementsByTagName("el");
int size = elNodes.getLength();
for(int i = 0; i < size; i++) {
Element el = (Element)elNodes.item(i);
Element newEl = wrapWordsInContents(el, document);
Element parent = (Element)el.getParentNode(); // this is `<root>`
parent.replaceChild(newEl, el);
}
这篇关于修改 XML 标签的文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!