Serializing AuthorNode to String
Posted: Tue Oct 11, 2022 6:59 pm
Dear All,
I am trying to convert AuthorNode at caret position to String so it could be, after some tweaking, pasted instead of that original element. But I have difficulties iterating the element children in case of mixed content as 'getContentNodes()' method returns just elements, not text nodes.
Any idea how to convert the content properly?
Thanks, Jan
I am trying to convert AuthorNode at caret position to String so it could be, after some tweaking, pasted instead of that original element. But I have difficulties iterating the element children in case of mixed content as 'getContentNodes()' method returns just elements, not text nodes.
Code: Select all
private static String getXmlFragment(AuthorNode authorNode) throws BadLocationException {
StringBuilder xmlFragment = new StringBuilder();
switch (authorNode.getType()) {
case AuthorNode.NODE_TYPE_TEXT : {
xmlFragment.append(authorNode.getTextContent());
break;
}
case AuthorNode.NODE_TYPE_ELEMENT: {
d docbookElement = (d) authorNode;
xmlFragment.append("<" + docbookElement.getName());
for (int i = 0; i < docbookElement.getAttributesCount(); i++) {
String attr = docbookElement.getAttributeAtIndex(i);
if (!attr.startsWith("xmlns")) {
xmlFragment.append(" " + attr + "=\"" + docbookElement.getAttribute(attr).getValue() + "\"");
}
}
xmlFragment.append(">");
for (AuthorNode contentNode : docbookElement.getContentNodes()) {
xmlFragment.append(getXmlFragment(contentNode));
}
/*
ContentIterator ci = docbookElement.getContentIterator();
while (ci.hasNext()) {
xmlFragment.append(ci.next());
}
*/
xmlFragment.append("</" + authorNode.getName() + ">");
}
}
return xmlFragment.toString();
}
Thanks, Jan