If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

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

Reply
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified
Accepted Solution

Help with parsing XML (XMLParseDemo.java)

Hi Everyone,

 

I am trying to parse XML in my application. Here is a sample of what I am trying to parse:

 

 

<?xml version="1.0" standalone="yes"?>
<autocomplete>
  <url_template>http://api.netflix.com/catalog/titles/autocomplete?{-join|&amp;|term}</url_template>
  <autocomplete_item>
    <title short="Forrest Gump"></title>
  </autocomplete_item>
  <autocomplete_item>
    <title short="Forrest Landis"></title>
  </autocomplete_item>
  <autocomplete_item>
    <title short="Finding Forrester"></title>
  </autocomplete_item>
  <autocomplete_item>
    <title short="Menotti: The Medium: Maureen Forrester"></title>
  </autocomplete_item>
</autocomplete>

 

 

For the parser, I am using the XMLDemoScreen.java which is provided in the JDE samples. What I am trying to do is parse out all of the <title> element's. However, when I run the application, my output is as follows:

 

 

autocomplete
   url_template = http://api.netflix.com/catalog/titles/autocomplete?{-join|&|term}"
   autocomplete_item
      title
   autocomplete_item
      title
   autocomplete_item
      title
   autocomplete_item
      title

 

 

So, it is not printing the "title" values. Does anyone know why? Is it because the element contains <title short>?

 

Thanks for your help. Below is the code that I am using (which can be found in the JDE samples):

 

 

/*
 * XMLDemoScreen.java
 *
 * Copyright © 1998-2009 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package com.kflicks.xml;

import java.io.InputStream;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;

/**
 * The main screen for the application. Displays the results of parsing the XML
 * file.
 */
/* package */public final class XMLDemoScreen extends MainScreen {

	// Constants
	// -----------------------------------------------------------------------------------
	private static final int _tab = 4;
	InputStream input;

	/**
	 * This constructor parses the XML file into a W3C DOM document, and
	 * displays it on the screen.
	 * 
	 * @see Document
	 * @see DocumentBuilder
	 * @see DocumentBuilderFactory
	 */
	public XMLDemoScreen(InputStream input) {
		setTitle(new LabelField("XML Demo"));
		this.input = input;

		try {
			// Build a document based on the XML file.
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(input);

			// Normalize the root element of the XML document. This ensures that
			// all Text
			// nodes under the root node are put into a "normal" form, which
			// means that
			// there are neither adjacent Text nodes nor empty Text nodes in the
			// document.
			// See Node.normalize().
			Element rootElement = document.getDocumentElement();
			rootElement.normalize();

			// Display the root node and all its descendant nodes, which covers
			// the entire
			// document.
			displayNode(rootElement, 0);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * Displays a node at a specified depth, as well as all its descendants.
	 * 
	 * @param node
	 *            The node to display.
	 * @param depth
	 *            The depth of this node in the document tree.
	 */
	private void displayNode(Node node, int depth) {
		// Because we can inspect the XML file, we know that it contains only
		// XML elements
		// and text, so this algorithm is written specifically to handle these
		// cases.
		// A real-world application will be more robust, and will handle all
		// node types.
		// See the entire list in org.w3c.dom.Node.
		// The XML file is laid out such that each Element node will either have
		// one Text
		// node child (e.g. <Element>Text</Element>), or >= 1 children
		// consisting of at
		// least one Element node, and possibly some Text nodes. Start by
		// figuring out
		// what kind of node we're dealing with.
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			StringBuffer buffer = new StringBuffer();
			indentStringBuffer(buffer, depth);
			NodeList childNodes = node.getChildNodes();
			int numChildren = childNodes.getLength();
			Node firstChild = childNodes.item(0);

			// If the node has only one child and that child is a Text node,
			// then it's of
			// the form <Element>Text</Element>, so print 'Element = "Text"'.
			if (numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE) {
				buffer.append(node.getNodeName()).append(" = \"").append(
						firstChild.getNodeValue()).append('"');
				add(new RichTextField(buffer.toString()));
			} else {
				// The node either has > 1 children, or it has at least one
				// Element node child.
				// Either way, its children have to be visited. Print the name
				// of the element
				// and recurse.
				buffer.append(node.getNodeName());
				add(new RichTextField(buffer.toString()));

				// Recursively visit all this node's children.
				for (int i = 0; i < numChildren; ++i) {
					displayNode(childNodes.item(i), depth + 1);
				}
			}
		} else {
			// Node is not an Element node, so we know it is a Text node. Make
			// sure it is
			// not an "empty" Text node (normalize() doesn't consider a Text
			// node consisting
			// of only newlines and spaces to be "empty"). If it is not empty,
			// print it.
			String nodeValue = node.getNodeValue();
			if (nodeValue.trim().length() != 0) {
				StringBuffer buffer = new StringBuffer();
				indentStringBuffer(buffer, depth);
				buffer.append('"').append(nodeValue).append('"');
				add(new RichTextField(buffer.toString()));
			}
		}
	}

	/**
	 * Adds leading spaces to the provided string buffer according to the depth
	 * of the node it represents.
	 * 
	 * @param buffer
	 *            The string buffer to add leading spaces to.
	 * @param depth
	 *            The depth of the node the string buffer represents.
	 */
	private static void indentStringBuffer(StringBuffer buffer, int depth) {
		int indent = depth * _tab;

		for (int i = 0; i < indent; ++i) {
			buffer.append(' ');
		}
	}
}

 

Thanks!

 

 

 

Please use plain text.
Developer
ankit2grover
Posts: 159
Registered: ‎08-19-2008
My Device: Not Specified

Re: Help with parsing XML (XMLParseDemo.java)

Hi I dn't  know  about the code in XML Demo, but i parse XML in this way

 public void MySplitName(String parse,String delimeter,String name)
    {
        int npos = 0;
        int fpos =0;
        String value = "";
        while(parse.indexOf(delimeter,npos)>0)
            {
                fpos =parse.indexOf(name,fpos)+delimeter.length()-1;
                npos = parse.indexOf(delimeter,npos) +    delimeter.length();
                value  = parse.substring(fpos,npos-delimeter.length());
             if(name =="<Title short=")
             SpecialId.addElement(value);
}
}

 MySplitName( parseXml,</Title>,"<Title short= ");

 

parseXml is ur XML u want to parse

SpecialID is a vector object ,which u can replac as per your requirement........

You may get values as "Forest Gump" >, "Forrest Landis" >............You can modify the code as per your requirement

 

Please use plain text.
Developer
sonicboomboy
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: Help with parsing XML (XMLParseDemo.java)

you should get correctly the ' title ' node attributes via   

NamedNodeMap attributes = node.getAttributes();

iterate through the attributes map to retrieve the desired values.

Got resolved! Press kudo icon!
Please use plain text.
Developer
behrk2
Posts: 367
Registered: ‎11-25-2009
My Device: Not Specified

Re: Help with parsing XML (XMLParseDemo.java)

[ Edited ]

I have figured it out via your help, thanks!

Please use plain text.