Cześć,
Mam taki mały problem z parsowaniem XML. Po wpisaniu danego Stringa, ma wyświetlić jego wartość.
U mnie jest jednak błąd, bo wyświetla wszystkie wartości przypisane do "RBC, HGB, HCT, LYM, PLT or OB", po wpisaniu obojetnie jakiego Stringa.
Wiecie może co tu jest źle?
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.Scanner;
public class ReadXMLFile extends DefaultHandler {
public static String userChoice;
public static String PATH = "/Users/szymo/Desktop/labDataExample.xml";
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Select one of : RBC, HGB, HCT, LYM, PLT or OB");
Scanner s = new Scanner(System.in);
userChoice = s.nextLine();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (userChoice) {
case "RBC": System.out.println("Value of RBC is " + attributes.getValue("value") + ".");
break;
case "HGB": System.out.println("Value of HGB is " + attributes.getValue("value") + ".");
break;
case "HCT": System.out.println("Value of HCT is " + attributes.getValue("value") + ".");
break;
case "LYM": System.out.println("Value of LYM is " + attributes.getValue("value") + ".");
break;
case "PLT": System.out.println("Value of PLT is " + attributes.getValue("value") + ".");
break;
case "OB": System.out.println("Value of OB is " + attributes.getValue("value") + ".");
break;
}
}
};
saxParser.parse(PATH, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}