2

我有一个 J2ME 应用程序,我需要在 J2ME 中绑定我的 XML 响应。在这种情况下您能帮我吗?如何在 J2ME 中绑定 XML 数据绑定?

4

2 回答 2

0

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).

于 2012-09-20T13:25:20.703 回答
0

您似乎想要将 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);
于 2012-09-20T16:21:56.643 回答