Reputation: 169
How can I insert a new tag for each word from the text content of a tag?
If i have a xml like:
<root>
<el> Text content for tag
</el>
</root>
I want the output to be:
<root>
<el> <new>Text</new> <new>content</new> <new>for</new> <new>tag</new>
</el>
</root>
Any idea?
Upvotes: 1
Views: 2253
Reputation: 23637
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).
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>
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;
}
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"
.
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);
}
Upvotes: 1