?? parsexml.java
字號:
package util;/*
* parse xml file
*/
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.*;
public class ParseXML extends Hashtable {
InputStream inputStream = null;
public ParseXML() {
super();
}
public ParseXML(InputStream in) throws Exception {
try {
inputStream = in;
/* get xml string */
byte[] text = new byte[inputStream.available()];
inputStream.read(text);
String xmlText = new String(text);
/* analyse string */
if (!"<?".equals(xmlText.substring(0, 2)) || xmlText.indexOf("?>") < 2)
throw new Exception("The file is not xml File!");
xmlText = xmlText.substring(xmlText.indexOf("?>") + 2, xmlText.length());
String title = xmlText.substring(xmlText.indexOf("<") + 1, xmlText.indexOf(">"));
if ("".equals(title))
throw new Exception("The file is not xml File!");
String content = xmlText.substring(xmlText.indexOf(title) + title.length() + 1, xmlText.indexOf("</" + title + ">"));
this.put(title, parseString(content));
} catch (Exception e) {
System.out.println(e);
}
}
public Object parseString(String s) {
if (s.indexOf("</") <= 2) {/* not paramers */
return s;
} else {
Hashtable hs = new Hashtable();
while (s.indexOf("</") > 2) {
String title = s.substring(s.indexOf("<") + 1, s.indexOf(">"));
String content = s.substring(s.indexOf(title) + title.length() + 1, s.indexOf("</" + title + ">"));
hs.put(title, parseString(content));
s = s.substring(s.indexOf("</" + title + ">") + title.length() + 3, s.length());
}
return hs;
}
}
public void saveToFile(String path)
{
String xml = spellXmlString(this);
xml = "<?xml version='1.0' encording='ISO-8859-1'?>" + xml;
/* try{
FileConnection file = (FileConnection)Connector.open(path);
OutputStream out = file.openOutputStream();
out.write(xml.getBytes());
out.close();
}catch(IOException e)
{
e.printStackTrace();
}
*/
}
private String spellXmlString(Hashtable hs)
{
String xml="";
Enumeration el = hs.keys();
while(el.hasMoreElements())
{
String key = (String)el.nextElement();
xml += "<"+key+">";
if(hs.get(key) instanceof Hashtable)
{
xml += spellXmlString((Hashtable)hs.get(key));
}else
{
xml += (String)hs.get(key);
}
xml += "</"+key+">";
}
return xml;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -