快速业务通道

在J2ME开发中解析XML - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-07-04
utStream() ); kXMLElement root = new kXMLElement(); try { root.parseFromReader( doc ); } catch( kXMLParseException pe ){ } catch( IOException ie ){ }

下面是一个J2ME的应用程序简单演示了如何解析xml。如果有时间可以写写复杂的测试程序。你可以从如下地址下载源代码:XMLTest. 里面包括了kxml和nanoxml的源代码,如果想得到最新的源代码请参考他们的官方网站,在本站提供了kxml的在线API

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

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号