当前位置: 代码迷 >> J2ME >> 小弟我想用j2me 读取xml的数据,可以给一些代码嘛?小弟我用的是netbean开发工具的!
  详细解决方案

小弟我想用j2me 读取xml的数据,可以给一些代码嘛?小弟我用的是netbean开发工具的!

热度:6454   发布时间:2013-02-25 21:34:55.0
我想用j2me 读取xml的数据,可以给一些代码嘛?我用的是netbean开发工具的!在线等,急急急
我想用j2me 读取xml的数据,可以给一些代码嘛?我用的是netbean开发工具的

------解决方案--------------------------------------------------------
<result>
<resultCode></resultCode>
<errorException></errorException>
<args>
<getResearchList currentPage=”” totalPage=””>
<item researchId=”” researchTitle=””/>
<item researchId=”” researchTitle=”” />
</getResearchList>
</args>
</result>
 
//-------------------------------------------------------------------------------
String str = 上述形式的XML格式字符串;
byte[] bt = str.getBytes("UTF-8");
ByteArrayInputStream bas = new ByteArrayInputStream(bt);
InputStreamReader isr = new InputStreamReader(bas);
Document doc = new Document();
XmlParser parser = new XmlParser(isr);
doc.parse(parser);

Element root = doc.getRootElement();
int child_count = root.getChildCount();
for (int i = 0; i < child_count; i++) {
Element kid = root.getElement(i);
if (kid.getName().equals("resultCode")) {
resultCode = kid.getText();
}

if (kid.getName().equals("args")) {
int address_item_count = kid.getChildCount();
for (int j = 0; j < address_item_count; j++) {
Element gid = kid.getElement(j);
if (gid.getName().equals("GetResearchList")) {

String currentPage = gid.getAttribute("currentPage").getValue();
totalPage = gid.getAttribute("totalPage").getValue();
Page page = new Page();
page.setCurrentPage(Integer.parseInt(currentPage));
page.setTotalPage(Integer.parseInt(totalPage));
hashtable.put("page",page);
int items = gid.getChildCount();

for(int k=0; k < items;k++){

Element cid = gid.getElement(k);

String researchId = cid.getAttribute("researchId").getValue();

researchTitle = cid.getAttribute("researchTitle").getValue();

CheckQuestionItem checkQuestionItem = new CheckQuestionItem();

checkQuestionItem.setResearchId(researchId);

checkQuestionItem.setResearchTitle(researchTitle);

CheckRequestList.addElement(checkQuestionItem);

}
hashtable.put("CheckRequestList",CheckRequestList);
}
}
}
} else {
if (kid.getName().equals("errorException")) {
errorException = kid.getText();
CheckQuestionItem checkQuestionItem = new CheckQuestionItem();
checkQuestionItem.setResearchId("error");
checkQuestionItem.setResearchTitle(errorException);
CheckRequestList.addElement(checkQuestionItem);
hashtable.put("CheckRequestList",CheckRequestList);
}
}
}
------解决方案--------------------------------------------------------
上面应该用的是一个简单的 文件 
public class XmlReader {
public final static int START_DOCUMENT = 0;
public final static int END_DOCUMENT = 1;
public final static int START_TAG = 2;
public final static int END_TAG = 3;
public final static int TEXT = 4;

final static int CDSECT = 5;
final static int ENTITY_REF = 6;

static final private String UNEXPECTED_EOF =
"Unexpected EOF";
static final private int LEGACY = 999;

// general

public boolean relaxed;
private Hashtable entityMap;
private int depth;
private String[] elementStack = new String[4];