By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,126 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,126 IT Pros & Developers. It's quick & easy.

need help converting java to c++

P: n/a
hello,
i have a java app that constructs an xml from a specific file format
and vice versa.
i've been asked to convert it to c++, but im not an expert in c++,
actually im mere beginner you can say.
i got a couple of questions though:
1- is there any equivalent to java's Stream
classes(ByteArrayInputStream,InputStreamRedare,Dat aInputStream ..)
2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
3- i know strings are ended in '\0' the null character in c++.in the
fil i want to parse, xml-to-be-elements are separated by '\0', how can
i split the read text into strings using '\0' as my delimiter without
confusing c++.
4- the most important : how to parse and construct xml in c++, is
there any api for that and where to find it?
5- last nut not least , since im targetting Windows , would it be
better to use unmanaged Visual C++ . does it have the types i am
looking for,and does it make it easy to accomplish the task ?
oh and i use g++ for windows.
Thanks
regards

Feb 13 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On 13 Feb., 12:17, "steven acer" <dudest...@gmail.comwrote:
hello,
i have a java app that constructs an xml from a specific file format
and vice versa.
i've been asked to convert it to c++, but im not an expert in c++,
actually im mere beginner you can say.
I am not sure that is a fair "first" job.
i got a couple of questions though:
1- is there any equivalent to java's Stream
classes(ByteArrayInputStream,InputStreamRedare,Dat aInputStream ..)
Yes. For formatted IO you probably need an std::ofstream. For
unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.
2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
You should read the documentation and be more specific in your
question here. It's not difficult, but we can't answer without more
knowledge of your problem.
3- i know strings are ended in '\0' the null character in c++.
That is false, actually. C strings are null-terminated, C++ strings
are not.
in the
fil i want to parse, xml-to-be-elements are separated by '\0', how can
i split the read text into strings using '\0' as my delimiter without
confusing c++.
This again depends on your requirements.
4- the most important : how to parse and construct xml in c++, is
there any api for that and where to find it?
There are several nice xml-parsers for C++. A short google should give
you a candidate. Choosing one probably is more of a political decision
to be made by your company.
5- last nut not least , since im targetting Windows , would it be
better to use unmanaged Visual C++ . does it have the types i am
looking for,and does it make it easy to accomplish the task ?
oh and i use g++ for windows.
There is only one C++ - namely the language we discuss in this
newsgroup. Microsoft has created a new language which they for some
reason call "managed C++", but it is not C++. "Managed C++" might
likely be a fine language for programming, but it can't be used for
porting to C++.

/Peter

Feb 13 '07 #2

P: n/a
On Tue, 13 Feb 2007 04:00:26 -0800, peter koch wrote:
On 13 Feb., 12:17, "steven acer" wrote:
>hello,
i have a java app that constructs an xml from a specific file format
and vice versa.
i've been asked to convert it to c++, but im not an expert in c++,
actually im mere beginner you can say.
I am not sure that is a fair "first" job.
>i got a couple of questions though:
1- is there any equivalent to java's Stream
classes(ByteArrayInputStream,InputStreamRedare,Da taInputStream ..)

Yes. For formatted IO you probably need an std::ofstream.
(did you mean std::ifstream ?)
For unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.
Easier to use one of the std::ifstream::get() functions, or better
std::ifstream::read(); but you may not need to use unformatted input - see
below.
>2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
You should read the documentation and be more specific in your
question here. It's not difficult, but we can't answer without more
knowledge of your problem.
>3- i know strings are ended in '\0' the null character in c++.
That is false, actually. C strings are null-terminated, C++ strings
are not.
To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).
>in the
fil i want to parse, xml-to-be-elements are separated by '\0', how can
i split the read text into strings using '\0' as my delimiter without
confusing c++.
Perhaps you could use std::getline() specifying '\0' as delimiter to read
your xml elements straight into std::string's. You won't confuse C++ that
way, you won't have to mess around with memory management and your
elements end up in nice C++-style strings.

/.../

--
Lionel B
Feb 13 '07 #3

