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
Contributor
thinkerRay
Posts: 16
Registered: ‎06-27-2012
My Device: 9900
Accepted Solution

I really need a help from professional in XML Parsing to XML Document, or I want to read XML

Hello everyone,

 

I really seek your gentle help. I am suffering since two days ago. I am working on Blackberry java app. I have xml file located in "file:///store/home/user/myNotes.xml"

 

I want to be able to parse XML into XMLDocument to deal with teh NodeList and ChildNode.

 

I am always getting Uncaught Exception RunTimeError

 

NOTE the following please:

1- My JDK is 1.7.0.5.5 (SE) only

2- I dont have "XMLSerializer", Or "Transformer", Or "TransformerFactory", Or "KXML"

3- All what I want is simply to be able to read XML located above and parse it to XMLDocument.

 

Find my Java code below:

=====================================================================================

 private void parseXmlFile() throws RuntimeException{
        try {

            XMLDocument dom = null;
            private static final String fileN = "file:///store/home/user/myNotes.xml";
            FileConnection fc = (FileConnection)Connector.open(fileN);
            fc.openDataInputStream();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream is = getClass().getResourceAsStream(fileN);
            dom = builder.parse(is);
    
        }catch(Exception pce) {
            Dialog.alert(pce.toString());
        }
        
    }

======================================================================

 

End of my Code

 

Thank you very much for your time reading and replying to help me.

I really appreciate that.

Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook

Re: I really need a help from professional in XML Parsing to XML Document, or I want to read XML

[ Edited ]

There is a xmlDemo that comes with SDK. you can import it and check it.

Also, there are ton of codes available on net.

 

check few of these on this forum

http://supportforums.blackberry.com/t5/Java-Development/Use-the-XML-Parser/ta-p/445210

http://supportforums.blackberry.com/t5/Java-Development/xml-parsing-using-dom/m-p/547221#M111026

http://supportforums.blackberry.com/t5/Java-Development/Help-with-parsing-XML-XMLParseDemo-java/td-p...

http://supportforums.blackberry.com/t5/Java-Development/Xml-parsing-using-sax-parser/m-p/1651191#M19...

 

Thanks

Abhinav Tyagi

 

 

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Developer
pankajace12
Posts: 206
Registered: ‎04-30-2011
My Device: BlackBerry Z10

Re: I really need a help from professional in XML Parsing to XML Document, or I want to read XML

Hi

Can you share you xml code.

Thanks
Pawan
Developer
pankajace12
Posts: 206
Registered: ‎04-30-2011
My Device: BlackBerry Z10

Re: I really need a help from professional in XML Parsing to XML Document, or I want to read XML

Hello

 

Try this code. here you will get the Document obj.

 

public static Document getXMLDocument() throws IOException
	{
		FileConnection fconn;
		InputStream input = null;
		try
		{
			fconn = (FileConnection)Connector.open( "file:///store/home/user/myNotes.xml");
			if(fconn.exists()==true)
			{
				input = fconn.openInputStream();
			}
		}
		catch(Exception e)
		{
			System.out.println("Not able to convert byte.");
		}
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document document = null;
		try {
			document = builder.parse(input);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		document.getDocumentElement().normalize();
		return document;
	}

 

Henceforth,

use like this.

 

Document doc = getXMLDocument();

 

Nodelist nodeList = doc.getElementsByTagName("<tagName>");

 

Thanks

Pawan

 

Contributor
thinkerRay
Posts: 16
Registered: ‎06-27-2012
My Device: 9900

Re: I really need a help from professional in XML Parsing to XML Document, or I want to read XML

Dear All,

Greetings and thanks from the bottom of my heart. It works. My problem was missing the "Normalization".

Thank you again I raelly appreciated your valuable input.