06-28-2012 11:21 AM
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.
Solved! Go to Solution.
06-28-2012 11:59 AM - edited 06-28-2012 12:00 PM
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-Develo
http://supportforums.blackberry.com/t5/Java-Develo
Thanks
Abhinav Tyagi
06-28-2012 02:55 PM
06-28-2012 03:21 PM
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
06-28-2012 04:50 PM
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.