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
New Developer
flavyobr
Posts: 7
Registered: ‎03-18-2009
My Device: Not Specified

SAXParseException: Expecting an element

Hi,

 

This code is working fine when I am opening a xml file:
 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DomentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = getClass().getResourceAsStream("test.xml" );
document = builder.parse( inputStream );
 
however it fails when I get the stream from network:
 
connection = (HttpConnection) Connector.open(url);
inputStream = connection.openInputStream();
 
Error message: org.xml.sax.SAXParseException: Expecting an element.
 
It seems an encoding problem as I've create the test.xml based on
stream I received from network. Do you have some idea to fix it?
 
Br
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: SAXParseException: Expecting an element

Generally you need to make wrapper classes for all these things just for

diagnosis.This will let you put most recently read file contents into a user dialog

or send back to you. 

Sure, the SAX parser could read ahead of its parsing but normally, if you can't

find the problem using a desktop with command line utilities, it is something obvious

like an "XML" file containing an elaborate 404 html from a gateway or some dumb thing.

 

 

 

 

New Developer
flavyobr
Posts: 7
Registered: ‎03-18-2009
My Device: Not Specified

Re: SAXParseException: Expecting an element

Hi,

 

It has been fixed, I've used a String and a ByteArrayInputStream.

 

InputStream xml = new ByteArrayInputStream(stream.getBytes());

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse( xml );

 

Br, Flavio

Developer
mnpaslay
Posts: 105
Registered: ‎03-09-2009
My Device: Not Specified

Re: SAXParseException: Expecting an element

I have the same issue I think, but don't understand how you linked back to your original http: source...

 

Thanks!

New Developer
flavyobr
Posts: 7
Registered: ‎03-18-2009
My Device: Not Specified

Re: SAXParseException: Expecting an element

Hi,

 

I am using the folowing code to extract the data from the input stream:

 

 

StringBuffer raw = new StringBuffer();                                
while ( -1 != (len = inputStream.read(data)) ) {
  raw.append(new String(data, 0, len));
  size += len;
}                            

 

 

So this method is called to parse the XML:

 

Parser(raw.toString());

...

public void Parser(String stream) {
  try {
    InputStream xml = new ByteArrayInputStream(stream.getBytes());
     
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();                 
    document = builder.parse( xml );
    e = document.getDocumentElement();
    e.normalize();
  }
  catch ( Exception e ) {
    System.out.println( e.toString() );
  }
}

 

I am not sure what you are trying to do but it works pretty well.

 

Br

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: SAXParseException: Expecting an element

You have several options here. Trying to do anything over a wireless means you need

diagnostic tools. You should have some way to buffer the input and examine it yourself.

Also, parser handlers should be able to tell you what it got and you can look at

the specific thing it didn't like. 

 

Generally, you can't rely on anything over a network to be reliable

and your code needs to be fault tolerant.

 

 

Developer
MRJAVA
Posts: 68
Registered: ‎10-26-2008
My Device: Not Specified

Re: SAXParseException: Expecting an element

Hey, I'm trying the same code that you've posted here, but it doesn't work. Keeps giving SAXParseException at character '&#x0'. I print the contents of the XML data I got, and it looks just fine to me. Nothing out of the ordrinary.

 

Any ideas on what that charater is?  I don't have any "null" values in my XML document....

New Developer
flavyobr
Posts: 7
Registered: ‎03-18-2009
My Device: Not Specified

Re: SAXParseException: Expecting an element

Hi,

 

'&#x0' is the NULL character which isn't allowed in XML documents.

 

http://www.fileformat.info/info/unicode/char/0000/index.htm

 

Br

Developer
MRJAVA
Posts: 68
Registered: ‎10-26-2008
My Device: Not Specified

Re: SAXParseException: Expecting an element

OK, so its the null character, but how did it get into my code? I'm using PHP DOM functions to build the data, there are no null values in my database...the XML file itself looks fine when printed out as a string...

 

Any idea on how to filter out the null character?

Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: SAXParseException: Expecting an element

LOL- your handlers should tell you roughly where you were in parsing and you "should"

have some facility for sending the binary data back to yourself. Alternatively,

before you dismiss the possibility it is in your generated XML, try something

like a binary dump and make sure no one tried to turn it into unicode.