So, I pass data in xml via socket. When I have run server side, I open cmd and execute command “telnet localhost 999”. In this moment new thread are started and in this thread I pass input stream in Stax parser. On cmd I input simple xml file (copy-past from file) and this data are sending to server side and parsing correctly.
Problem it is: when my parser parses the data, it continues to wait for something. I mean, data from input stream was parsed correctly and entity for this data was filled, but the program does not pass to next step. It seems like parser wait last chracter of xml, because when I have closed cmd app passes to next step (line: “String[] commands = parser.commands;”)
Here code:
<?xml version="1.0" encoding="UTF-8" ?>
<login>
<user-name>name</user-name>
<password>password</password>
</login>
@Override
public void run() {
try {
try {
InputStream is = socket.getInputStream();
boolean complete = false;
while (!complete) {
// handle command was received from client
if (is.available() == 0) {
continue;
}
Parser parser = new Parser();
parser.parseXmlStax(is);
String[] commands = parser.commands;
// if wrong format, send number of error end exit
if (!checkCommands(commands)) {
DataOutputStream oos = new DataOutputStream(
socket.getOutputStream());
oos.writeBytes("Eror. Try again");
continue;
}
public void parseXmlStax(InputStream is) throws XMLStreamException,
IOException, ParserConfigurationException, SAXException {
XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlReader = xmlFactory.createXMLStreamReader(is);
int type = xmlReader.getEventType();
try {
while (true) {
switch (type) {
case XMLStreamConstants.START_DOCUMENT:
logger.info("Stax parsing: start document");
break;
case XMLStreamConstants.START_ELEMENT:
logger.info("Stax parsing: start element");
thisElement = xmlReader.getLocalName();
break;
case XMLStreamConstants.CHARACTERS:
logger.info(String.valueOf("CHARACTERS"));
char[] ch = xmlReader.getTextCharacters();
int length = xmlReader.getTextLength();
int start = xmlReader.getTextStart();
String command = new String(ch, start, length);
if (thisElement.equalsIgnoreCase(LoginItem.LOGIN)) {
commands[0] = thisElement;
} else if (!xmlReader.isWhiteSpace()
&& thisElement
.equalsIgnoreCase(LoginItem.USER_NAME)) {
commands[1] = command;
} else if (!xmlReader.isWhiteSpace()
&& thisElement.equalsIgnoreCase(LoginItem.PASSWORD)) {
commands[2] = command;
}
if (thisElement.equalsIgnoreCase(LoginItem.LOGOUT)) {
commands[0] = thisElement;
commands[1] = command;
}
if (thisElement.equalsIgnoreCase(LoginItem.STATUS)) {
commands[0] = thisElement;
commands[1] = command;
}
break;
case XMLStreamConstants.END_ELEMENT:
logger.info("Stax parsing: end element");
break;
case XMLStreamConstants.END_DOCUMENT:
logger.info("Stax parsing: end document");
break;
}
if (!xmlReader.hasNext()) {
break;
}
type = xmlReader.next();
}
} finally {
xmlReader.close();
}
}
I solved my issue. On client side I send data without end symbol. So I only add ‘\n’ to end of my command.
It is worth solution, and I think that I know how to do it better. I should wrap my xml document in tag and send/response data until I get tag