I am trying to get XML data from a MIDP device to a Servlet. Servlet is obtaining the Data as follows -
DataInputStream dis = new DataInputStream((InputStream) request.getInputStream());
String readUTF = dis.readUTF();
After getting this done from MIDP I thought it was over. But now I am having trouble converting the readUTF to a InputStream. I want to parse the xml string. I used kXML 2.3.0 and here is the code -
XmlPullParser xpp = new KXmlParser();
try {
xpp.setInput(new InputStreamReader(new ByteArrayInputStream(readUTF.getBytes("UTF-8"))));
int event = xpp.getEventType();
if (event != xpp.END_DOCUMENT) {
System.out.println("Inside Document");
if (xpp.getName() != null) {
System.out.println("The tag is not null");
if (xpp.getName().equals("title")) {
System.out.println("Title =" + xpp.getText());
} else if (xpp.getName().equals("note")) {
System.out.println("Note =" + xpp.getText());
} else if (xpp.getName().equals("priority")) {
System.out.println("Priority =" + xpp.getText());
}
}
event = xpp.next();
}
}
The problem I am having is that only Inside Document is ever printed. That means that the second print statement doesn't get a chance to execute. I used Dom4j for the same thing.
SAXReader sr = new SAXReader();
sr.read(stringReader);
System.out.println(sr.getDocumentFactory().createDocument().asXML());
The result is something is this just this -
INFO: <?xml version="1.0" encoding="UTF-8"?>
Original string is - <?xml version='1.0' encoding='UTF-8' ?> <data> <task><title>dsfsdfds</title><note>null</note><priority>High</priority></task><task><title>sdfsdfdsf</title><note>null</note><priority>High</priority></task> </data>
I validated XML for it and it works perfectly. Is there a problem for converting UTF-8 to InputStreams? And is there another way to parse my XML String to data.
elseclause to show ifxpp.getName()is null?eventisxpp.END_DOCUMENT.