首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

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

2012-01-13 
我想用j2me 读取xml的数据,可以给一些代码嘛?我用的是netbean开发工具的!在线等,急急急我想用j2me 读取xml

我想用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];



// source

private Reader reader;

private char[] srcBuf =
new char[Runtime.getRuntime().freeMemory() >= 1048576
? 8192
: 128];

private int srcPos;
private int srcCount;

private boolean eof;

private int line;
private int column;

private int peek0;
private int peek1;

// txtbuffer

private char[] txtBuf = new char[128];
private int txtPos;

// Event-related

private int type;
private String text;
private boolean isWhitespace;
private String name;

private boolean degenerated;
private int attributeCount;
private String[] attributes = new String[16];

private String[] TYPES =
{"Start Document",
"End Document",
"Start Tag",
"End Tag",
"Text"};

private final int read() throws IOException {

int r = peek0;
peek0 = peek1;

if (peek0 == -1) {
eof = true;
return r;
} else if (r == '\n' || r == '\r') {
line++;
column = 0;
if (r == '\r' && peek0 == '\n')
peek0 = 0;
}
column++;

if (srcPos >= srcCount) {
srcCount = reader.read(srcBuf, 0, srcBuf.length);
if (srcCount <= 0) {
peek1 = -1;
return r;
}
srcPos = 0;
}

peek1 = srcBuf[srcPos++];
return r;
}

private final void exception(String desc)
throws IOException {
throw new IOException(
desc + " pos: " + getPositionDescription());
}

private final void push(int c) {
if (c == 0)
return;

if (txtPos == txtBuf.length) {
char[] bigger = new char[txtPos * 4 / 3 + 4];
System.arraycopy(txtBuf, 0, bigger, 0, txtPos);
txtBuf = bigger;
}

txtBuf[txtPos++] = (char) c;
}

private final void read(char c) throws IOException {
if (read() != c) {
if (relaxed) {
if (c <= 32) {
skip();
read();
}
} else {
exception("expected: '" + c + "'");
}
}
}

private final void skip() throws IOException {

while (!eof && peek0 <= ' ')
read();
}

private final String pop(int pos) {
String result = new String(txtBuf, pos, txtPos - pos);
txtPos = pos;
return result;
}

private final String readName() throws IOException {

int pos = txtPos;
int c = peek0;
if ((c < 'a' || c > 'z')
&& (c < 'A' || c > 'Z')
&& c != '_'
&& c != ':'
&& !relaxed)
exception("name expected");

do {
push(read());
c = peek0;
}
while ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '_'
|| c == '-'
|| c == ':'
|| c == '.');

return pop(pos);
}

private final void parseLegacy(boolean push)


throws IOException {

String req = "";
int term;

read(); // <
int c = read();

if (c == '?') {
term = '?';
} else if (c == '!') {
if (peek0 == '-') {
req = "--";
term = '-';
} else {
req = "DOCTYPE";
term = -1;
}
} else {
if (c != '[')
exception("cantreachme: " + c);
req = "CDATA[";
term = ']';
}

for (int i = 0; i < req.length(); i++)
read(req.charAt(i));

if (term == -1)
parseDoctype();
else {
while (true) {
if (eof)
exception(UNEXPECTED_EOF);

c = read();
if (push)
push(c);

if ((term == '?' || c == term)
&& peek0 == term
&& peek1 == '>')
break;
}
read();
read();

if (push && term != '?')
pop(txtPos - 1);
}
}
private final void parseDoctype() throws IOException {

int nesting = 1;

while (true) {
int i = read();
switch (i) {

case -1 :
exception(UNEXPECTED_EOF);

case '<' :
nesting++;
break;

case '>' :
if ((--nesting) == 0)
return;
break;
}
}
}
[解决办法]
建议用KXML2.jar包

热点排行