This page was last modified 12:24, 14 May 2008.
How to parse an XML file in J2ME with kXML
From Forum Nokia Wiki
We'll see how to parse a generic XML file using kXML library.
Source code: XmlNode class
First, we'll define an XmlNode class to represent a generic XmlNode. We'll define 2 node types:
- Text nodes: nodes without a name, containing only a textual value
- Element nodes: nodes with a tag name, containing children nodes
Each node will have also a list of attributes.
public class XmlNode { public static final int TEXT_NODE = 0; public static final int ELEMENT_NODE = 1; public int nodeType = 0; public String nodeName = null; public String nodeValue = null; public Vector children = null; public Hashtable attributes = null; public XmlNode(int nodeType) { this.nodeType = nodeType; this.children = new Vector(); this.attributes = new Hashtable(); } public String[] getAttributeNames() { String[] names = new String[attributes.size()]; Enumeration e = attributes.keys(); int i = 0; while(e.hasMoreElements()) { names[i] = (String)e.nextElement(); i++; } return names; } public void setAttribute(String key, String value) { attributes.put(key, value); } public String getAttribute(String key) { return (String)attributes.get(key); } public void addChild(XmlNode child) { this.children.addElement(child); } }
Source code: GenericXmlParser class
GenericXmlParser is the class with which we'll able to parse XML files. It has only one public method, parseXML(), that will accept as arguments:
- a KXmlParser instance to do parsing
- a boolean that defines if whitespaces-only children must be ignored
import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; public class GenericXmlParser { public XmlNode parseXML(KXmlParser parser, boolean ignoreWhitespaces) throws Exception { parser.next(); return _parse(parser, ignoreWhitespaces); } XmlNode _parse(KXmlParser parser, boolean ignoreWhitespaces) throws Exception { XmlNode node = new XmlNode(XmlNode.ELEMENT_NODE); if(parser.getEventType() != XmlPullParser.START_TAG) { throw new Exception("Illegal XML state: " + parser.getName() + ", " + parser.getEventType()); } else { node.nodeName = parser.getName(); for(int i = 0; i < parser.getAttributeCount(); i++) { node.setAttribute(parser.getAttributeName(i), parser.getAttributeValue(i)); } parser.next(); while(parser.getEventType() != XmlPullParser.END_TAG) { if(parser.getEventType() == XmlPullParser.START_TAG) { node.addChild(_parse(parser, ignoreWhitespaces)); } else if(parser.getEventType() == XmlPullParser.TEXT) { if(!ignoreWhitespaces || !parser.isWhitespace()) { XmlNode child = new XmlNode(XmlNode.TEXT_NODE); child.nodeValue = parser.getText(); node.addChild(child); } } parser.next(); } } return node; } }
Source code: sample usage
To use the classes defined above, you can simply do as follows:
InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/test3.xml")); KXmlParser parser = new KXmlParser(); parser.setInput(reader); GenericXmlParser gParser = new GenericXmlParser(); XmlNode xml = gParser.parseXML(parser, true);
And, if you want to dump out the parsed XML data, you can use the following method:
void dumpXML(XmlNode node, int deep) { for(int i = 0; i < deep; i++) { System.out.print(" "); } System.out.print(node.nodeName + " - "); if(node.nodeValue != null) { System.out.print("(" + node.nodeValue + ") - "); } String[] attributes = node.getAttributeNames(); for(int i = 0; i < attributes.length; i++) { System.out.print(attributes[i] + ": " + node.getAttribute(attributes[i]) + ", "); } System.out.println(); for(int i = 0; i < node.children.size(); i++) { dumpXML((XmlNode)node.children.elementAt(i), deep + 1); } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| N95 don't parse XML properly | rcaboni | Mobile Java General | 1 | 2008-03-20 10:42 |
| Parsing xml file on nokia 6630 | Transalp | Mobile Java General | 0 | 2008-04-25 16:00 |
| why the error happen in framework?(listbox app) | isarc | General Symbian C++ | 10 | 2007-05-29 08:50 |
| How2get Pixel resolution of Nokia Mobile Browser? | DancingWave | Browsing and Mark-ups | 6 | 2007-04-23 06:31 |
| FileConnection Error in J2ME Polish | divyas | Mobile Java General | 4 | 2006-08-21 23:52 |
