Java reading XML files containing long values

J

JimmyB

In C you can write something like this

#define something = "döaslkdasö" \
"djalkjddlasdj" \
"asdölasköla" \
"ädadassdlö"

to prevent long definitions in one big line, not writing a long clause
like:
#define something = "döaslkdasö djalkjddlasdj asdölasköla lasödla ölaskdölas
ölaskdöal"

So, is it possible to write that kind of long values or XML tag sentences
into XML file and read them in Java application as a whole row?

I mean something like this:

<myKey = "<tag> this is a veeeeeeeeery " +
" loooooooooong sentence or value " +
"</tag>"


Cheers,
 
O

Oliver Wong

JimmyB said:
In C you can write something like this

#define something = "döaslkdasö" \
"djalkjddlasdj" \
"asdölasköla" \
"ädadassdlö"

Is this legal C? Wouldn't you need some sort of concatenation operator
between each string?
to prevent long definitions in one big line, not writing a long clause
like:
#define something = "döaslkdasö djalkjddlasdj asdölasköla lasödla
ölaskdölas
ölaskdöal"

So, is it possible to write that kind of long values or XML tag sentences
into XML file and read them in Java application as a whole row?

I mean something like this:

<myKey = "<tag> this is a veeeeeeeeery " +
" loooooooooong sentence or value " +
"</tag>"

This would be legal Java except for the fact that "<myKey" is not a
legal identifier. If you rename it "myKey", it should work, assuming myKey
can be assigned a string.

However, maybe I'm completely misunderstanding you, because the subject
of this post talks about *reading* XML, but you seem be *writing* strings.

- Oliver
 
R

Roedy Green

I mean something like this:

<myKey = "<tag> this is a veeeeeeeeery " +
" loooooooooong sentence or value " +
"</tag>"

I think in XML \n is treated as if it were space. At least that is how
its relatives work. That still does not give you a way of breaking up
a long string without embedded spaces.

One practical way of handling the situation would be on parsing the
file to compact the strings to get to get rid of junk whitespace
introduced through transport with XML.

/**
* Remove all spaces from a String. see also condense .
*
* @param s
* String to strip of blanks.
* @return String with all blanks, lead/trail/embedded removed.
*/
public static String squish ( String s )
{
if ( s == null )
{
return null;
}
s = s.trim();
if ( s.indexOf( ' ' ) < 0 )
{
return s;
}
int len = s.length();

StringBuilder b = new StringBuilder( len - 1 );
for ( int i = 0; i < len; i++ )
{
char c = s.charAt( i );
if ( c != ' ' )
{
b.append( c );
}
} // end for
return b.toString();
} // end squish
 
R

Roedy Green

Is this legal C? Wouldn't you need some sort of concatenation operator
between each string?

in C it is as if the line breaks were not there. So you don't need an
operator.
 
O

Oliver Wong

Roedy Green said:
in C it is as if the line breaks were not there. So you don't need an
operator.

I'm aware of the usage of \ to "escape" line breaks, but wouldn't code
like:

<C code>
something = "a" \
"b"
</C code>

be seen by the compiler (after the pre-processor has had its way with
it) as:

<C code>
something = "a" "b"
</C code>

Or does the \ actually remove the white space indendation and pairs of
quotation marks as well?

- Oliver
 
G

~Glynne

Oliver said:
I'm aware of the usage of \ to "escape" line breaks, but wouldn't code
like:

<C code>
something = "a" \
"b"
</C code>

be seen by the compiler (after the pre-processor has had its way with
it) as:

<C code>
something = "a" "b"
</C code>
Yes.

Or does the \ actually remove the white space indendation and pairs of
quotation marks as well?
The C compiler concatenates adjacent string literals into a single
string literal....but this "trick" will not work at run time, i.e. with
string variables.

~Glynne
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top