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

a3580737 2007-12-26 06:02:57
我想用j2me 读取xml的数据,可以给一些代码嘛?我用的是netbean开发工具的
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongdong715 2008-01-04
  • 打赏
  • 举报
回复
建议用KXML2.jar包
ping3021 2008-01-03
  • 打赏
  • 举报
回复
/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE. */ 这个必须加上
ping3021 2008-01-03
  • 打赏
  • 举报
回复
上面应该用的是一个简单的 文件
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;
}
}
}
ping3021 2008-01-03
  • 打赏
  • 举报
回复
上面应该用的是一个简单的 文件
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;
}
}
}
yihu0817 2007-12-28
  • 打赏
  • 举报
回复
<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);
}
}
}
a3580737 2007-12-28
  • 打赏
  • 举报
回复
请问高手,这个是用那一个包的?可以完整的发来嘛?是用kxml2-2.3.0.jar?

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