Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800
My Carrier: AT&T
Accepted Solution

Problems parsing XML

I started with the XML parse sample and tried to get a simple UI up that loads buttons with the value of a child node (later to go to a new screen and parse another XML).  When I parse the XML, I get the nodes, but I don't get the values.  My sample XML is generated.



Sample XML:


<?xml version="1.0" encoding="UTF-8"?>







import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import mbt.util.DataUtils;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

public class XMLViewer extends UiApplication {

  MainScreen _screen = new MainScreen();
  public static final boolean isTest = true;
  ConnectionThread _conThread;
  public final String xmlUrl;
  public static void main(String args[]) {
    XMLViewer viewer = new XMLViewer();
  public XMLViewer() {
    this.xmlUrl = "http://localhost:8080/FooXML.action";
    this._screen.setTitle("XML Viewer");
    this._screen.add(new RichTextField("Requesting update..."));
    this._screen.add(new SeparatorField());
    this._conThread = new ConnectionThread();
  public void updateField(String name, String action) {
    synchronized (Application.getEventLock()) {
      ButtonField button = new ButtonField(name,
        ButtonField.CONSUME_CLICK | ButtonField.FOCUSABLE | 
          protected boolean navigationClick(int status, int time) {
            System.out.println("Name: " + this.getLabel());
            return true;
  private class ConnectionThread extends Thread {
    public ConnectionThread() {
    public void run() {
      Document doc;
      try {
        int i,j;
        DocumentBuilderFactory docFactory = 
DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); InputStream is = DataUtils.getXMLActions(xmlUrl); if (is == null) { System.out.println("InputStream is null!"); return; } doc = docBuilder.parse(is); doc.getDocumentElement().normalize(); NodeList list = doc.getElementsByTagName("action"); for (i=0;i<list.getLength();i++) { NodeList children = list.item(i).getChildNodes(); String _name = new String(); String _url = new String(); for (j=0;j<children.getLength();j++) { if (children.item(j).getNodeType()!= Node.TEXT_NODE) { System.out.println("Item: "+j+ " has name: "+children.item(j).getNodeName()+ " has text: "+children.item(j).getTextContent() + " has value: "+children.item(j).getNodeValue()); if (children.item(j).getNodeName().
equalsIgnoreCase("name")) _name = children.item(j).getNodeValue(); if (children.item(j).getNodeName().
equalsIgnoreCase("url")) _url = children.item(j).getNodeValue(); } } updateField(_name,_url); } } catch (Exception e) { System.out.println(e.toString()); } } } }



I get this output:


Item: 1 has name: name has text: null has value: null
Item: 3 has name: url has text: null has value: null






Posts: 159
Registered: ‎08-19-2008
My Device: Not Specified

Re: Problems parsing XML

Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800
My Carrier: AT&T

Re: Problems parsing XML

Thanks! That helped!  I was looking at it as being similar to innerHTML for JS.  The text between the tag <name>Bugs</name> is actually a child node of type Node.TEXT_NODE. So I needed to get the text child node of the node <name> and then get the value.