P: n/a
On 13 Feb., 13:48, Lionel B <m...@privacy.netwrote:
On Tue, 13 Feb 2007 04:00:26 -0800, peter koch wrote:
On 13 Feb., 12:17, "steven acer" wrote:
[snip]
Yes. For formatted IO you probably need an std::ofstream.

(did you mean std::ifstream ?)
Yup.
>
For unformatted/binary, you could use a streambuffer or the C file
iosystem. You better study that.

Easier to use one of the std::ifstream::get() functions, or better
std::ifstream::read(); but you may not need to use unformatted input - see
below.
What is "easier" depends. My experience with binary IO in C++ has
either been a layered above C file io for simple IO jobs as the OP has
or going "to the metal" for IO with high performance requirements (or
large files: so far as I know MSVC still only supports IO for files up
to 2GB).
[snip]
3- i know strings are ended in '\0' the null character in c++.
That is false, actually. C strings are null-terminated, C++ strings
are not.

To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).
The standard allows embedded nulls and also has other requirements
that forbids std::string to be implemented as a null-terminated
string.

[snip]

/Peter

Feb 13 '07 #4

P: n/a
Thanks for the replies guys.
first i dont wanna study and delve into c++ because im a java
developer and im doing this conversion for *just 1 time*.besides it's
a really small app.so any help i can get is really appreciated.
ok let's see
To be more precise: the C++ string class std::string is probably not
implemented using null-terminated character arrays (the standard does not
require this).
Perhaps you could use std::getline() specifying '\0' as delimiter to read
your xml elements straight into std::string's. You won't confuse C++ that
way, you won't have to mess around with memory management and your
elements end up in nice C++-style strings.
i think you got me wrong . i have a .trs(whatever) file which i want
to read and generate an xml based upon its contents.
in this file i have data separated by null characters, in java i would
have done read the contents of the trs file into a string
and then myString.split("\0") would return me an array of tokens.is
there anything similar in c++ to split a string using a
delimiter( since strings do not end with "\0" no problem).

regarding question 2:
=====================
>2- how to open a stream on a byte ( char) array in c++ in order to
read it as text. i have a byte array i need to strip off some bytes
from and then pass it to a text stream.
an example of java code would clear things up : BufferedReader in=new
BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
data is an array of bytes (char). at the end of this statement i would
be reading text characters.that is what i meant by opening a text
stream on a byte array.can this be done in c++?
1 more thing is there a way to copy an array to another in c++ other
then looping through the array ?

thanks alot

Feb 13 '07 #5

P: n/a
On Tue, 13 Feb 2007 07:12:03 -0800, steven acer wrote:

[snip]
>Perhaps you could use std::getline() specifying '\0' as delimiter to read
your xml elements straight into std::string's. You won't confuse C++ that
way, you won't have to mess around with memory management and your
elements end up in nice C++-style strings.

i think you got me wrong .
Not necessarily. Read on.
i have a .trs(whatever) file which i want
to read and generate an xml based upon its contents.
in this file i have data separated by null characters, in java i would
have done read the contents of the trs file into a string
and then myString.split("\0") would return me an array of tokens.is
there anything similar in c++ to split a string using a
delimiter( since strings do not end with "\0" no problem).
Code snippet off the top of my head (not tested!):

#include <ifstream>
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::stringtoken;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token[i] is the i-th token from your file.

[snip]

--
Lionel B
Feb 13 '07 #6

P: n/a
#include <ifstream>
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::stringtoken;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token[i] is the i-th token from your file.
all right i was not that far , but what about consecutive nulls like
'\0\0' ?

i've googled for a c++ xml parser and found apache's xercesc-c++
(kinda like SAX), but it just parses xml as i've seen,do you know of
any that constructs or transforms xml ?

one more silly question - *sorry found nothing about it on the web* -
how to make c++ ( or the preprocessor) find the following include
#include <xercesc/dom/DOM.hpp- or any other
does it have to point to some file on my filesystem , how does c++
look for it?
and regarding byte arrays, any suggestions ?
many thanks
Feb 13 '07 #7

