Code review requested

E

Ed

Hi All,

I am starting out in Java. My development skill set is in another
language and therefore am not sure of the best way to develop in Java
(ie. performance, efficient, memory consumption, etc).

I have written a program that accepts an XML document as an input in
the form of a stream.
I need to insert a filename into the XML document and output again as
a stream.
I have written what I think is the way to do it. I am sure there are
better ways or things I have not considered.

Specific questions I have:
1. How do I convert special characters in the text without manually
coding "replace '>' with >" ?
2. Is treating it as a byte stream the best way? My intent is to
avoid the overhead of using the Document approach I have read about
for the sake of inserting one tag.
3. To avoid namespace problems I deliberately insert just after the
root tag. Is this appropriate? Is it fair to assume the second
occurrence of ">" is the end of the second tag?
4. What about Unicode? I am simply writing my new tag and filename
out with no concern for proper Unicode handling. How can I improve
that?
Also, the comparison with the input byte to '>' does not consider
Unicode, should it?

Any advice or feedback will be greatly appreciated.

Thanks,
Ed

The code:
//note: the inputStream is as the name suggests, an InputStream.
//The result is an OutputStream.
....
try {
//Filename
filename = "C:/somepath/somefile.dat";

/*
* Search the byte stream for the end of the second tag.
* The assumption is that the first tag is the <?xml?>
* declaration.
* The second is the start of the message type.
*/
int closeTag = 0;
int x = 0;
while ((x = inputStream.read()) > 0){
if (closeTag < 3){
if ((char)x == '>'){ closeTag++; }
}
result.write(x);
if (closeTag == 2){
closeTag++;
result.write("\r\n<SourceFilename>".getBytes());
result.write(filename.getBytes());
result.write("</SourceFilename>".getBytes());
};
}

} catch (Exception e) {
throw new Exception("Error:" + e.getMessage(), e);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top