I have a J2ME Application in which i need to bind my XML response in J2ME.Will you please help me in this case?How to bind XML Data Binding in J2ME?
-
Is that XML the response returned by a SOAP WS? – Mister Smith Sep 20 '12 at 13:10
-
No,its response obtained by Http Get/Post method. – user1520655 Sep 20 '12 at 13:17
-
Are you talking about using this: import org.xmlpull.v1.XmlPullParser – user1520655 Sep 20 '12 at 13:22
2 Answers
JiBX seems to support J2ME. See the following related JIRA issue: [#JIBX-110] Having a J2ME compatible official jibx release.
Once downloaded, you'll have to ant-build the j2me jars using the j2me
target (ant j2me
from the build
directory where build.xml
is sitting). You can just build it with a standard javac
, no need for a specialized compiler (see this discussion in the JiBX users list).

- 27,550
- 11
- 97
- 161
It seems that what you want is to unmarshall an XML file to a Java class. If so, I have shared a generic way at my blog. It uses two classes to implement it. The code of the first class is:
public class XMLTag {
// if you do not have enough memory, use lazy
// instantiation on these attributes
private Hashtable attributes = new Hashtable();
private Vector childs = new Vector();
public void setAttributeValue(String attribute, String value) {
if (attribute != null && value != null) {
attributes.put(attribute, value);
}
}
public String getAttributeValue (String attribute) {
return (String) attributes.get(attribute);
}
public void addChild (XMLTag child) {
childs.addElement(child);
}
public Enumeration getChilds () {
return childs.elements();
}
public XMLTag getChildAt (int index) {
return (XMLTag) childs.elementAt(index);
}
}
Below is the source code of the second class:
class XMLBinder extends org.xml.sax.helpers.DefaultHandler {
private Hashtable map = new Hashtable();
private Stack stack = new Stack();
private XMLTag rootElement;
private String attribute;
private StringBuffer value = new StringBuffer();
/**
* @param map with String keys and XMLTag values
*/
public XMLBinder(Hashtable map) {
Enumeration e = map.keys();
while (e.hasMoreElements()) {
Object key = e.nextElement();
Object tag = map.get(key);
if (validateMapping(key, tag)) {
this.map.put(key, tag);
} else {
throw new IllegalArgumentException("key " + key);
}
}
}
private boolean validateMapping (Object key, Object tag) {
return key instanceof String
&& tag instanceof Class
&& XMLTag.class.isAssignableFrom((Class) tag);
}
public XMLTag unmarshall (InputStream in) throws IOException {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(in, this);
return rootElement;
} catch (Exception ex) {
throw new IOException("caused by " + ex);
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Class tag = (Class) map.get(qName);
if (tag != null) {
try {
XMLTag newTag = (XMLTag) tag.newInstance();
addAttributesToXMLTag(attributes, newTag);
stack.push(newTag);
} catch (Exception e) {
throw new SAXException("caused by " + e);
}
} else {
attribute = qName;
}
}
private void addAttributesToXMLTag (Attributes attributes, XMLTag newTag) {
if (attributes != null) {
for (int i = attributes.getLength() - 1; i >= 0; i--) {
String attrName = attributes.getQName(i);
String attrValue = attributes.getValue(i);
newTag.setAttributeValue(attrName, attrValue);
}
}
}
public void characters(char[] ch, int start, int length) {
if (attribute != null) {
value.append(ch, start, length);
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (stack.isEmpty()) {
throw new SAXException("no mapping for " + qName);
}
if (attribute != null && attribute.equals(qName)) {
XMLTag parent = (XMLTag) stack.peek();
parent.setAttributeValue(attribute, value.toString());
attribute = null;
value.setLength(0);
} else {
XMLTag child = (XMLTag) stack.pop();
if (stack.isEmpty() == false) {
XMLTag parent = (XMLTag) stack.peek();
parent.addChild(child);
} else {
rootElement = (XMLTag) child;
}
}
}
}
To prevent the use of Class.forName we use a map with tags and classes. The key is a String with the tag name and the value is a Class that extends XMLTag. For example, reading an RSS feed would use below classes:
class RSS extends XMLTag {
Channel channel;
public void addChild(XMLTag child) {
if (child instanceof Channel) {
channel = (Channel) child;
}
}
}
class Channel extends XMLTag {
public void addChild(XMLTag child) {
if (child instanceof Item) {
super.addChild(child);
}
}
}
class Item extends XMLTag {
}
And the following map:
Hashtable map = new Hashtable();
map.put("rss", RSS.class);
map.put("channel", Channel.class);
map.put("item", Item.class);
The binder can then be used:
XMLBinder binder = new XMLBinder(map);
rss = (RSS) binder.unmarshall(in);
Update after comments
For your xml sample you need to create the following classes:
class DataTable extends XMLTag {
XsSchema xsSchema;
DiffgrDiffgram diffgrDiffgram;
public void addChild(XMLTag child) {
if (child instanceof XsSchema) {
xsSchema = (XsSchema) child;
}
else if (child instanceof DiffgrDiffgram) {
diffgrDiffgram = (DiffgrDiffgram) child;
}
}
}
class XsSchema extends XMLTag {
}
class DiffgrDiffgram extends XMLTag {
}
and use the following map
Hashtable map = new Hashtable();
map.put("DataTable", DataTable.class);
map.put("xs:schema", XsSchema.class);
map.put("diffgr:diffgram", DiffgrDiffgram.class);

- 4,033
- 16
- 22
-
Thanx.But will you please tell me one thing;Actually i have a procedure in .Net Webservice in which i have different operators and now i am using that procedure in my J2me Application via Http GET/POST.Problem is that the response i am getting is multiple nodes XML.I have use XMLParser for rest of the responses but in this it is not applicable because it contains multiple nodes as there are so many operators.So what am i supposed to do?Do what you have suggested before or something else?Please tell me.Hope u got my point. – user1520655 Sep 21 '12 at 09:00
-
If you required then i will send you the response String?Let me know. – user1520655 Sep 21 '12 at 09:02
-
Sorry, but I did not get your point. Please, update your question with this String. – Telmo Pimentel Mota Sep 21 '12 at 11:17
-
-
-
-
-
-
this is what i obtain in my String(StrResponse) through Http GET/POST in j2me application.Now i have to parse this XML.i have used XML Parser for other responses but what should i do in this case as this XML format contains multiple Nodes? – user1520655 Sep 21 '12 at 13:05
-
Your XML contains one root tag DataTable and two childs: xs:schema and diffgr:diffgram. You can create one class for each one of them. – Telmo Pimentel Mota Sep 21 '12 at 18:16
-
Ok.Will you please elaborate what you haveanswered?Can you give any example? – user1520655 Sep 22 '12 at 10:33