WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程在以CLDC為基礎的架構上分析XML

在以CLDC為基礎的架構上分析XML
作者:不详  来源:本站整理  发布时间:2005-11-29 12:10:16
的 NanoXML 版本,能夠與 CLDC 並存 (原本的 NanoXML 只適合以 J2SE 為基礎的系統) 可以在 http://www.ericgiguere.com/nanoxml 找到。就像 kXML,必須將 NanoXML 包含到您的程式中,然後新增下面的引入陳序到您的程式:

import nanoxml.*;

去分析一份文件,產生一個 kXMLElement 類別的實體,並且選擇使用 parseFromReader、parseString 或 parseCharArray 其中之一:

由於 NanoXML 是一個單一步驟的分析器,它分析全部的文件,並且轉換它成為一個樹狀的XML元件。樹狀圖的根部是您所產生的 kXMLElement 實體,且每一個樹狀圖的節點是另一個 kXMLElement 實體。您可以使用像是 getChildren、getTagName 和 getContents 的方法穿越這棵樹。

去示範如何分析,這裡提供一個 MIDP 程式。填滿一個字串,包含了許多 XML 的元件,這個程式使用 kXML。以 NanoXML 分析的話,將呼叫 parseUsingNanoXML 這行的註解符號 (//) 去掉,並將呼叫 parseUsingkXML 這行標記為註解。這個程式的一個完整的專案也是可以利用的 http://www.ericgiguere.com/techtips/XMLTest.zip,你可以使用 J2ME Wireless Toolkit 來執行這個專案。





package com.ericgiguere.techtips;

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import nanoxml.*;
import org.kxml.*;
import org.kxml.parser.*;

/**
* Simple MIDlet that demonstrates how an XML document can be
* parsed using kXML or NanoXML.
*/

public class XMLTest extends MIDlet {

// Our XML document -- normally this would be something you
// download.

private static String xmlDocument =
"apple" +
"orange" +
"pear
";

private Display display;
private Command exitCommand = new Command( "Exit",
Command.EXIT, 1 );


public XMLTest(){
}

protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}

protected void pauseApp(){
}

protected void startApp() throws MIDletStateChangeException {
if( display == null ){ // first time called...
initMIDlet();
}
}

private void initMIDlet(){
display = Display.getDisplay( this );

String [] items;

//items = parseUsingNanoXML( xmlDocument );
items = parseUsingkXML( xmlDocument );

display.setCurrent( new ItemList( items ) );
}

public void exitMIDlet(){
notifyDestroyed();
}

// Parses a document using NanoXML, looking for
// "item" nodes and returning their content as an
// array of strings.

private String[] parseUsingNanoXML( String xml ){
kXMLElement root = new kXMLElement();

try {
root.parseString( xml );

Vector list = root.getChildren();
Vector items = new Vector();

for( int i = 0; i < list.size(); ++i ){
kXMLElement node =
(kXMLElement) list.elementAt( i );
String tag = node.getTagName();

if( tag == null ) continue;
if( !tag.equals( "item" ) ) continue;

items.addElement( node.getContents() );
}

String[] tmp = new String[ items.size() ];
items.copyInto( tmp );
return tmp;
}
catch( kXMLParseException ke ){
return new String[]{ ke.toString() };
}
}

// Parses a document using kXML, looking for "item"
// nodes and returning their content as an
// array of strings.

private String[] parseUsingkXML( String xml ){
try {
ByteArrayInputStream bin =
new ByteArrayInputStream(
xml.getBytes() );
InputStreamReader in = new InputStreamReader( bin );
XmlParser parser = new XmlParser( in );
Vector items = new Vector();

parsekXMLItems( parser, items );

String[] tmp = new String[ items.size() ];
items.copyInto( tmp );

return tmp;
}
catch( IOException e ){
return new String[]{ e.toString() };
}
}

private void parsekXMLItems( XmlParser parser, Vector items )
throws IOException {
boolean inItem = false;

while( true ){
ParseEvent event = parser.read();
switch( event.getType() ){
case Xml.START_TAG:
if( event.getName().equals( "item" ) ){
inItem = true;
}
break;
case Xml.END_TAG:
if( event.getName().equals( "item" ) ){
inItem = false;
}
break;
case Xml.TEXT:
if( inItem ){
items.addElement( event.getText() );

}
break;
case Xml.END_DOCUMENT:
return;
}
}
}

// Simple List UI component for displaying the list of
// items parsed from the XML document.

class ItemList extends List implements CommandListener {

ItemList( String[] list ){
super( "Items", IMPLICIT, list, null );
addCommand( exitCommand );
setCommandListener( this );
}

public void commandAction( Command c, Displayable d ){
if( c == exitCommand ){
exitMIDlet();
}
}
}
}



上一页  [1] [2] 

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码