📄 kxmldemo_dom.java
字号:
package com.talkweb.micp.icsp;
import javax.microedition.midlet.*;
import com.talkweb.micp.icsp.parse.*;
import java.io.*;
import org.kxml.parser.*;
import org.kxml.kdom.*;
import org.kxml.*;
import java.util.Vector;
/**
* A simple app to demonstrate pull parsing using kXML. It opens
* a file from the resource directory /res then passes the InputStream
* to a kXML parser. The application will then ask the parser for the
* next event. If the start tag of an <address> element is
* encountered then control is passed to the parseAddress function
* which will begin retrieving data and continue doing so until
* an end tag for </address> is encountered.
*
* This demonstrates how easy it is to use a pull parser by
* fragmenting node parsing between functions.
*
* @author Robert Cadena
*/
public class kXMLDemo_dom
extends javax.microedition.midlet.MIDlet {
/**
* Name of resource file we'll use later
*/
// public static final String resfile_name = "/res/address_book.xml";
public static final String resfile_name = "/res/222.smil";
Vector vecSmilRelation = null ;
int lableId = 0 ;
// Image image = Image.createImage("/res/icon.png");
/**
* Creates an XML parser and parses a file in the local store into a
* DOM tree. Then we traverse the DOM and print out its contents.
*/
public void beginParse() {
// Now we get the root element which is "address_book"
Document doc = getXMLDocment();
Element root = doc.getRootElement();
vecSmilRelation = new Vector() ;
int smilCount = 0 ;
// Since the <address> tags are children of <address_book>
// the we get the children of root and see which one is
// called <address>. We can't just say root.getElement("address")
// because it will return an exception if more than one element
// with that name exists
//int child_count = root.getChildCount();
getSmilElement(root.getElement("body"),smilCount,"lable0");
System.out.println("vecSmilRelation parse start.........") ;
for (int i=0 ; i<vecSmilRelation.size(); i++){
SmilRelation sEle = (SmilRelation)vecSmilRelation.elementAt(i);
System.out.println("id="+sEle.getId()+
" lable="+sEle.getLable() +
" parentId="+sEle.getParentId()+
" regionId="+sEle.getRegionId()) ;
}
}
public void getSmilElement(Element element,
int smilCount,String parentId){
//Element root = doc.getRootElement();
// Since the <address> tags are children of <address_book>
// the we get the children of root and see which one is
// called <address>. We can't just say root.getElement("address")
// because it will return an exception if more than one element
// with that name exists
int child_count = element.getChildCount();
if (child_count <= 0) return;
System.out.println("===="+element.getName()+"======");
for (int j = 0; j < child_count; j++) {
// System.out.println(j);
// if it's not an element tag then skip it
if (element.getType(j) != Xml.ELEMENT) {
continue;
}
Element item = element.getElement(j);
System.out.println(item.getName() + "+++" + "parent:" +
item.getParent().getName()+
" This_hashCode:" + item.hashCode() +
" parent_ID:" + String.valueOf(smilCount)+
" Parent_hashCode:"+ String.valueOf(item.getParent().hashCode()));
lableId += 1;
SmilRelation smilRelation = new SmilRelation();
smilRelation.setId(item.hashCode());
smilRelation.setLable(item.getName());
smilRelation.setParentId(item.getParentElement().hashCode());
if (item.getChildCount() <= 0 ){
//smilRelation.setRegionId(item.getAttribute(0).getValue());
smilRelation.setRegionId(getAttribute(item));
}else{
smilRelation.setRegionId("");
}
vecSmilRelation.addElement(smilRelation);
if (item.getChildCount() > 0) {
smilCount += 1;
getSmilElement(item,smilCount,parentId);
}
item = null;
}
System.out.println("===="+element.getName()+"**smilCount:"+smilCount+"======");
}
public String getAttribute(Element lableElement){
Vector vecAttr = lableElement.getAttributes() ;
for (int j=0 ; j<vecAttr.size(); j++){
Attribute aEle = (Attribute) vecAttr.elementAt(j) ;
if (aEle.getName().equals("region")){
return aEle.getValue();
}
}
return "" ;
}
public Document getXMLDocment(){
XmlParser parser = null;
Document doc = new Document();
try {
InputStream in = this.getClass().getResourceAsStream(resfile_name);
InputStreamReader isr = new InputStreamReader(in);
parser = new XmlParser(isr);
doc.parse(parser);
return doc ;
}
catch (IOException ioe) {
System.err.println("XML Parsing Error: " + ioe);
ioe.printStackTrace();
return null;
}
finally{
parser = null;
doc = null;
}
}
/**
* Starts the parser
*
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
beginParse();
}
/**
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
/**
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -