How to to scan a string, char by char?
Hi i have some xml data, i want to scan the string char until it sees > for the first time. Then it adds the following data until it sees <, at which point it stops. So i should get s2tidm1 from "<server>s2tidm1</server>"
The problem with the below code is that sa.next() gives me the whole string as there is no space, is there a sa.nextChar() or something similar?
Code following -- Thanks Peter
import java.io.*;
import java.util.Scanner;
import java.util.*;
publicclass xmltestmod{
static String data ="<server>s2tidm1</server>";
publicstaticvoid main(String args[]){
Scanner sa =new Scanner(data);
String pete =" ";
String temp =" ";
while (sa.hasNext()){
temp = sa.next();
System.out.println(temp);
if (temp.contains(">")){
while (true){
System.out.println("Entering if");
temp = sa.next();
pete = pete + temp;
if(temp.contains("<"))break;
}
}
}
System.out.println(pete);
sa.close();
}
}
[2045 byte] By [
cabbagesa] at [2007-11-26 18:53:50]

Scanner is a tokenizer, not suited to character by character analysis.There are excellent classes for reading XML documents in the standard library, but if you want to do your own lexical analysis, read the stuff in as a String, and use indexOf() and substring() methods.
the problem with some on the xml api is that they require you to know the full quantifier.
I got muiltiple xml lines in a array, i want to loop the array and strip out the tags so only the data is left. Thats why i didn't use the xml apis out there as you need to say starttag = "<Server>";, endtag = "/Server>", but i just want to say store all data inbetween < and >, that way i can loop through the whole array.
Can you explain abit more about hte substring and index of part please.
[url=http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#charAt(int)]String.charAt(int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#indexOf(int)]String.indexOf(int)[/url]
[url=http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int,%20int)]String.substring(int,int)[/url]
mlka at 2007-7-9 6:27:52 >

>
> I got muiltiple xml lines in a array, i want to loop
> the array and strip out the tags so only the data is
> left. Thats why i didn't use the xml apis out there
> as you need to say starttag = "<Server>";, endtag =
> "/Server>", but i just want to say store all data
> inbetween < and >, that way i can loop through the
> whole array.
>
Actually if you use the SAX parser, rather than the DOM parser, it will call a method of your chosing for each text element, and you can just ignore the callbacks for tags etc. if you wish.
> Can you explain abit more about hte substring and
> index of part please.
Check the Javadocs. Substring chops part of a string out, indexOf finds the first occurance of a character.
Thanks Guys used the stubstring instead, but i had to say "</" instead as the indexOf was giving me the first >< instead of the indexOf the last <. is there a way of doing that, telling it to give me the index of the last <?
public class xmltestmod {
static String data = "<server111>s2tidm1</server>";
public static void main(String args[]) {
int start = data.indexOf (">");
int end = data.indexOf ("</");
System.out.println("Start = " +start + "end + " + end);
String Peter = data.substring(start+1, end);
System.out.println(Peter);
}
}>