Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java me - XML parsing not working on android build of lwuit app

I use Kxml2 parser to parse the xml response I get as a response from a remote page. It is for user authentication, and the returned xml gives several details about the user. The app is built with LWUIT 1.5 and it works on MIDP and Blackberry versions. On the Android version it doesn't work. Is there any extra specification I am supposed to add, for Android to work properly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a XMLParser in LWUIT, is very easy to use. You can try to use it. It works fine in my MIDP and BB apps. I can't test it in Android but I think It could be the solution. How do you port your LWUIT apps to Android?

ADD

Well I will try to explain the use of this Parser the best that I can

You need to import this packages:

import com.sun.lwuit.xml.Element;
import com.sun.lwuit.xml.XMLParser;

In my project I extract a XML from an HTTPConnection.

Suppose out XML is as follows:

  <?xml version="1.0" encoding="UTF-8"?>
    <xml>
     <TagFromXML>
       <child>
         <data>HI</data>
       </child>
     </TagFromXML>
    <TagFromXML>
      <child>
         <data>HI2</data>
       </child>
     </TagFromXML>
    </xml>

Then do the following:

InputStream is = hc.openInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
XMLParser myParser = new XMLParser();
Element e = myParser.parse(isr);
Vector tagVector = e.getChildrenByTagName("TagFromXML");
for(int i = 0; i<tagVector.size();i++){
 Element singleTag = (Element) tagVector.elementAt(i);
 Element child = (Element) (singleTag.getChildrenByTagName("child")).elementAt(0);
 Element data = (Element) (child.getChildrenByTagName("data")).elementAt(0);
 System.out.println("DATA: " + data.getChildAt(0).getText());
}

You will get

HI

HI2

I hope this wroks for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...