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
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: problem is xml parsing

Actually i am begineer in xml .That why i am facing lot of problem .i learn a lot from this problem .

But result that i am getting is different.Smiley Sad

Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: problem is xml parsing

Last time print only

name and value

 

i will not ask any question.

actually i am not able to unterstand.actually i understand the things

But not able to implement..Smiley Sad 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: problem is xml parsing

maybe a pull parser would be easier for you to understand, like http://www.blackberry.com/developers/docs/7.1.0api/org/xml/sax/helpers/DefaultHandler.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: problem is xml parsing

hello Thanks Sir i am able to Print all value tag  value.

Till now name tag problem till frustate me.

 

Here is my code to print all 

package mypackage;
 
import java.io.InputStream;
 
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.file.FileConnection;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
 
/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
Document doc;
     StreamConnection conn;
     HttpConnection hc = null;
     FileConnection fc=null;
    public MyScreen()
    {        
    Connection  _connectionthread = new Connection();
        _connectionthread.start();
   
   
    }
    
    private class Connection extends Thread
    {
        public Connection(){
            super();
        }
 
        public void run()
        {
            
           
            try{
                
            //Dialog.alert("hai");
               
           
                DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance(); 
                  
                DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
                  
               
                FileConnection fc=(FileConnection)Connector.open("file:///SDCard/a.xml");
              InputStream  is=fc.openInputStream();
            doc= docBuilder.parse(is);
            System.out.println("*******************************doc****"+doc);
                doc.getDocumentElement ().normalize ();
                NodeList list=doc.getElementsByTagName("CharacteristicValue");
                System.out.println("*******************************list****"+list);
             String   _node=new String();
             String  _element = new String();
                
 
                for (int i=0;i<list.getLength();i++){
                 Node node = list.item(i);
                 
                      if (node.getNodeType() == Node.ELEMENT_NODE) {
 
                          System.out
                                  .println("=====================");
 
                          Element e = (Element) node;
                          NodeList nodeList = e.getElementsByTagName("Value");
                          System.out.println("value: "
                                  + nodeList.item(0).getChildNodes().item(0)
                                          .getNodeValue());
 
                          
                      }
               
                }//end for
            }//end try
            //will catch any exception thrown by the XML parser
            catch (Exception e){
                System.out.println(e.toString());
            }
        }//end connection function
    }// end connection class
    
}
 
Highlighted
Developer
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: problem is xml parsing

package mypackage;

import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.file.FileConnection;

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

import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
	 Document doc;
     StreamConnection conn;
     HttpConnection hc = null;
     FileConnection fc=null;
    public MyScreen()
    {        
    	Connection  _connectionthread = new Connection();
        _connectionthread.start();
    	
  	 
    }
    
    private class Connection extends Thread
    {
        public Connection(){
            super();
        }

        public void run()
        {
            
           
            try{
                
            	//Dialog.alert("hai");
               
            	
                DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance(); 
                  
                DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
                  
               
                FileConnection fc=(FileConnection)Connector.open("file:///SDCard/a.xml");
              InputStream  is=fc.openInputStream();
            doc= docBuilder.parse(is);
            System.out.println("*******************************doc****"+doc);
                doc.getDocumentElement ().normalize ();
                NodeList list=doc.getElementsByTagName("CharacteristicValue");
                System.out.println("*******************************list****"+list);
           

                for (int i=0;i<list.getLength();i++){
                	  Node node = list.item(i);
                	  
                	  if ( node.hasChildNodes() ) {
                		  NodeList level2List = node.getChildNodes();
                		  for ( int j = 0; j < level2List.getLength(); j++ ) {
                			  
                			  Element e = (Element) node;
                              NodeList nodeList = e.getElementsByTagName("Name");
                              System.out.println("value: "
                                      + nodeList.item(0).getChildNodes().item(0)
                                              .getNodeValue());
                		  }
                	  }
                	  
                      if (node.getNodeType() == Node.ELEMENT_NODE) {

                          System.out
                                  .println("=====================");

                          Element e = (Element) node;
                          NodeList nodeList = e.getElementsByTagName("Value");
                          System.out.println("Name: "
                                  + nodeList.item(0).getChildNodes().item(0)
                                          .getNodeValue());

                          
                      }
                	
                	
//                	 String node = list.item(i).getFirstChild().getTextContent();
//                	 Node nn=list.item(i).getChildNodes().item(0).getFirstChild();
//                	String ss= list.item(i).getChildNodes().item(0).getNodeName();
//                	String sss=list.item(i).getChildNodes().item(0).getNodeValue();
//                	int bs=list.item(i).getChildNodes().item(0).hashCode();
//                	
//                	 
//                	 
//                	 System.out.println("**************Node");
//                	
//                	
//                	Node textNode = list.item(i).getFirstChild(); 
//                System.out.println("*******************************textNode****"+textNode);
//                
//                System.out.println("*******************************textNode.getNodeValue()****"+textNode.getNodeValue());
//                    Node value=list.item(i).
//                      getChildNodes().item(0);
//                    _node=list.item(i).getNodeName();
//                    _element=value.getNodeValue();
//                System.out.println("*****************_node+ value"+_node+_element);
                }//end for
            }//end try
            //will catch any exception thrown by the XML parser
            catch (Exception e){
                System.out.println(e.toString());
            }
        }//end connection function
    }// end connection class
    
}

 

 

Hello i got success in printing all name and value but there is a proble , Name is pronting 3 time?

i am not able to understand why it length display 3.