/* 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. */ 这个必须加上
上面应该用的是一个简单的 文件
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];
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;
}
}
}
上面应该用的是一个简单的 文件
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];
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;
}
}
}
//-------------------------------------------------------------------------------
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();