Serializing AuthorNode to String
Post here questions and problems related to oXygen frameworks/document types.
-
- Posts: 176
- Joined: Wed Apr 29, 2009 4:55 pm
Serializing AuthorNode to String
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
-
- Posts: 9431
- Joined: Fri Jul 09, 2004 5:18 pm
Re: Serializing AuthorNode to String
Hi Jan,
We have specific API for what you need:
https://www.oxygenxml.com/InstData/Edit ... tFragment-
Coming back to your remarks, the Author Nodes structure looks like the one in the image here:
https://www.oxygenxml.com/InstData/Edit ... gment.html
Indeed there are only element nodes, no text nodes. Each node points using the "getStartOffset()" and "getEndOffset()" methods to a large content string containing all text characters and special characters for the node start/end offsets. And there are methods like "AuthorDocumentController.getChars(int, int, Segment)" which allow you to access text between two offsets.
Regards,
Radu
We have specific API for what you need:
https://www.oxygenxml.com/InstData/Edit ... tFragment-
Coming back to your remarks, the Author Nodes structure looks like the one in the image here:
https://www.oxygenxml.com/InstData/Edit ... gment.html
Indeed there are only element nodes, no text nodes. Each node points using the "getStartOffset()" and "getEndOffset()" methods to a large content string containing all text characters and special characters for the node start/end offsets. And there are methods like "AuthorDocumentController.getChars(int, int, Segment)" which allow you to access text between two offsets.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
-
- Posts: 176
- Joined: Wed Apr 29, 2009 4:55 pm
Re: Serializing AuthorNode to String
Cool, I was not aware of that method. In my case I use AuthorNode as an Input:
It works, but while the original fragment contains attributes in the desired order, the insertXMLFragment method is causing reordering them alphabetically...
Code: Select all
AuthorDocumentFragment uLinkFragment = documentController.createDocumentFragment(uLinkElement, true);
String uLinkXmlFragment = documentController.serializeFragmentToXML(uLinkFragment);
// modifying the fragment
documentController.deleteNode(uLinkElement);
documentController.insertXMLFragment(uLinkXmlFragment, authorAccess.getEditorAccess().getCaretOffset());
-
- Posts: 176
- Joined: Wed Apr 29, 2009 4:55 pm
Re: Serializing AuthorNode to String
That alphabetical sorting is only in the Author mode, not in the XML source. It is caused by the settings, which can be disabled in Preferences...|Editor|Edit Modes|Author: Sort attributes alphabetically for "Full Tags with Attributes".
Return to “SDK-API, Frameworks - Document Types”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service