Get String content of the editor takes a long time
Post here questions and problems related to oXygen frameworks/document types.
Get String content of the editor takes a long time
Hi everyone,
I'm using the last oxygen SDK (18.0.0.3). I try to get the string content of my current page. My current page is a large XML file (about 700 k) and I want to get the string content during the edition of the document (before saving).
In order to do so, I used this code :
The string is available (just before "writeStringToFile" method) after more than 30 seconds... Why is the reader so long to get this string content ?
Thanks for your help,
Johann
I'm using the last oxygen SDK (18.0.0.3). I try to get the string content of my current page. My current page is a large XML file (about 700 k) and I want to get the string content during the edition of the document (before saving).
In order to do so, I used this code :
Code: Select all
File tempFile = File.createTempFile("tempfile", ".XML");
tempFile.deleteOnExit();
Reader reader = wsEditor.createContentReader();
String xmlContent = "";
int data = reader.read();
while (data != -1) {
char dataChar = (char) data;
xmlContent += dataChar;
data = reader.read();
}
FileUtils.writeStringToFile(tempFile, xmlContent);
Thanks for your help,
Johann
Re: Get String content of the editor takes a long time
Hi Johann,
As general rules:
1) When you read from a reader you should first wrap a buffered reader around it so that you can read larger chunks of chars.
2) You never add strings together, a string contains inside an array of characters like:
['h', 'e', 'l', 'l', 'o']
and it is immutable. So when to this string you add a space for example ' ', the virtual machine needs to create another array of characters which is one character larger:
['h', 'e', 'l', 'l', 'o', ' '].
So whenever you want to add characters you should use string builders.
The optimized code would need to look like this:
If it still takes too long, the problem might be elsewhere.
Regards,
Radu
As general rules:
1) When you read from a reader you should first wrap a buffered reader around it so that you can read larger chunks of chars.
2) You never add strings together, a string contains inside an array of characters like:
['h', 'e', 'l', 'l', 'o']
and it is immutable. So when to this string you add a space for example ' ', the virtual machine needs to create another array of characters which is one character larger:
['h', 'e', 'l', 'l', 'o', ' '].
So whenever you want to add characters you should use string builders.
The optimized code would need to look like this:
Code: Select all
BufferedReader reader = new BufferedReader(wsEditor.createContentReader());
StringBuilder xmlContent = new StringBuilder();
int len = -1;
char[] buffer = new char[1024];
while ((len = reader.read(buffer)) != -1) {
xmlContent.append(buffer, 0, len);
}
String finalContent = xmlContent.toString();
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
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