📄 xmlparse.java
字号:
package com.zxy.j2me.utils;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Vector;
import java.io.IOException;
import java.util.Stack;
import com.zxy.j2me.utils.ArrayList;
/**
* <p>Title: XmlParse</p>
*
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author 赵欣祎
* @version 1.0
* @since MIDP 2.0
* @since CLDC 1.0
*/
public class XmlParse {
public Hashtable table = new Hashtable();
private Node rootNode;
public void parse(InputStream ins) throws Exception {
if (ins == null) {
throw new IOException("null InputStream");
} else {
System.out.println("total memory:" + Runtime.getRuntime().freeMemory() + " free memory:" + Runtime.getRuntime().freeMemory());
InputStreamReader reader = new InputStreamReader(ins, "UTF-8");
XmlReader xmlReader = new XmlReader(reader);
xmlReader.next();
Stack pathStack = new Stack();
Stack nodeStack = new Stack();
String currentTag;
StringBuffer path = new StringBuffer();
String text;
Node node = new Node();
rootNode = node;
node.name = "root";
path.append(node.name);
nodeStack.push(node);
int count = 0;
while (xmlReader.next() != XmlReader.END_DOCUMENT) {
// System.out.println((count++) + ". total memory:" + Runtime.getRuntime().freeMemory() + " free memory:" + Runtime.getRuntime().freeMemory() +
// " stack size:" + pathStack.size() + " | " + nodeStack.size());
if (xmlReader.getType() == XmlReader.START_TAG) {
currentTag = xmlReader.getName();
pathStack.push(currentTag);
path.append(".");
path.append(currentTag);
Node pNode = (Node) nodeStack.lastElement();
node = new Node();
node.name = currentTag;
node.parentNode = pNode;
node.parentNode.childNodes.append(node);
String currentPath = path.toString();
ArrayList v;
if (table.containsKey(currentPath)) {
v = (ArrayList) table.get(currentPath);
v.append(node);
} else {
v = new ArrayList();
v.append(node);
table.put(currentPath, v);
}
nodeStack.push(node);
} else if (xmlReader.getType() == XmlReader.TEXT) {
text = xmlReader.getText().trim();
if (!text.equals("")) {
((Node) nodeStack.lastElement()).value = text;
}
} else if (xmlReader.getType() == XmlReader.END_TAG) {
currentTag = xmlReader.getName();
Object o = pathStack.pop();
o = null;
o = nodeStack.pop();
o = null;
path.setLength(0);
path.append("root");
for (int i = 0; i < pathStack.size(); i++) {
path.append(".");
path.append(pathStack.elementAt(i));
}
}
}
pathStack.removeAllElements();
nodeStack.removeAllElements();
pathStack = null;
nodeStack = null;
currentTag = null;
path = null;
text = null;
node = null;
xmlReader = null;
reader.close();
reader = null;
}
}
private class PathInfo {
int tableIndex;
int columnIndex;
int columns;
int stackDepth = -1;
}
public ArrayList[] parseSax(InputStream ins, ArrayList regPaths) throws Exception {
PathInfo currentPathInfo = null;
Hashtable resultSetInfo = new Hashtable();
ArrayList[] retTables = new ArrayList[regPaths.getSize()];
boolean[] currentRowFlags = new boolean[regPaths.getSize()];
int[] rowStackDepths = new int[regPaths.getSize()];
boolean textReady = false;
for (int i = 0; i < regPaths.getSize(); i++) {
String[] paths = null;
try {
paths = (String[]) regPaths.getValue(i);
} catch (Exception ex) {
throw new ClassCastException("regPaths format error, element must be array of String");
}
for (int j = 0; j < paths.length; j++) {
PathInfo pi = new PathInfo();
pi.tableIndex = i;
pi.columnIndex = j;
pi.columns = paths.length;
if (resultSetInfo.containsKey("root." + paths[j])) {
throw new Exception("duplicate path");
}
resultSetInfo.put("root." + paths[j], pi);
}
retTables[i] = new ArrayList();
}
if (ins == null) {
throw new IOException("null InputStream");
} else {
System.out.println("total memory:" + Runtime.getRuntime().freeMemory() + " free memory:" + Runtime.getRuntime().freeMemory());
InputStreamReader reader = new InputStreamReader(ins, "UTF-8");
XmlReader xmlReader = new XmlReader(reader);
xmlReader.next();
Stack pathStack = new Stack();
String currentTag;
StringBuffer path = new StringBuffer("root");
String text;
int stackDepth = 0;
while (xmlReader.next() != XmlReader.END_DOCUMENT) {
// System.out.println((count++) + ". total memory:" + Runtime.getRuntime().freeMemory() + " free memory:" + Runtime.getRuntime().freeMemory() );
if (xmlReader.getType() == XmlReader.START_TAG) {
stackDepth++;
currentTag = xmlReader.getName();
pathStack.push(currentTag);
path.append(".");
path.append(currentTag);
String currentPath = path.toString();
// System.out.print(currentPath + "---->");
// for (int i = 0; i < rowStackDepths.length; i++) {
// System.out.print(rowStackDepths[i]);
// System.out.print("-");
// }
// System.out.println();
if (resultSetInfo.containsKey(currentPath)) {
currentPathInfo = (PathInfo) (resultSetInfo.get(currentPath));
currentPathInfo.stackDepth = stackDepth - 1;
if (currentRowFlags[currentPathInfo.tableIndex] == false) {
Enumeration e = resultSetInfo.elements();
while (e.hasMoreElements()) {
PathInfo pi = (PathInfo) e.nextElement();
if (pi.tableIndex == currentPathInfo.tableIndex) {
if (rowStackDepths[pi.tableIndex] > currentPathInfo.stackDepth) {
rowStackDepths[currentPathInfo.tableIndex] = currentPathInfo.stackDepth;
}
pi.stackDepth = currentPathInfo.stackDepth;
}
}
retTables[currentPathInfo.tableIndex].append(new String[currentPathInfo.columns]);
}
currentRowFlags[currentPathInfo.tableIndex] = true;
textReady = true;
}
} else if (xmlReader.getType() == XmlReader.TEXT) {
if (textReady && currentPathInfo != null && currentRowFlags[currentPathInfo.tableIndex] == true) {
text = xmlReader.getText();
String[] s = (String[]) retTables[currentPathInfo.tableIndex].getValue(retTables[currentPathInfo.tableIndex].getSize() - 1);
s[currentPathInfo.columnIndex] = text;
textReady = false;
}
} else if (xmlReader.getType() == XmlReader.END_TAG) {
stackDepth--;
Enumeration e = resultSetInfo.elements();
while (e.hasMoreElements()) {
PathInfo pi = (PathInfo) e.nextElement();
rowStackDepths[pi.tableIndex] = Math.max(rowStackDepths[pi.tableIndex], pi.stackDepth);
}
for (int i = 0; i < rowStackDepths.length; i++) {
if (stackDepth < rowStackDepths[i]) {
currentRowFlags[i] = false;
}
}
currentTag = xmlReader.getName();
// if(!pathStack.empty()){
pathStack.pop();//}
// String ddd=pathStack.
path.setLength(0);
path.append("root");
for (int i = 0; i < pathStack.size(); i++) {
path.append(".");
path.append(pathStack.elementAt(i));
}
}
}
pathStack.removeAllElements();
pathStack = null;
currentTag = null;
path = null;
text = null;
xmlReader = null;
reader.close();
reader = null;
}
return retTables;
}
public ArrayList getNode(String path) {
ArrayList result = new ArrayList();
StringBuffer p = new StringBuffer();
ArrayList v;
p.append("root.");
p.append(path);
path = p.toString();
if (table.containsKey(path)) {
v = (ArrayList) table.get(path);
for (int i = 0; i < v.getSize(); i++) {
result.append(v.getValue(i));
}
}
return result;
}
public ArrayList getNode(Node node, String path) {
ArrayList v;
Node n = node;
ArrayList result = new ArrayList();
String parentPath = "";
while (n.parentNode != null) {
parentPath = n.name + "." + parentPath;
n = n.parentNode;
}
parentPath = "root." + parentPath;
path = parentPath + path;
if (table.containsKey(path)) {
v = (ArrayList) table.get(path);
for (int i = 0; i < v.getSize(); i++) {
n = (Node) v.getValue(i);
while (n.parentNode != null) {
if (n.parentNode == node) {
result.append(n);
break;
}
n = n.parentNode;
}
}
}
return result;
}
private void ClearNode(Node node) {
ArrayList v = node.childNodes;
if (v.getSize() == 0) {
node = null;
} else {
for (int i = 0; i < v.getSize(); i++) {
ClearNode((Node) v.getValue(i));
}
}
}
public void destroy() {
ClearNode(rootNode);
table.clear();
System.gc();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -