0

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.

5
  • You say that "the second print statement doesn't get a chance to execute" - why don't you add an else clause to show if xpp.getName() is null? Commented Aug 7, 2011 at 12:24
  • But then it means that there is no node there right since null is returning? Commented Aug 7, 2011 at 12:28
  • Well, it means it's not a start/end element node, or an entity reference. It could be TEXT, or START_DOCUMENT etc. Commented Aug 7, 2011 at 12:31
  • Oh...Then how can I shift the code to make it work? Commented Aug 7, 2011 at 12:34
  • It's not really clear what you're trying to do, but you should probably be looping around until event is xpp.END_DOCUMENT. Commented Aug 7, 2011 at 12:38

1 Answer 1

3

I don't know about your XmlPullParser code, but your dom4j code is broken. You've asked the SAXReader to give you its document factory, which you're then asking for a new document, and then you're printing it. In other words, that has nothing to do with the data that's been read from stringReader. You're ignoring the result of the read call. Try this instead:

SAXReader sr = new SAXReader();
Document doc = sr.read(stringReader);
System.out.println(doc.asXML());

See whether that shows the complete XML.

I note that in the XmlPullParser code you're not looping round as you normally would - you're only reading the first node, examining it, then reassigning the value of event - and then not doing anything else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.