Extracting a string

M

Mar Thomas

Here's my question

How can I retrieve only stuff1's value from a processing instruction?

<?target stuff1="1-100" stuff2="200.0 ?>
 
S

Sergio Juan

Hi

The easy way (but you can only apply it if you are sure that the message
will have this format)

java.util.StringTokenizer str = new java.util.StringTokenizer("<?target
stuff1=\"1-100\" stuff2=\"200.0\" ?>", "\"");
str.nextToken(); // discard the first part of the substring, that will be
<?target stuff1=
String myString = str.nextToken(); // Here you got it.

If the String can change slightly, you may want to look for a regular
expression parser (jakarta has one called ORO, I think), or even a grammar
parser, as the solution I wrote will fail if the order of the attributes is
altered.

Regards.
 
M

Mar Thomas

Thanks..Your solution gives me an idea


Sergio Juan said:
Hi

The easy way (but you can only apply it if you are sure that the message
will have this format)

java.util.StringTokenizer str = new java.util.StringTokenizer("<?target
stuff1=\"1-100\" stuff2=\"200.0\" ?>", "\"");
str.nextToken(); // discard the first part of the substring, that will be
<?target stuff1=
String myString = str.nextToken(); // Here you got it.

If the String can change slightly, you may want to look for a regular
expression parser (jakarta has one called ORO, I think), or even a grammar
parser, as the solution I wrote will fail if the order of the attributes is
altered.

Regards.
 
J

John C. Bollinger

Mar said:
Here's my question

How can I retrieve only stuff1's value from a processing instruction?

<?target stuff1="1-100" stuff2="200.0 ?>

Using DOM? Using SAX? Parsing it with custom code?

And what exactly do you mean by "retrieve only stuff1's value"? As
opposed to both stuff1's and stuff2's? Or as opposed to stuff1's name
and value? Or as opposed to some other alternative?

If you have an implementation now that isn't working as you'd like then
it would be very helpful if you would show the code.


John Bollinger
(e-mail address removed)
 
V

VisionSet

Mar Thomas said:
Here's my question

How can I retrieve only stuff1's value from a processing instruction?

<?target stuff1="1-100" stuff2="200.0 ?>

Or the regex alternative:

String input = "<?target stuff1=\"1-100\" stuff2=\"200.0 ?>";

Matcher matcher =
Pattern.compile(".*stuff1=\"([^\"]*)\".*").matcher(input);

matcher.matches();

System.out.println(matcher.group(1));
 
M

Mar Thomas

John C. Bollinger said:
Using DOM? Using SAX? Parsing it with custom code?

Not with DOM or SAX.
And what exactly do you mean by "retrieve only stuff1's value"? As
opposed to both stuff1's and stuff2's?

Exactly what I meant. stuff1's value which in this case would be 1-100. The
reason why I use the sentence "only stuff1" does NOT mean both stuff1 and
stuff2

Or as opposed to stuff1's name
and value? Or as opposed to some other alternative?

Stuff1's value is stuff1's value not name...Does it say name anywhere in the
question?

Lastly, RTFQ
 
J

John C. Bollinger

Not with DOM or SAX.




Exactly what I meant. stuff1's value which in this case would be 1-100. The
reason why I use the sentence "only stuff1" does NOT mean both stuff1 and
stuff2

Or as opposed to stuff1's name



Stuff1's value is stuff1's value not name...Does it say name anywhere in the
question?

Lastly, RTFQ

Well, RTFR! I did not suggest that you might also want stuff1's name, I
was asking whether that was what you were specifically trying to avoid.
It is customary to at least make an attempt at solving a problem
yourself before appealing to this newsgroup for assistance, so my line
of questioning was aimed at trying to determine the nature of the
difficulty with your current code so as to give you a response well
suited to your need. In the future I'll try to remember to not make
such effort on your behalf.

For what it's worth, a regex would be a good tool for a task like this.


John Bollinger
(e-mail address removed)
 
T

Tony Morris

There are multiple XML (assuming that is what it is) parsing implementations
that use either SAX or DOM.
As of 1.4, an implementation is provided for each of these.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top