Java微环境中的XML数据绑定
我有一个J2ME应用程序,其中我需要在J2ME中绑定我的XML响应。在这种情况下,请您帮助我好吗?如何在J2ME中绑定XML数据?似乎支持J2ME。请参阅以下相关JIRA问题: 一次,您必须从build.xml所在的build目录使用j2me目标ant j2me ant构建j2me JAR。您只需使用标准的javac构建它,不需要专门的编译器,请参见JiBX用户列表。似乎支持J2ME。请参阅以下相关JIRA问题:Java微环境中的XML数据绑定,java,java-me,Java,Java Me,我有一个J2ME应用程序,其中我需要在J2ME中绑定我的XML响应。在这种情况下,请您帮助我好吗?如何在J2ME中绑定XML数据?似乎支持J2ME。请参阅以下相关JIRA问题: 一次,您必须从build.xml所在的build目录使用j2me目标ant j2me ant构建j2me JAR。您只需使用标准的javac构建它,不需要专门的编译器,请参见JiBX用户列表。似乎支持J2ME。请参阅以下相关JIRA问题: 一次,您必须从build.xml所在的build目录使用j2me目标ant j2m
一次,您必须从build.xml所在的build目录使用j2me目标ant j2me ant构建j2me JAR。您只需使用标准的javac构建它,不需要专门的编译器,请参见JiBX用户列表。似乎您想要的是将XML文件解组到Java类。如果是这样的话,我在这里分享了一个通用的方法。它使用两个类来实现它。第一类的代码是:
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);
}
}
下面是第二类的源代码:
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;
}
}
}
}
为了防止使用Class.forName,我们使用带有标记和类的映射。键是一个带有标记名的字符串,值是一个扩展XMLTag的类。例如,阅读RSS提要将使用以下类:
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 {
}
及以下地图:
Hashtable map = new Hashtable();
map.put("rss", RSS.class);
map.put("channel", Channel.class);
map.put("item", Item.class);
然后可使用粘合剂:
XMLBinder binder = new XMLBinder(map);
rss = (RSS) binder.unmarshall(in);
评论后更新
对于xml示例,您需要创建以下类:
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 {
}
并使用下面的地图
Hashtable map = new Hashtable();
map.put("DataTable", DataTable.class);
map.put("xs:schema", XsSchema.class);
map.put("diffgr:diffgram", DiffgrDiffgram.class);
似乎您想要的是将XML文件解组到Java类。如果是这样的话,我在这里分享了一个通用的方法。它使用两个类来实现它。第一类的代码是:
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);
}
}
下面是第二类的源代码:
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;
}
}
}
}
为了防止使用Class.forName,我们使用带有标记和类的映射。键是一个带有标记名的字符串,值是一个扩展XMLTag的类。例如,阅读RSS提要将使用以下类:
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 {
}
及以下地图:
Hashtable map = new Hashtable();
map.put("rss", RSS.class);
map.put("channel", Channel.class);
map.put("item", Item.class);
然后可使用粘合剂:
XMLBinder binder = new XMLBinder(map);
rss = (RSS) binder.unmarshall(in);
评论后更新
对于xml示例,您需要创建以下类:
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 {
}
并使用下面的地图
Hashtable map = new Hashtable();
map.put("DataTable", DataTable.class);
map.put("xs:schema", XsSchema.class);
map.put("diffgr:diffgram", DiffgrDiffgram.class);
该XML是SOAP WS返回的响应吗?否,它的响应是通过Http Get/Post方法获得的。您是说使用以下方法:import org.xmlpull.v1.XmlPullParserIs该XML是SOAP WS返回的响应吗?否,它的响应是通过Http Get/Post方法获得的。你是说使用这个:import org.xmlpull.v1.XmlPullParserThanx。但是你能告诉我一件事吗;实际上,我在.Net Webservice中有一个过程,其中我有不同的运算符,现在我通过Http GET/POST在J2me应用程序中使用该过程。问题是,我得到的响应是多节点XML。我对其余响应使用了XMLParser,但在这里它不适用,因为它包含多个节点这么多操作员。那我该怎么办?按照你之前的建议做还是做其他事情?请告诉我。希望你明白我的意思。如果你需要,我会给你发送响应字符串?让我知道。对不起,我没有明白你的意思。请使用此字符串更新您的问题。但是你能告诉我一件事吗;实际上,我在.Net Webservice中有一个过程,其中我有不同的运算符,现在我通过Http GET/POST在J2me应用程序中使用该过程。问题是,我得到的响应是多节点XML。我对其余响应使用了XMLParser,但在这里它不适用,因为它包含多个节点这么多操作员。那我该怎么办?按照你之前的建议做还是做其他事情?请告诉我。希望你明白我的意思。如果你需要,我会给你发送响应字符串?让我知道。对不起,我没有明白你的意思。请使用此字符串更新您的问题。