P: n/a
steven acer wrote:
>
i've googled for a c++ xml parser and found apache's xercesc-c++
(kinda like SAX), but it just parses xml as i've seen,do you know of
any that constructs or transforms xml ?
I though xerces did that too.
>
one more silly question - *sorry found nothing about it on the web* -
how to make c++ ( or the preprocessor) find the following include
#include <xercesc/dom/DOM.hpp- or any other
does it have to point to some file on my filesystem , how does c++
look for it?
C++ does not specify how the file is found. That depends on your
compiler. Consult your compiler docs.
and regarding byte arrays, any suggestions ?
In C++ you would use an std::istringstream which is a stream that reads
from a string, you would need to convert you byte array into a string
first. Simple enough unless you have character encoding issues (you seem
to use byte and char interchangeably so I'm not sure exactly what you need).

>
many thanks

john

Feb 13 '07 #8

P: n/a
On Tue, 13 Feb 2007 08:46:54 -0800, steven acer wrote:
> #include <ifstream>
#include <string>
#include <vector>

ifstream ifs(your_trs_file);
if (!ifs) ... // handle file open failure

std::string str;
std::vector<std::stringtoken;
while (!getline(ifs,str,'\0')) token.push_back(str);

now token[i] is the i-th token from your file.

all right i was not that far , but what about consecutive nulls like
'\0\0' ?
Well, how do you want to treat them? If there's nothing between delimiters
('\0' in this case) you will end up with a token comprising an empty
string; i.e. a string containing no characters, or of length zero, if you
like.

[...]
and regarding byte arrays, any suggestions ?
I'ts not clear to me what you want "byte arrays" *for* so it's difficult
to suggest anything... C++ certainly has arrays and I suspect that "byte"
probably translates to the C++ "char" type. But if all you want is
something that behaves like a string of characters then std::string is the
way to go. If you want to do formatted I/O with strings then you might
look at std::stringstream.

--
Lionel B
Feb 13 '07 #9

P: n/a
I though xerces did that too.
can you please show me a small example of writing xml to a file.
C++ does not specify how the file is found. That depends on your
compiler. Consult your compiler docs.
i did . i'm usig g++ for windows.it says to use the -
I<include_files_diretoryoption while compiling.
but it did not work !
it just could not find the files
i have in my header file
#include <xerces/dom/DOM.hpp>
on the command line typing : -Ic:\includes as an option for g++ never
works though the hierarchy xerces\dom\DOM.hpp is in c:\includes.
im starting to think i need an IDE for this small task.
do you know of any free IDE that provies intelliscence help, i'm using
the CDT plugin for eclipse but it's ot much of an environment.

Feb 14 '07 #10

P: n/a
Geo
i've googled for a c++ xml parser and found apache's xercesc-c++
(kinda like SAX), but it just parses xml as i've seen,do you know of
any that constructs or transforms xml ?
This is a simple XML reader/writer (though I've only used it for
reading) it''s free and the author is open to suggestions for fixes/
new features, very easy to use.

http://www.applied-mathematics.net/tools/xmlParser.html

Feb 14 '07 #11

P: n/a
On Wed, 14 Feb 2007 04:17:15 -0800, steven acer wrote:

[...]
>C++ does not specify how the file is found. That depends on your
compiler. Consult your compiler docs.

i did . i'm usig g++ for windows.it says to use the -
I<include_files_diretoryoption while compiling.
but it did not work !
it just could not find the files
i have in my header file
#include <xerces/dom/DOM.hpp>
on the command line typing : -Ic:\includes as an option for g++ never
works though the hierarchy xerces\dom\DOM.hpp is in c:\includes.
Maybe try specifying -Ic:/includes (with forward slash); I think g++ may
be happier with Unix-style paths.
im starting to think i need an IDE for this small task.
I suspect that might end up making your small task much larger...

--
Lionel B
Feb 14 '07 #12

P: n/a
steven acer wrote:
>>I though xerces did that too.

can you please show me a small example of writing xml to a file.
The sample called CreateDOMDocument seems to do what you require.

http://xml.apache.org/xerces-c/samples.html

There's an alternative to Xerces on this site, claims to be smaller and
simpler than Xerces.

http://www.applied-mathematics.net/

No recommendation here, I haven't used EITHER of these products.

john
Feb 14 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.