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: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

SAXParseException

hi, i've got such an xml:

 

<?xml version="1.0" encoding="UTF-8" ?>
<billing limit="100" currency="PLN" account_ballance="" account_ballance_date="" period_start="1">
  <item type="voice" max_ammount="100.00"/>
  <item type="data" max_ammount="50000"/>
  <item type="sms" max_ammount="10.00"/>
  <item type="mms" max_ammount="10.00"/>
</billing>

now, to get the first element (where limit, currency.. ) i've used such a code:

 

  public Element getRootElement(String path, String fileName) {

  try {
  fileConnection = (FileConnection) Connector.open(path + fileName, Connector.READ);

  documentBuilderFactory = DocumentBuilderFactory.newInstance();
  documentBuilder = documentBuilderFactory.newDocumentBuilder();
  dis = fileConnection.openDataInputStream();
  document = documentBuilder.parse(dis);
  rootElement = document.getDocumentElement();
  rootElement.normalize();

  } catch (IOException e) {
  RimLog.writeToLog("RimXml.txt", "failed1 "+e);
  } catch (ParserConfigurationException e) {
  RimLog.writeToLog("RimXml.txt", "failed2 "+e);
  } catch (SAXException e) {
  RimLog.writeToLog("RimXml.txt", "failed3 "+e);
  } finally {

  try {
  if (dis != null) {
  dis.close();
  }
  if (fileConnection != null) {
  fileConnection.close();
  }
  } catch (Exception e) {
  }
  }
  return rootElement;
  }

 

but i got: org.xml.sax.SaxParseException: Expecting an element

 

i do not understand what is wrong with this code? i do not understand it more because it sometimes works, sometimes throws this exception! note that i test it all the time on the same file! (the one i've presented at the beginning).

 

 can anyone help me with that?

 

 

regards

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: SAXParseException

[ Edited ]

Can I suggest that you try feeding your 'file' in as a String to make absolutely sure there is not something silly going on here.

 

For example you could change your code as follows:

 

Replace this:

  fileConnection = (FileConnection) Connector.open(path + fileName, Connector.READ);

  documentBuilderFactory = DocumentBuilderFactory.newInstance();
  documentBuilder = documentBuilderFactory.newDocumentBuilder();
  dis = fileConnection.openDataInputStream();
  document = documentBuilder.parse(dis);

 

with this:

 

String xmlDefinition = "<?xml version=\""1.0\"" .....

  documentBuilderFactory = DocumentBuilderFactory.newInstance();
  documentBuilder = documentBuilderFactory.newDocumentBuilder();

  document = documentBuilder.parse(

    new InputStreamReader(new ByteArrayInputStream(xmlDefinition.getBytes()) );

 

I'm not sure that code will work, but you get the idea. 

 

This will remove the possibility that the file you are reading differs from time to time. 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: SAXParseException

Peter's suggestion is spot on. The exception message suggests an error in the XML. I'd only suggest changing the last statement in Peter's code to:

 

    document = documentBuilder.parse( new ByteArrayInputStream(xmlDefinition.getBytes("UTF-8")) );

 

Since the parser can work directly with an InputStream, there's no need to both encode and decode before feeding the bytes to the parser, so long as the bytes are a UTF-8 encoding, so as to match what the XML declares.

 

Also, I would strongly suggest changing:

 

    dis = fileConnection.openDataInputStream();
    document = documentBuilder.parse(dis);

 

to:

 

    is = fileConnection.openInputStream();

    document=documentBuilder.parse(is);

 

There is no reason to use a DataInputStream. The parser does not expect one and (at best) all it does is add overhead to delivering data to the parser. At worst, it "helpfully" interprets the byte stream as some higher-level data type, which would cause the parser to gag.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: SAXParseException

hi, unfortunatelly, both:

 

1)
            fileConnection = (FileConnection) Connector.open(path + fileName, Connector.READ);
            is = fileConnection.openDataInputStream();

            StringBuffer sb = new StringBuffer();
            String xml = "";
            int c;

            while((c=is.read()) != -1){
                sb.append((char)c);
            }

            xml = sb.toString();
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));

            rootElement = document.getDocumentElement();
            rootElement.normalize();
			
			
2)
			fileConnection = (FileConnection) Connector.open(path + fileName, Connector.READ);
            is = fileConnection.openDataInputStream();
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            document = documentBuilder.parse(is);

            rootElement = document.getDocumentElement();
            rootElement.normalize();

 

 

gave me the same error: SAXParseException: Expecting an element.

 there is nothing wrong with an xml document. as i've mentioned before-i coud parse it few times, then the code just started throwing me this exception. then once i also could pare it and then again these exceptions. NOTHING was changed in xml! it looks that this class is not well implemented..

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: SAXParseException

Try the following. Remove the <item> entries, and reparse, and see if you get the exception. Isolate the smallest segment of XML that produces the SAX exception.

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: SAXParseException

i cannot. i download this xml. ok i can read it to a stringBuffer and then parse it and try to remove "item" or others.. but.. yhh... that is not right.  also-how can You explain that sometimes this code works for the same xml file?

 

 regards

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: SAXParseException

i've tried kxml but i got probles with reading! i've used such a code:

 

  fileConnection = (FileConnection) Connector.open(path, Connector.READ);
  is = fileConnection.openDataInputStream();

  StringBuffer sb = new StringBuffer();
  int c;

  while((c=is.read()) != -1){
  sb.append((char)c);
  }

   kxmlParser = new KXmlParser();
  kxmlParser.setInput(is, "UTF-8");
  document = new Document();
  document.parse(kxmlParser);

but.. when i write stringBuffer (which should haved read values from my file) it is empty! when i check fileConnection.fileSize() it is 0!! but when i check it on the device, my xml has 317bytes and when i open it using desktop manager i can see that it is my xml!! not any empty document!!

what is wrong???

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: SAXParseException

Well, if we know *where* the exception is occuring, both in your code and in the source file, I think we might be in a better position to say what is what.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: SAXParseException

You have not actually tried my suggestion and I would encourage you to do so.

 

Remove the read of the file completely from your code.  Hard code the String that you think you are reading from the file into you program.  Then parse the String.  If this works, then the problem is in the reading (which is where I think it is).  If not then the problem is in the XML.  Either way, the problem is almost certainly not in the parser.  Testing with a valid hard coded String will determine quickly where the problem is.  Otherwise we are flying blind here and wasting a lot of our time. 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: SAXParseException

oh, i'm so stupid!! shame to say but it was all my fault.. i downloaded xml file from server and stared parsing it. actions happens too fast and sometimes file was downloaded and fileConn was closed and i could parse it (it was these exceptions when it worked which i wrote about. i mean i was wondering why it does not ALYWAS fails). but usually parsing function did not work cause i called it too fast. i've fixed it and the code i've presented at the beginning works.. thank You all for Your suggestions and time!

 regards and have a nice day Smiley Happy